代码之家  ›  专栏  ›  技术社区  ›  Mark Allison

如何使用Javascript在鼠标上方放大图像?

  •  0
  • Mark Allison  · 技术社区  · 7 年前

    我做错了什么?

    document.getElementById("photo").onmouseover = function() {
      mouseOver()
    };
    document.getElementById("photo").onmouseout = function() {
      mouseOut()
    };
    
    function getPhoto(handle) {
      var img = new Image();
      var div = document.getElementById("photo");
      while (div.firstChild) {
        div.removeChild(div.firstChild);
      }
      img.src = "https://res.cloudinary.com/harmhxmnk/image/upload/" + handle;
      img.height = 32;
      img.onload = function() {
        div.appendChild(img);
      };
    }
    
    function mouseOver() {
      var img = document.getElementById("photo");
      img.height = 100;
    }
    
    function mouseOut() {
      // TODO
    }
    .photo {
      position: absolute;
      top: 86px;
      right: 92px;
    }
    <div class="photo" id="photo"></div>
    4 回复  |  直到 7 年前
        1
  •  4
  •   kev    7 年前

    .photo {
      position: absolute;
      top: 86px;
      right: 92px;
      transform: scale(1);
      transition: transform .5s;
    }
    
    .photo:hover {
      transform: scale(1.2);
    }
    <div class="photo" id="photo"><img src="https://wallpaperbrowse.com/media/images/3848765-wallpaper-images-download.jpg" /></div>
        2
  •  1
  •   epascarello    7 年前

    function mouseOver() {
      var img = document.querySelector("#photo img");
      img.height = 100;
    }
    
        3
  •  0
  •   Max90    7 年前

    我认为仅仅使用css来实现这一点会更容易、更干净

    #photo:hover{
        height: 100:
    }
    
        4
  •  0
  •   Andy    7 年前

    使用flexbox使用CSS非常简单( demo

    .col {
      display: flex;
    }
    
    .col img {
      margin: 5px 5px; /* more margin makes the image smaller */
      flex-grow: 1;
    }
    
    .col img:hover {
      margin: 0 0; /* less margin on hover makes the image bigger */
    }
    <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
    <div class="container">
      <div class="row">
        <div class="col">       
            <img src="https://placekitten.com/g/200/200">
        </div>
        <div class="col">
          <img src="https://placekitten.com/g/200/200">
        </div>
      </div>
      <div class="row">
        <div class="col">
          <img src="https://placekitten.com/g/200/200">
        </div>
        <div class="col">
          <img src="https://placekitten.com/g/200/200">
        </div>
        <div class="col">
          <img src="https://placekitten.com/g/200/200">
        </div>
      </div>  
    </div>