js simulation to achieve the effect of enlarging the picture on the Jingdong details page

js simulation to achieve the effect of enlarging the picture on the Jingdong details page

This article shares the specific code of js to realize the enlargement of the picture on the Jingdong details page for your reference. The specific content is as follows

Effect:

html:

<div class="preview_img">
 <img src="upload/s3.png" alt="">
 <div class="mask"></div>
 <div class="big">
  <img src="upload/big.jpg" alt="" class="bigImg">
 </div>
</div>

css:

.preview_img {
 position: relative;
 height: 398px;
 border: 1px solid #ccc;
}
.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;
}
.bigimg {
 position: absolute;
 left: 0;
 top: 0;
}

js (emphasis):

window.addEventListener('load',function(){
    var preview_img = document.querySelector('.preview_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';
    })
    //Mouse out 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 the occlusion layer moves var maskX = x - mask.offsetWidth/2;
        var maskY = y -mask.offsetHeight/2;
        //Large image moving distance //Maximum moving distance of occlusion layer var maskMaxX = preview_img.offsetWidth - mask.offsetWidth;
        var maskMaxY = preview_img.offsetHeight - mask.offsetHeight;
        //If the X coordinate is less than 0, let it stay at 0 if (maskX <= 0) {
            maskX = 0;
        }else if (maskX >= maskMaxX) {
            maskX = maskMaxX;
        }
        //If the Y coordinate is less than 0, let it stay at 0 if (maskY <= 0) {
            maskY = 0;
        }else if (maskY >= maskMaxY) {
            maskY = maskMaxY;
        }
        //Occlusion layer movement mask.style.left = maskX + 'px';
        mask.style.top = maskY + 'px';
        
        //The maximum moving distance of the big picture var bigMaxX = bigImg.offsetWidth - big.offsetWidth;
        var bigMaxY = bigImg.offsetHeight - big.offsetHeight;
        //The moving distance of the large image XY
        // Moving distance of large image = Moving distance of occlusion layer * Maximum moving distance of large image / Maximum moving distance of occlusion layer var bigX = maskX * bigMaxX / maskMaxX;
        var bigY = maskY * bigMaxY / maskMaxY;
        //The big picture and the small picture (mouse movement) are in opposite directions bigImg.style.left = -bigX + 'px';
        bigImg.style.top = -bigY + 'px';
    })
})

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Two simple ways to zoom in and out of pictures in JS
  • js magnifying glass magnifying picture effect
  • js implements the method of clicking on the picture to enlarge the picture
  • js realizes the effect of clicking on the picture to pop up and enlarge it in the middle of the screen
  • JS realizes the picture zooming effect when the mouse moves to the thumbnail to display the large picture
  • js code for the image enlargement and floating effect after the mouse slides up
  • JavaScript image magnifying glass effect code [the code is relatively simple]
  • js to achieve image enlargement display effect
  • Mobile HTML5 uses photoswipe.js to imitate the zoom effect of WeChat Moments pictures
  • JS web page image viewer (compatible with IE, FF) can control the image zoom in and out and move

<<:  How to Apply for Web Design Jobs

>>:  How to set the style of ordered and unordered list items in CSS

Recommend

How to configure VMware multi-node environment

This tutorial uses CentOS 7 64-bit. Allocate 2GB ...

Summary of Mysql common benchmark commands

mysqlslap Common parameter description –auto-gene...

Docker runs operations with specified memory

as follows: -m, --memory Memory limit, the format...

Use nginx + secondary domain name + https support

Step 1: Add a secondary domain name to the Alibab...

Canonical enables Linux desktop apps with Flutter (recommended)

Google's goal with Flutter has always been to...

What to do after installing Ubuntu 20.04 (beginner's guide)

Ubuntu 20.04 has been released, bringing many new...

Two ways to implement HTML page click download file

1. Use the <a> tag to complete <a href=&...

Advertising skills in the Baidu Union environment (graphic tutorial)

Recently, students from the User Experience Team o...

Learn about JavaScript closure functions in one article

Table of contents Variable Scope The concept of c...

MySQL GROUP_CONCAT limitation solution

effect: The GROUP_CONCAT function can concatenate...

The most convenient way to build a Zookeeper server in history (recommended)

What is ZooKeeper ZooKeeper is a top-level projec...

How to set MySQL foreign keys for beginners

Table of contents The role of foreign keys mysql ...

How to redirect nginx directory path

If you want the path following the domain name to...

HTML form submission method case study

To summarize the form submission method: 1. Use t...

Detailed explanation of common Docker commands

1. Help Command 1. View the current Docker versio...