【问题标题】:How to hide GIF and replace with image (in order to use magnifying tool)如何隐藏 GIF 并用图像替换(以便使用放大工具)
【发布时间】:2021-04-01 20:32:32
【问题描述】:

你好 * 世界,

第一篇文章,在这里完成新手!我正在努力与时俱进,研究一些网页设计,因为这个互联网事物显然会继续存在。

有人能告诉我一种隐藏 GIF 的方法(已经只制作了 1 循环)并用图像替换它,这样我就可以使用放大工具了sn-p 我在这里找到的。

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};
  }
}
</script>

<script>
/* Initiate Magnify Function
with the id of the image, and the strength of the magnifier glass:*/
magnify("export-a-product-to-china", 2);
* {box-sizing: border-box;}

.img-magnifier-container {
  position:relative;
}

.img-magnifier-glass {
  position: absolute;
  border: 2px solid #000;
  border-radius: 50%;
  cursor: none;
  /*Set the size of the magnifier glass:*/
  width: 250px;
  height: 250px;
}
<div class="img-magnifier-container">
  <img id="export-a-product-to-china" src="https://static1.squarespace.com/static/5a27e60318b27d7d0e86bd68/t/5fda32c7b424be58e1607b44/1608135367867/anipro_bw_final.svg" width="960" height="540">
</div>

提前非常感谢!

编辑: A Haworth 的以下回答完美而优雅地解决了这个问题(就我和有限的编码知识而言,这是可以理解的)。 感谢他和其他像他一样在这个令人难以置信的网站上的人!

追求知识的真正友情是人类最崇高的待遇之一。老实说,我不敢再希望它退出了。

为新的一年干杯,我希望在这个职位上成长,让我能够像帮助别人一样帮助别人。

【问题讨论】:

    标签: javascript html css gif squarespace


    【解决方案1】:

    问题中的 sn-p 不起作用,因为 JS 的顺序错误,但如果我们更改它,它似乎可以工作 - 即可以将放大镜移动到图像上并看到放大的部分。

    更新:在 cmets 中阐明了在加载时覆盖 GIF 的要求。因此,这个 sn-p 做了两件额外的事情:在容器上使用伪元素 (:after) 来覆盖 GIF,使用 CSS 动画计时将其保持 33 秒(当然可以更改);检测鼠标是否移到了不是主图像的窗口部分上,如果是,则将玻璃显示设置为无。

    function magnify(imgID, zoom) {
      var img, glass, w, h, bw;
      img = document.getElementById(imgID);
      img.parentElement.style.width = img.width + 'px';//ADDED so that the pseudo element can pick up the right dimensions
      img.parentElement.style.height = img.height + 'px';// - ditto - 
      /*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);
      window.addEventListener("mousemove",outside);
      /*and also for touch screens:*/
      glass.addEventListener("touchmove", moveMagnifier);
      img.addEventListener("touchmove", moveMagnifier);
      window.addEventListener("touchmove",outside);
      function outside() {
        glass.style.display = 'none';
      }
      function moveMagnifier(e) {
        glass.style.display = 'block';
        var pos, x, y;
        /*prevent any other actions that may occur when moving over the image*/
        e.preventDefault();
        e.stopPropagation();
        /*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};
      }
    }
    
    function init() {
        magnify("export-a-product-to-china", 2);
    }
    /* Initiate Magnify Function with the id of the image, and the strength of the magnifier glass:*/
    window.onload = init;// this is put in to ensure we don't try to get info about the img before it is loaded
    * {box-sizing: border-box;}
    
    .img-magnifier-container {
      position:relative;
    }
    .img-magnifier-glass {
      position: absolute;
      border: 2px solid #000;
      border-radius: 50%;
      cursor: none;
      /*Set the size of the magnifier glass:*/
      width: 250px;
      height: 250px;
    }
    </style>
    <style>
    .img-magnifier-container {
      width: auto;
      height: auto;
      overflow: hidden;
    }
    .img-magnifier-container:after {
      content: '';
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      background-image: none;
      background-size: cover;
      background-repeat: no-repeat no-repeat;
      background-position: 0 0;
      animation-name: showgif;
      animation-duration: 33s;/* CHANGE THIS TO THE NUMBER OF SECONDS YOU WANT THE GIF TO SHOW */
      animation-iteration-count: 1;
      z-index: -1;
    }
    @keyframes showgif {
      from {
        background-image: url(https://static1.squarespace.com/static/5a27e60318b27d7d0e86bd68/t/5fe7115448ad1f1396df7d5e/1608978804344/anipro_bw_finalgif.gif);
        z-index: 999999;
      }
      to {  
        background-image: url(https://static1.squarespace.com/static/5a27e60318b27d7d0e86bd68/t/5fe7115448ad1f1396df7d5e/1608978804344/anipro_bw_finalgif.gif);
        z-index: 999999;
      }
    }
    <div class="img-magnifier-container">
      <img id="export-a-product-to-china" src="https://static1.squarespace.com/static/5a27e60318b27d7d0e86bd68/t/5fda32c7b424be58e1607b44/1608135367867/anipro_bw_final.svg" width="960" height="540">
    </div>

    【讨论】:

    • 是的,我的错!谢谢霍沃斯先生。 sn-p 没问题。问题是我如何能够运行 GIF 并在它播放后被图形替换(包括放大工具)。能否告诉我光标离开图形后如何隐藏放大镜?
    • 将您的 gif 转换为单独的图像或精灵,然后我们可以为它们制作动画,完成后我们将其删除并显示图表和放大镜。
    • 或者,如果不需要精确的准确性,只需对 GIF 进行计时,我们可以设置超时并在完成时将其淡出并显示图表。能发个 GIF 的 URL 吗?对于消失的放大镜,您可能需要感应鼠标移出。
    • 当然!这是链接:static1.squarespace.com/static/5a27e60318b27d7d0e86bd68/t/… 超时选项似乎完全可行! GIF 的持续时间为 33 秒。
    • 祝您和所有友好的帮助者圣诞快乐。你让生活变得美好。