Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use js Simulation to realize the magnifying effect of JD.com 's details Page

Shulou Source: shulou.com Published: 2022-06-02 14:47:45 10月03日 Update

This article is about how to use js simulation to achieve JD.com details page picture magnification. 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

Effect:

Html:

Css:

Mask {display: none; position: absolute; width: 300px; height: 300px; top: 0; left: 0; background: # FEFE4F; opacity: .5; border: 1px solid # ccc; cursor: move;}. Big {display: none; position: absolute; width: 550px; height: 550px; top: 0; left: 410px; z-index: 999; border: 1px solid # ccc; overflow: hidden;}. Big img {position: absolute; left: 0 Top: 0;}

Js (key points):

Window.addEventListener ('load',function () {var preview_img = document.querySelector (' .mask _ img'); var mask = this.document.querySelector ('.mask'); var big = this.document.querySelector ('.big'); var bigImg = this.document.querySelector ('.bigImg'); / / mouse over preview_img.addEventListener ('mouseover',function () {mask.style.display =' block'') Big.style.display = 'block';}) / / move the mouse out of preview_img.addEventListener (' mouseout',function () {mask.style.display = 'none'; big.style.display =' none' }) / / when the mouse moves, preview_img.addEventListener ('mousemove',function (e) {/ / the coordinates of the mouse in the box var x = e.pageX-this.offsetLeft; var y = e.pageY-this.offsetTop; / / the distance to move the occlusion layer var maskX = x-mask.offsetWidth/2; var maskY = y-mask.offsetHeight/2 / / large image moving distance / / the maximum moving distance of the occlusion layer var maskMaxX = preview_img.offsetWidth-mask.offsetWidth; var maskMaxY = preview_img.offsetHeight-mask.offsetHeight; / / if the X coordinate is less than 0, leave it at 0 position if (maskX = maskMaxX) {maskX = maskMaxX } / / if the Y coordinate is less than 0, let it stay at 0 position if (maskY = maskMaxY) {maskY = maskMaxY;} / / the occlusion layer moves mask.style.left = maskX + 'px'; mask.style.top = maskY +' px' / / maximum moving distance of large picture var bigMaxX = bigImg.offsetWidth-big.offsetWidth; var bigMaxY = bigImg.offsetHeight-big.offsetHeight; / / moving distance of large picture X Y / / moving distance of large picture = moving distance of occlusion layer * maximum moving distance of large picture / maximum moving distance of occlusion layer var bigX = maskX * bigMaxX / maskMaxX Var bigY = maskY * bigMaxY / maskMaxY; / / the big image and the small image (mouse movement) go in the opposite direction bigImg.style.left =-bigX + 'px'; bigImg.style.top =-bigY +' px';})}) Thank you for reading! On "how to use js simulation to achieve JD.com details page picture magnification effect" this article is shared here, 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 it!

Tags: Mobile picture mouse maximum effect content coordinates details JD.com location more article big picture good practical article direction time box see Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Microsoft Shulou Tech Info Apple NVidia