鼠标悬停缩放图像仅在鼠标悬停时显示

我制作了这个鼠标悬停图像缩放页面。但我希望输出显示在左侧,并且仅当有人将鼠标悬停在其上时。在此代码中,输出窗口不断打开。


这是我的代码:


function imageZoom(imgID, resultID) {

  var img, lens, result, cx, cy;

  img = document.getElementById(imgID);

  result = document.getElementById(resultID);

  /*create lens:*/

  lens = document.createElement("DIV");

  lens.setAttribute("class", "img-zoom-lens");

  /*insert lens:*/

  img.parentElement.insertBefore(lens, img);

  /*calculate the ratio between result DIV and lens:*/

  cx = result.offsetWidth / lens.offsetWidth;

  cy = result.offsetHeight / lens.offsetHeight;

  /*set background properties for the result DIV:*/

  result.style.backgroundImage = "url('" + img.src + "')";

  result.style.backgroundSize = (img.width * cx) + "px " + (img.height * cy) + "px";

  /*execute a function when someone moves the cursor over the image, or the lens:*/

  lens.addEventListener("mousemove", moveLens);

  img.addEventListener("mousemove", moveLens);

  /*and also for touch screens:*/

  lens.addEventListener("touchmove", moveLens);

  img.addEventListener("touchmove", moveLens);


  function moveLens(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);

    /*calculate the position of the lens:*/

    x = pos.x - (lens.offsetWidth / 2);

    y = pos.y - (lens.offsetHeight / 2);

    /*prevent the lens from being positioned outside the image:*/

    if (x > img.width - lens.offsetWidth) {

      x = img.width - lens.offsetWidth;

    }

    if (x < 0) {

      x = 0;

    }

    if (y > img.height - lens.offsetHeight) {

      y = img.height - lens.offsetHeight;

    }

    if (y < 0) {

      y = 0;

    }

    /*set the position of the lens:*/

    lens.style.left = x + "px";

    lens.style.top = y + "px";

    /*display what the lens "sees":*/

    result.style.backgroundPosition = "-" + (x * cx) + "px -" + (y * cy) + "px";

  }


该代码运行良好,但结果应该在右侧,并且仅当用户将鼠标悬停在其上时才显示。


红糖糍粑
浏览 77回答 2
2回答

森栏

下面我修改了你的 CSS。display: flex是你所追求的。然后我让你的代码隐藏result元素并只在mouseover. 然后我让它隐藏在mouseout.您可以调整 CSS 等以匹配您的风格,但这是我为您准备的片段;function imageZoom(imgID, resultID) {&nbsp; var img, lens, result, cx, cy;&nbsp; img = document.getElementById(imgID);&nbsp; result = document.getElementById(resultID);&nbsp; /*create lens:*/&nbsp; lens = document.createElement("DIV");&nbsp; lens.setAttribute("class", "img-zoom-lens");&nbsp; /*insert lens:*/&nbsp; img.parentElement.insertBefore(lens, img);&nbsp; /*calculate the ratio between result DIV and lens:*/&nbsp; cx = result.offsetWidth / lens.offsetWidth;&nbsp; cy = result.offsetHeight / lens.offsetHeight;&nbsp; /*set background properties for the result DIV:*/&nbsp; result.style.backgroundImage = "url('" + img.src + "')";&nbsp; result.style.backgroundSize = (img.width * cx) + "px " + (img.height * cy) + "px";&nbsp; /*execute a function when someone moves the cursor over the image, or the lens:*/&nbsp; lens.addEventListener("mousemove", moveLens);&nbsp; img.addEventListener("mousemove", moveLens);&nbsp; /*and also for touch screens:*/&nbsp; lens.addEventListener("touchmove", moveLens);&nbsp; img.addEventListener("touchmove", moveLens);&nbsp;&nbsp;&nbsp; /*initialise and hide lens result*/&nbsp; result.style.display = "none";&nbsp; /*Reveal and hide on mouseover or out*/&nbsp; lens.onmouseover = function(){result.style.display = "block";};&nbsp; lens.onmouseout = function(){result.style.display = "none";};&nbsp;&nbsp;&nbsp; function moveLens(e) {&nbsp; &nbsp; var pos, x, y;&nbsp; &nbsp; /*prevent any other actions that may occur when moving over the image:*/&nbsp; &nbsp; e.preventDefault();&nbsp; &nbsp; /*get the cursor's x and y positions:*/&nbsp; &nbsp; pos = getCursorPos(e);&nbsp; &nbsp; /*calculate the position of the lens:*/&nbsp; &nbsp; x = pos.x - (lens.offsetWidth / 2);&nbsp; &nbsp; y = pos.y - (lens.offsetHeight / 2);&nbsp; &nbsp; /*prevent the lens from being positioned outside the image:*/&nbsp; &nbsp; if (x > img.width - lens.offsetWidth) {x = img.width - lens.offsetWidth;}&nbsp; &nbsp; if (x < 0) {x = 0;}&nbsp; &nbsp; if (y > img.height - lens.offsetHeight) {y = img.height - lens.offsetHeight;}&nbsp; &nbsp; if (y < 0) {y = 0;}&nbsp; &nbsp; /*set the position of the lens:*/&nbsp; &nbsp; lens.style.left = x + "px";&nbsp; &nbsp; lens.style.top = y + "px";&nbsp; &nbsp; /*display what the lens "sees":*/&nbsp; &nbsp; result.style.backgroundPosition = "-" + (x * cx) + "px -" + (y * cy) + "px";&nbsp; }&nbsp; function getCursorPos(e) {&nbsp; &nbsp; var a, x = 0, y = 0;&nbsp; &nbsp; e = e || window.event;&nbsp; &nbsp; /*get the x and y positions of the image:*/&nbsp; &nbsp; a = img.getBoundingClientRect();&nbsp; &nbsp; /*calculate the cursor's x and y coordinates, relative to the image:*/&nbsp; &nbsp; x = e.pageX - a.left;&nbsp; &nbsp; y = e.pageY - a.top;&nbsp; &nbsp; /*consider any page scrolling:*/&nbsp; &nbsp; x = x - window.pageXOffset;&nbsp; &nbsp; y = y - window.pageYOffset;&nbsp; &nbsp; return {x : x, y : y};&nbsp; }};imageZoom("myimage", "myresult");* {box-sizing: border-box;}.img-zoom-container {&nbsp; position: relative;&nbsp; display: flex;}.img-zoom-lens {&nbsp; position: absolute;&nbsp; border: 1px solid #d4d4d4;&nbsp; /*set the size of the lens:*/&nbsp; width: 40px;&nbsp; height: 40px;}.img-zoom-result {&nbsp; border: 1px solid #d4d4d4;&nbsp; position: absolute;&nbsp; left: 300px; /*match width of #myimage*/&nbsp; /*set the size of the result div:*/&nbsp; width: 300px;&nbsp; height: 300px;}<h1>Image Zoom</h1><p>Mouse over the image:</p><div class="img-zoom-container">&nbsp; <img id="myimage" src="https://hatrabbits.com/wp-content/uploads/2018/10/risky-assumptions.jpg" width="300" height="240">&nbsp; <div id="myresult" class="img-zoom-result" style=""></div></div><p>The image must be placed inside a container with relative positioning.</p><p>The result can be put anywhere on the page, but must have the class name "img-zoom-result".</p><p>Make sure both the image and the result have IDs. These IDs are used when a javaScript initiates the zoom effect.</p>如果您希望我这样做,我可以努力使镜头结果绝对定位并对齐,这样您就不会在底部看到文字跳动。除非你知道如何做所有这些——希望这就足够了?

慕的地10843

默认情况下不透明度:0;并在 mouseenter 上通过设置不透明度使其可见:1;function imageZoom(imgID, resultID) {&nbsp; var img, lens, result, cx, cy;&nbsp; img = document.getElementById(imgID);&nbsp; result = document.getElementById(resultID);&nbsp; /*create lens:*/&nbsp; lens = document.createElement("DIV");&nbsp; lens.setAttribute("class", "img-zoom-lens");&nbsp; /*insert lens:*/&nbsp; img.parentElement.insertBefore(lens, img);&nbsp; /*calculate the ratio between result DIV and lens:*/&nbsp; cx = result.offsetWidth / lens.offsetWidth;&nbsp; cy = result.offsetHeight / lens.offsetHeight;&nbsp; /*set background properties for the result DIV:*/&nbsp; result.style.backgroundImage = "url('" + img.src + "')";&nbsp; result.style.backgroundSize = (img.width * cx) + "px " + (img.height * cy) + "px";&nbsp; /*execute a function when someone moves the cursor over the image, or the lens:*/&nbsp; lens.addEventListener("mousemove", moveLens);&nbsp; img.addEventListener("mousemove", moveLens);&nbsp; /*and also for touch screens:*/&nbsp; lens.addEventListener("touchmove", moveLens);&nbsp; img.addEventListener("touchmove", moveLens);&nbsp; function moveLens(e) {&nbsp; &nbsp; var pos, x, y;&nbsp; &nbsp; /*prevent any other actions that may occur when moving over the image:*/&nbsp; &nbsp; e.preventDefault();&nbsp; &nbsp; /*get the cursor's x and y positions:*/&nbsp; &nbsp; pos = getCursorPos(e);&nbsp; &nbsp; /*calculate the position of the lens:*/&nbsp; &nbsp; x = pos.x - (lens.offsetWidth / 2);&nbsp; &nbsp; y = pos.y - (lens.offsetHeight / 2);&nbsp; &nbsp; /*prevent the lens from being positioned outside the image:*/&nbsp; &nbsp; if (x > img.width - lens.offsetWidth) {&nbsp; &nbsp; &nbsp; x = img.width - lens.offsetWidth;&nbsp; &nbsp; }&nbsp; &nbsp; if (x < 0) {&nbsp; &nbsp; &nbsp; x = 0;&nbsp; &nbsp; }&nbsp; &nbsp; if (y > img.height - lens.offsetHeight) {&nbsp; &nbsp; &nbsp; y = img.height - lens.offsetHeight;&nbsp; &nbsp; }&nbsp; &nbsp; if (y < 0) {&nbsp; &nbsp; &nbsp; y = 0;&nbsp; &nbsp; }&nbsp; &nbsp; /*set the position of the lens:*/&nbsp; &nbsp; lens.style.left = x + "px";&nbsp; &nbsp; lens.style.top = y + "px";&nbsp; &nbsp; /*display what the lens "sees":*/&nbsp; &nbsp; result.style.backgroundPosition = "-" + (x * cx) + "px -" + (y * cy) + "px";&nbsp; }&nbsp; function getCursorPos(e) {&nbsp; &nbsp; var a, x = 0,&nbsp; &nbsp; &nbsp; y = 0;&nbsp; &nbsp; e = e || window.event;&nbsp; &nbsp; /*get the x and y positions of the image:*/&nbsp; &nbsp; a = img.getBoundingClientRect();&nbsp; &nbsp; /*calculate the cursor's x and y coordinates, relative to the image:*/&nbsp; &nbsp; x = e.pageX - a.left;&nbsp; &nbsp; y = e.pageY - a.top;&nbsp; &nbsp; /*consider any page scrolling:*/&nbsp; &nbsp; x = x - window.pageXOffset;&nbsp; &nbsp; y = y - window.pageYOffset;&nbsp; &nbsp; return {&nbsp; &nbsp; &nbsp; x: x,&nbsp; &nbsp; &nbsp; y: y&nbsp; &nbsp; };&nbsp; }}imageZoom("myimage", "myresult");function show(x) {&nbsp; document.getElementById('myresult').style.opacity = 1;}* {&nbsp; box-sizing: border-box;}.img-zoom-container {&nbsp; position: relative;&nbsp; display: flex;}.img-zoom-lens {&nbsp; position: absolute;&nbsp; border: 1px solid #d4d4d4;&nbsp; /*set the size of the lens:*/&nbsp; width: 70px;&nbsp; height: 70px;}#myresult {&nbsp; opacity: 0;}.img-zoom-result {&nbsp; border: 1px solid #d4d4d4;&nbsp; /*set the size of the result div:*/&nbsp; width: 300px;&nbsp; height: auto;}<h1>Image Zoom</h1><p>Mouse over the image:</p><div class="img-zoom-container">&nbsp; <img onmouseenter="show()" id="myimage" src="https://i.pinimg.com/originals/f8/b6/9e/f8b69e6156999b84137f3f0a23701b75.jpg" width="300" height="240">&nbsp; <div id="myresult" class="img-zoom-result"></div></div><p>The image must be placed inside a container with relative positioning.</p><p>The result can be put anywhere on the page, but must have the class name "img-zoom-result".</p><p>Make sure both the image and the result have IDs. These IDs are used when a javaScript initiates the zoom effect.</p>// Initiate zoom effect:
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript