【问题标题】:How can I show my magnifying glass only when I hover over the image in CSS?仅当我将鼠标悬停在 CSS 中的图像上时,如何才能显示我的放大镜?
【发布时间】:2020-02-22 20:19:59
【问题描述】:

// magnify hover
function magnify(imgID, zoom) {
  var img, glass, w, h, bw;
  img = document.getElementById(imgID);
  /*create magnifier glass:*/
  glass = document.createElement("DIV");
  glass.setAttribute("class", "img-magnifier-glass");
  /*insert magnifier glass:*/
  img.parentElement.insertBefore(glass, img);
  /*set background properties for the magnifier glass:*/
  glass.style.backgroundImage = "url('" + img.src + "')";
  glass.style.backgroundRepeat = "no-repeat";
  glass.style.backgroundSize = (img.width * zoom) + "px " + (img.height * zoom) + "px";
  bw = 3;
  w = glass.offsetWidth / 2;
  h = glass.offsetHeight / 2;
  /*execute a function when someone moves the magnifier glass over the image:*/
  glass.addEventListener("mousemove", moveMagnifier);
  img.addEventListener("mousemove", moveMagnifier);
  /*and also for touch screens:*/
  glass.addEventListener("touchmove", moveMagnifier);
  img.addEventListener("touchmove", moveMagnifier);
  function moveMagnifier(e) {
    var pos, x, y;
    /*prevent any other actions that may occur when moving over the image*/
    e.preventDefault();
    /*get the cursor's x and y positions:*/
    pos = getCursorPos(e);
    x = pos.x;
    y = pos.y;
    /*prevent the magnifier glass from being positioned outside the image:*/
    if (x > img.width - (w / zoom)) {x = img.width - (w / zoom);}
    if (x < w / zoom) {x = w / zoom;}
    if (y > img.height - (h / zoom)) {y = img.height - (h / zoom);}
    if (y < h / zoom) {y = h / zoom;}
    /*set the position of the magnifier glass:*/
    glass.style.left = (x - w) + "px";
    glass.style.top = (y - h) + "px";
    /*display what the magnifier glass "sees":*/
    glass.style.backgroundPosition = "-" + ((x * zoom) - w + bw) + "px -" + ((y * zoom) - h + bw) + "px";
  }
  function getCursorPos(e) {
    var a, x = 0, y = 0;
    e = e || window.event;
    /*get the x and y positions of the image:*/
    a = img.getBoundingClientRect();
    /*calculate the cursor's x and y coordinates, relative to the image:*/
    x = e.pageX - a.left;
    y = e.pageY - a.top;
    /*consider any page scrolling:*/
    x = x - window.pageXOffset;
    y = y - window.pageYOffset;
    return {x : x, y : y};
  }
}
/* Initiate Magnify Function
with the id of the image, and the strength of the magnifier glass:*/
magnify("mag", 2);
.img-magnifier-glass {
  position: absolute;
  z-index: 3;
  border: 3px solid #000;
  border-radius: 40%;
  cursor: none;
  /*Set the size of the magnifier glass:*/
  width: 100px;
  height: 100px;
}
&lt;table class="img-magnifier-container" width="115" border="0" cellpadding="0" cellspacing="0" style="display: inline-block"&gt;&lt;tr&gt;&lt;td width="115" style="text-align: center"&gt;&lt;center&gt;&lt;a href="https://www.imdb.com/title/tt3315342/" title="Logan 4K (2017)" target="_blank"&gt;&lt;img class="cover" width="115" height="133" border="0" id="mag" class="pstrart" id="pstr" src="https://images.static-bluray.com/movies/covers/174423_front.jpg" style=""&gt;&lt;/a&gt;&lt;/center&gt;&lt;center&gt;&lt;input type="checkbox" name="movieboxes" value="Logan" style="width: 15px; height: 15px; margin: 0px;"&gt;

你好,

如何使放大镜仅在我将鼠标悬停在图像上时显示?我已尝试在此网站和其他地方进行搜索,但我的理解不足以使解决方案适用于我的情况。

我从 W3School 复制了这个,但他们没有说明我可以让它在我将鼠标悬停在它上面时显示的部分。

我看到了模态框,并试图复制它的悬停功能,但它似乎不起作用。 谢谢!

【问题讨论】:

    标签: javascript html css hover


    【解决方案1】:

    只需在悬停时切换不透明度:

    // magnify hover
    function magnify(imgID, zoom) {
      var img, glass, w, h, bw;
      img = document.getElementById(imgID);
      /*create magnifier glass:*/
      glass = document.createElement("DIV");
      glass.setAttribute("class", "img-magnifier-glass");
      /*insert magnifier glass:*/
      img.parentElement.insertBefore(glass, img);
      /*set background properties for the magnifier glass:*/
      glass.style.backgroundImage = "url('" + img.src + "')";
      glass.style.backgroundRepeat = "no-repeat";
      glass.style.backgroundSize = (img.width * zoom) + "px " + (img.height * zoom) + "px";
      bw = 3;
      w = glass.offsetWidth / 2;
      h = glass.offsetHeight / 2;
      /*execute a function when someone moves the magnifier glass over the image:*/
      glass.addEventListener("mousemove", moveMagnifier);
      img.addEventListener("mousemove", moveMagnifier);
      /*and also for touch screens:*/
      glass.addEventListener("touchmove", moveMagnifier);
      img.addEventListener("touchmove", moveMagnifier);
      function moveMagnifier(e) {
        var pos, x, y;
        /*prevent any other actions that may occur when moving over the image*/
        e.preventDefault();
        /*get the cursor's x and y positions:*/
        pos = getCursorPos(e);
        x = pos.x;
        y = pos.y;
        /*prevent the magnifier glass from being positioned outside the image:*/
        if (x > img.width - (w / zoom)) {x = img.width - (w / zoom);}
        if (x < w / zoom) {x = w / zoom;}
        if (y > img.height - (h / zoom)) {y = img.height - (h / zoom);}
        if (y < h / zoom) {y = h / zoom;}
        /*set the position of the magnifier glass:*/
        glass.style.left = (x - w) + "px";
        glass.style.top = (y - h) + "px";
        /*display what the magnifier glass "sees":*/
        glass.style.backgroundPosition = "-" + ((x * zoom) - w + bw) + "px -" + ((y * zoom) - h + bw) + "px";
      }
      function getCursorPos(e) {
        var a, x = 0, y = 0;
        e = e || window.event;
        /*get the x and y positions of the image:*/
        a = img.getBoundingClientRect();
        /*calculate the cursor's x and y coordinates, relative to the image:*/
        x = e.pageX - a.left;
        y = e.pageY - a.top;
        /*consider any page scrolling:*/
        x = x - window.pageXOffset;
        y = y - window.pageYOffset;
        return {x : x, y : y};
      }
    }
    /* Initiate Magnify Function
    with the id of the image, and the strength of the magnifier glass:*/
    magnify("mag", 2);
    .img-magnifier-glass {
      position: absolute;
      z-index: 3;
      border: 3px solid #000;
      border-radius: 40%;
      cursor: none;
      /*Set the size of the magnifier glass:*/
      width: 100px;
      height: 100px;
      opacity:0;
      pointer-events:none;
    }
    a:hover .img-magnifier-glass{
      opacity:1;
      pointer-events:initial;
    }
    &lt;table class="img-magnifier-container" width="115" border="0" cellpadding="0" cellspacing="0" style="display: inline-block"&gt;&lt;tr&gt;&lt;td width="115" style="text-align: center"&gt;&lt;center&gt;&lt;a href="https://www.imdb.com/title/tt3315342/" title="Logan 4K (2017)" target="_blank"&gt;&lt;img class="cover" width="115" height="133" border="0" id="mag" class="pstrart" id="pstr" src="https://images.static-bluray.com/movies/covers/174423_front.jpg" style=""&gt;&lt;/a&gt;&lt;/center&gt;&lt;center&gt;&lt;input type="checkbox" name="movieboxes" value="Logan" style="width: 15px; height: 15px; margin: 0px;"&gt;

    【讨论】:

    • opacity 将使元素透明。因此,如果放大镜与按钮之类的任何元素重叠,则用户无法单击它。更好的用户display 而不是opacity
    • 尝试放大图像的底部,然后尝试单击下面的复选框。你不能因为放大镜重叠它,当你悬停复选框时出现
    • @BanujanBalendrakumar 我们添加pointer-events:none; 来解决这个问题
    • 我注意到我只能将它应用于页面上的一个图像;有没有办法可以将它应用于任意数量的图像?
    • 放大镜
    【解决方案2】:

    您可以使用javascript mouse event handlers。我在下面修改了你的代码 sn-p。

    我所做的是,

    .img-magnifier-glass {
      ...
      display:none; // hide the magnifier by default
    }
    
    // Show the magnifier when hover the container
    $('.img-magnifier-container').mouseover(function(){
      $('.img-magnifier-glass').show();
    });
    
    // Hide the magnifier when leave the container
    $('.img-magnifier-container').mouseout(function(){
      $('.img-magnifier-glass').hide();
    });
    
    

    // magnify hover
    function magnify(imgID, zoom) {
      var img, glass, w, h, bw;
      img = document.getElementById(imgID);
      /*create magnifier glass:*/
      glass = document.createElement("DIV");
      glass.setAttribute("class", "img-magnifier-glass");
      /*insert magnifier glass:*/
      img.parentElement.insertBefore(glass, img);
      /*set background properties for the magnifier glass:*/
      glass.style.backgroundImage = "url('" + img.src + "')";
      glass.style.backgroundRepeat = "no-repeat";
      glass.style.backgroundSize = (img.width * zoom) + "px " + (img.height * zoom) + "px";
      bw = 3;
      w = glass.offsetWidth / 2;
      h = glass.offsetHeight / 2;
      /*execute a function when someone moves the magnifier glass over the image:*/
      glass.addEventListener("mousemove", moveMagnifier);
      img.addEventListener("mousemove", moveMagnifier);
      /*and also for touch screens:*/
      glass.addEventListener("touchmove", moveMagnifier);
      img.addEventListener("touchmove", moveMagnifier);
      function moveMagnifier(e) {
        var pos, x, y;
        /*prevent any other actions that may occur when moving over the image*/
        e.preventDefault();
        /*get the cursor's x and y positions:*/
        pos = getCursorPos(e);
        x = pos.x;
        y = pos.y;
        /*prevent the magnifier glass from being positioned outside the image:*/
        if (x > img.width - (w / zoom)) {x = img.width - (w / zoom);}
        if (x < w / zoom) {x = w / zoom;}
        if (y > img.height - (h / zoom)) {y = img.height - (h / zoom);}
        if (y < h / zoom) {y = h / zoom;}
        /*set the position of the magnifier glass:*/
        glass.style.left = (x - w) + "px";
        glass.style.top = (y - h) + "px";
        /*display what the magnifier glass "sees":*/
        glass.style.backgroundPosition = "-" + ((x * zoom) - w + bw) + "px -" + ((y * zoom) - h + bw) + "px";
      }
      function getCursorPos(e) {
        var a, x = 0, y = 0;
        e = e || window.event;
        /*get the x and y positions of the image:*/
        a = img.getBoundingClientRect();
        /*calculate the cursor's x and y coordinates, relative to the image:*/
        x = e.pageX - a.left;
        y = e.pageY - a.top;
        /*consider any page scrolling:*/
        x = x - window.pageXOffset;
        y = y - window.pageYOffset;
        return {x : x, y : y};
      }
    }
    /* Initiate Magnify Function
    with the id of the image, and the strength of the magnifier glass:*/
    magnify("mag", 2);
    
    $('.img-magnifier-container').mouseover(function(){
      $('.img-magnifier-glass').show();
    });
    
    $('.img-magnifier-container').mouseout(function(){
      $('.img-magnifier-glass').hide();
    });
    .img-magnifier-glass {
      position: absolute;
      z-index: 3;
      border: 3px solid #000;
      border-radius: 40%;
      cursor: none;
      /*Set the size of the magnifier glass:*/
      width: 100px;
      height: 100px;
      display:none;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <table class="img-magnifier-container" width="115" border="0" cellpadding="0" cellspacing="0" style="display: inline-block"><tr><td width="115" style="text-align: center"><center><a href="https://www.imdb.com/title/tt3315342/" title="Logan 4K (2017)" target="_blank"><img class="cover" width="115" height="133" border="0" id="mag" class="pstrart" id="pstr" src="https://images.static-bluray.com/movies/covers/174423_front.jpg" style=""></a></center><center><input type="checkbox" name="movieboxes" value="Logan" style="width: 15px; height: 15px; margin: 0px;">

    【讨论】:

    • 注意玻璃的中心如何不再跟随光标
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-08
    • 1970-01-01
    • 2022-11-20
    • 1970-01-01
    • 2019-07-04
    相关资源
    最近更新 更多