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

Json string + Cookie + localstorage in JS

Table of contents 1.Json string 1.1Json Syntax 1....

Postman automated interface testing practice

Table of contents Background Description Creating...

Detailed explanation of cocoscreater prefab

Table of contents Prefab How to create a prefab T...

Summary of practical skills commonly used in Vue projects

Table of contents Preface 1. Use $attrs and $list...

Detailed tutorial on MySQL installation and configuration

Table of contents Installation-free version of My...

Native js imitates mobile phone pull-down refresh

This article shares the specific code of js imita...

Document Object Model (DOM) in JavaScript

Table of contents 1. What is DOM 2. Select elemen...

How to install phabricator using Docker

I am using the Ubuntu 16.04 system here. Installa...

Detailed explanation of how to install PHP curl extension under Linux

This article describes how to install the PHP cur...

Detailed explanation of JS array methods

Table of contents 1. The original array will be m...

HTML uses regular expressions to test table examples

Here is an example code for using regular express...

js Promise concurrent control method

Table of contents question background Idea & ...

Control the vertical center of the text in the HTML text box through CSS

When the height attribute of Text is defined, the ...