admin管理员组文章数量:1401919
I added a magnifying glass for images in my webpage using the following instruction: .asp
The magnified box stays on the image even if the mouse is not over it. How can I revise this in a way that the magnifying glass appears only when the mouse is over it?
Thanks,
I added a magnifying glass for images in my webpage using the following instruction: https://www.w3schools./howto/howto_js_image_magnifier_glass.asp
The magnified box stays on the image even if the mouse is not over it. How can I revise this in a way that the magnifying glass appears only when the mouse is over it?
Thanks,
Share Improve this question asked Feb 13, 2018 at 0:22 MohammadMohammad 1131 silver badge8 bronze badges3 Answers
Reset to default 2You can do this all in CSS using hover, no need for javascript copy the style sheet from below.
<style>
* {box-sizing: border-box;}
.img-magnifier-container {
position:relative;
cursor:none;
}
.img-magnifier-container:hover .img-magnifier-glass {
position: absolute;
border: 3px solid #000;
border-radius: 50%;
cursor: none;
/*Set the size of the magnifier glass:*/
width: 100px;
height: 100px;
display:block;
}
.img-magnifier-glass {
display:none;
}
</style>
In HTML, there is an event called onmouseleave. You could make the element call a javascript function when your mouse leaves the image.
Here's the link for more information: https://www.w3schools./jsref/event_onmouseleave.asp
Here's the basics of it:
onmouseleave="functionName()"
You need to add a onmouseout event to the magnifying glass div and an id to identify it, like this:
glass = document.createElement("DIV");
glass.id="glassId";
glass.setAttribute("onmouseout", "hide()");
Then add a show and hide function like this:
function hide(){
glass = document.getElementById("glassId").style.display = "none";
}
function show(){
glass = document.getElementById("glassId").style.display = "block";
}
Finally add the mouseover event to the image itself:
onmouseover="show()"
This will then hide the glass when the mouse leaves the glass (not the image) and show it when the mouse goes back into the image.
本文标签: javascriptMouse over the image with magnifying glass in htmlStack Overflow
版权声明:本文标题:javascript - Mouse over the image with magnifying glass in html - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744335884a2601187.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论