Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the effect of Commercial magnifying Glass by JavaScript

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article is about how JavaScript achieves the effect of commodity magnifying glass. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

The details are as follows

HTML+CSS section:

.small {position: relative; width: 400px; height: 450px; border: 1px solid # ccc;} .small img {width: 100%; height: 100%;} .small .mask {position: absolute; left: 0; top: 0 Width: 300px; height: 300px; background-color: rgba (0,255,0, .2);} .big {position: absolute; left: 450px; top: 8px; width: 550px; height: 550px; border: 1px solid # ccc; overflow: hidden Display: none;} .big img {position: absolute; left: 0; top: 0;}

JS section:

Var small=document.querySelector ('.small'); var mask=document.querySelector ('.mask'); var big=document.querySelector ('.big'); var bigImg=document.querySelector ('.big > img'); / / get the width and height of the large image var bigWidth=bigImg.offsetWidth; var bigHeight=bigImg.offsetHeight; / / function move (e) {var x=e.pageX-this.offsetLeft Var y=e.pageY-this.offsetTop; console.log (x maskWidth+'px'; mask.style.top y); / / move mask position var maskHeight = mask.offsetHeight/2; var maskWidth = mask.offsetWidth/2; mask.style.left = x-maskWidth+'px'; mask.style.top = y-maskHeight+'px' / / limit the movement range of the mask / / console.log ('mask.offsetTop',mask.offsetTop); / / console.log (' mask.offsetLeft',mask.offsetLeft); var maxLeft=small.offsetWidth-mask.offsetWidth; if (mask.offsetTop small.offsetWidth-mask.offsetWidth) {mask.style.left = maxLeft+'px' } if (mask.offsetLeft small.offsetHeight-mask.offsetHeight) {mask.style.top = small.offsetHeight-mask.offsetHeight + 'px' } / / bigImg big picture big big box big picture move / / (bigImg.offsetWidth-big.offsetWidth) / (samll.offsetWidtth-mask.offsetWidth) / / the maximum moving distance of the big image =-the moving distance of the mask * the maximum moving distance of the big image / the maximum moving distance of the mask bigImg.style.left =-mask.offsetLeft* (bigImg.offsetWidth-big.offsetWidth) / (small.offsetWidth-mask.offsetWidth) + "px" BigImg.style.top =-mask.offsetTop* (bigImg.offsetHeight-big.offsetHeight) / (small.offsetHeight-mask.offsetHeight) + "px";} / / move mask small.addEventListener ('mousemove',move) on the small image; small.addEventListener (' mouseover',function () {big.style.display='block'; mask.style.display='block') }) small.addEventListener ('mouseout',function () {big.style.display='none'; mask.style.display='none';})

Demonstration of the effect:

Thank you for reading! This is the end of the article on "how to achieve the effect of commodity magnifying glass in JavaScript". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 254

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report