简体   繁体   中英

How can I show my magnifying glass only when I hover over the image in CSS?

 // magnify hover 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}; } } /* Initiate Magnify Function with the id of the image, and the strength of the magnifier glass:*/ magnify("mag", 2);
 .img-magnifier-glass { position: absolute; z-index: 3; border: 3px solid #000; border-radius: 40%; cursor: none; /*Set the size of the magnifier glass:*/ width: 100px; height: 100px; }
 <table class="img-magnifier-container" width="115" border="0" cellpadding="0" cellspacing="0" style="display: inline-block"><tr><td width="115" style="text-align: center"><center><a href="https://www.imdb.com/title/tt3315342/" title="Logan 4K (2017)" target="_blank"><img class="cover" width="115" height="133" border="0" id="mag" class="pstrart" id="pstr" src="https://images.static-bluray.com/movies/covers/174423_front.jpg" style=""></a></center><center><input type="checkbox" name="movieboxes" value="Logan" style="width: 15px; height: 15px; margin: 0px;">

Hello,

How can I make the magnifier show only when I hover over the image? I have tried searching on this website and elsewhere, but I don't understand enough to make the solutions apply to my situation.

I copied this from W3School but they don't have a section where they explain that I can make it show when I hover over it.

I have modals on my sight and tried to replicate the hovering features of it but it doesn't seem to work. Thanks!

Simply toggle opacity on hover:

 // magnify hover 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}; } } /* Initiate Magnify Function with the id of the image, and the strength of the magnifier glass:*/ magnify("mag", 2);
 .img-magnifier-glass { position: absolute; z-index: 3; border: 3px solid #000; border-radius: 40%; cursor: none; /*Set the size of the magnifier glass:*/ width: 100px; height: 100px; opacity:0; pointer-events:none; } a:hover .img-magnifier-glass{ opacity:1; pointer-events:initial; }
 <table class="img-magnifier-container" width="115" border="0" cellpadding="0" cellspacing="0" style="display: inline-block"><tr><td width="115" style="text-align: center"><center><a href="https://www.imdb.com/title/tt3315342/" title="Logan 4K (2017)" target="_blank"><img class="cover" width="115" height="133" border="0" id="mag" class="pstrart" id="pstr" src="https://images.static-bluray.com/movies/covers/174423_front.jpg" style=""></a></center><center><input type="checkbox" name="movieboxes" value="Logan" style="width: 15px; height: 15px; margin: 0px;">

You can use javascript mouse event handlers . I modified your code snippet below.

What I did is,

.img-magnifier-glass {
  ...
  display:none; // hide the magnifier by default
}
// Show the magnifier when hover the container
$('.img-magnifier-container').mouseover(function(){
  $('.img-magnifier-glass').show();
});

// Hide the magnifier when leave the container
$('.img-magnifier-container').mouseout(function(){
  $('.img-magnifier-glass').hide();
});

 // magnify hover 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}; } } /* Initiate Magnify Function with the id of the image, and the strength of the magnifier glass:*/ magnify("mag", 2); $('.img-magnifier-container').mouseover(function(){ $('.img-magnifier-glass').show(); }); $('.img-magnifier-container').mouseout(function(){ $('.img-magnifier-glass').hide(); });
 .img-magnifier-glass { position: absolute; z-index: 3; border: 3px solid #000; border-radius: 40%; cursor: none; /*Set the size of the magnifier glass:*/ width: 100px; height: 100px; display:none; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <table class="img-magnifier-container" width="115" border="0" cellpadding="0" cellspacing="0" style="display: inline-block"><tr><td width="115" style="text-align: center"><center><a href="https://www.imdb.com/title/tt3315342/" title="Logan 4K (2017)" target="_blank"><img class="cover" width="115" height="133" border="0" id="mag" class="pstrart" id="pstr" src="https://images.static-bluray.com/movies/covers/174423_front.jpg" style=""></a></center><center><input type="checkbox" name="movieboxes" value="Logan" style="width: 15px; height: 15px; margin: 0px;">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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