简体   繁体   English

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

[英]Mouse hover zoom image to be only shown when mouse hovered

I have made this mouse hover image zoom page.我做了这个鼠标 hover 图像缩放页面。 But i want the out put to be shown on the left side and only when someone hovers the mouse on it.但我希望输出显示在左侧,并且只有当有人将鼠标悬停在它上面时。 In this code the output window is continuously opened.在此代码中,output window 不断打开。 HERE IS MY CODE: CSS CODE:这是我的代码:CSS 代码:

* {box-sizing: border-box;}

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

.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;
  /*set the size of the result div:*/
  width: 300px;
  height: 300px;
}

JAVASCRIPT CODE: JAVASCRIPT 代码:

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

HTML: HTML:


<h1>Image Zoom</h1>

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

<div class="img-zoom-container">
  <img id="myimage" src="img_girl.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:
imageZoom("myimage", "myresult");

The code is working perfectly but the result should be on the right and it should be only shown when user hovers the mouse on it.代码运行良好,但结果应在右侧,并且仅当用户将鼠标悬停在其上时才应显示。 Pls help and fix the issue.请帮助并解决问题。 thank you in advance先感谢您

by default have its opacity: 0;默认情况下有它的不透明度:0; and on mouseenter make it visible by making opacity: 1;并在 mouseenter 上通过设置 opacity: 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:

Below I've modified your CSS.下面我修改了你的 CSS。 display: flex is what you are after. display: flex是你所追求的。

I then get your code to hide the result element and only show it on mouseover .然后我让您的代码隐藏result元素并仅在mouseover上显示它。 I then have it hide on mouseout .然后我让它隐藏在mouseout上。

You could tweak the CSS etc to match your style, but here's a snippet of what I've got for you;您可以调整 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>

If you want me to, I could work on making the lens result an absolute positioned and aligned so that you don't get the text at the bottom jumping.如果您希望我这样做,我可以努力使镜头结果绝对定位和对齐,这样您就不会在底部跳跃的文本。 Unless you know how to do all that - hopefully this is sufficient?除非你知道如何做这一切——希望这就足够了吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM