为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

红糖糍粑 2022-12-09 13:53:53
我制作了这个鼠标悬停图像缩放页面。但我希望输出显示在左侧,并且仅当有人将鼠标悬停在其上时。在此代码中,输出窗口不断打开。这是我的代码: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";  }该代码运行良好,但结果应该在右侧,并且仅当用户将鼠标悬停在其上时才显示。
查看完整描述

2 回答

?
森栏

TA贡献1810条经验 获得超5个赞

下面我修改了你的 CSS。display: flex是你所追求的。


然后我让你的代码隐藏result元素并只在mouseover. 然后我让它隐藏在mouseout.


您可以调整 CSS 等以匹配您的风格,但这是我为您准备的片段;


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);

  

  /*initialise and hide lens result*/

  result.style.display = "none";

  /*Reveal and hide on mouseover or out*/

  lens.onmouseover = function(){result.style.display = "block";};

  lens.onmouseout = function(){result.style.display = "none";};

  

  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";

  }

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

  }

};


imageZoom("myimage", "myresult");

* {box-sizing: border-box;}


.img-zoom-container {

  position: relative;

  display: flex;

}


.img-zoom-lens {

  position: absolute;

  border: 1px solid #d4d4d4;

  /*set the size of the lens:*/

  width: 40px;

  height: 40px;

}


.img-zoom-result {

  border: 1px solid #d4d4d4;

  position: absolute;

  left: 300px; /*match width of #myimage*/

  /*set the size of the result div:*/

  width: 300px;

  height: 300px;

}

<h1>Image Zoom</h1>


<p>Mouse over the image:</p>


<div class="img-zoom-container">

  <img id="myimage" src="https://hatrabbits.com/wp-content/uploads/2018/10/risky-assumptions.jpg" width="300" height="240">

  <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>


如果您希望我这样做,我可以努力使镜头结果绝对定位并对齐,这样您就不会在底部看到文字跳动。除非你知道如何做所有这些——希望这就足够了?


查看完整回答
反对 回复 2022-12-09
?
慕的地10843

TA贡献1785条经验 获得超8个赞

默认情况下不透明度:0;并在 mouseenter 上通过设置不透明度使其可见:1;


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";

  }


  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

    };

  }

}

imageZoom("myimage", "myresult");


function show(x) {

  document.getElementById('myresult').style.opacity = 1;

}

* {

  box-sizing: border-box;

}


.img-zoom-container {

  position: relative;

  display: flex;

}


.img-zoom-lens {

  position: absolute;

  border: 1px solid #d4d4d4;

  /*set the size of the lens:*/

  width: 70px;

  height: 70px;

}


#myresult {

  opacity: 0;

}


.img-zoom-result {

  border: 1px solid #d4d4d4;

  /*set the size of the result div:*/

  width: 300px;

  height: auto;

}

<h1>Image Zoom</h1>


<p>Mouse over the image:</p>


<div class="img-zoom-container">

  <img onmouseenter="show()" id="myimage" src="https://i.pinimg.com/originals/f8/b6/9e/f8b69e6156999b84137f3f0a23701b75.jpg" width="300" height="240">

  <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:


查看完整回答
反对 回复 2022-12-09
  • 2 回答
  • 0 关注
  • 76 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号