js realizes the magnifying glass function of shopping website

js realizes the magnifying glass function of shopping website

This article shares the specific code of js to realize the magnifying glass function of shopping website for your reference. The specific content is as follows

First look at the effect diagram:

First is the layout, a small frame on the left, including a mouse movement frame, and a magnification frame on the right.

<div class="box">
        <div class="small">
            <img src="small3.jpg" alt="">
            <div class="move"></div>
        </div>
        <div class="big">
            <img src="big3.jpg" alt="">
        </div>
</div>

Write some CSS

.small{
    position: relative;
    float: left;
    width: 450px;
    height: 450px;
    border:1px solid #000;
}
.small .move{
    position: absolute;
    top:0;
    width: 300px;
    height: 300px;
    background-color: rgba(0,0,0,0.4);
    cursor:move;
    display: none;
}
.big{
    position: relative;
    float: left;
    width: 540px;
    height: 540px;
    margin-left: 20px;
    overflow: hidden;
    border:1px solid #000;
    display: none;
}
.bigimg{
    position: absolute;
    top:0;
    left: 0;
}

js part:

var box=document.getElementsByClassName('box')[0],small=box.getElementsByClassName('small')[0],move=small.getElementsByClassName('move')[0],smallImg=small.getElementsByTagName('img')[0],big=box.getElementsByClassName('big')[0],bigImg=big.getElementsByTagName('img')[0];
    //First, get all the required elements small.onmouseover=function(){
        move.style.display='block';
        big.style.display="block";
    };
    small.onmouseout=function(){
        move.style.display='none';
        big.style.display="none";
    };
    small.onmousemove=function(e){
        e=e||window.event; //Compatibility considerations var x=e.clientX-smallImg.getBoundingClientRect().left-move.offsetWidth/2;
        var y=e.clientY-smallImg.getBoundingClientRect().top-move.offsetHeight/2;
        if(x<0){
            x=0;
        }
        if(x>smallImg.offsetWidth-move.offsetWidth){
            x=smallImg.offsetWidth-move.offsetWidth;
        }
        if(y<0){
            y=0;
        }
        if(y>smallImg.offsetHeight-move.offsetHeight){
            y=smallImg.offsetHeight-move.offsetHeight;
        }
        move.style.left=x+"px";
        move.style.top=y+"px";
        //Code to implement the left move block following the mouse movement var scale = bigImg.offsetWidth/smallImg.offsetWidth;
        //Enlarge according to the proportion bigImg.style.left='-'+x*scale+'px';
        //Because the image needs to be moved left and up, a negative sign must be added bigImg.style.top='-'+y*scale+'px';
    }

The magnifying glass effect is achieved!

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:
  • JS version of the picture magnifying glass effect
  • js to achieve a simple magnifying glass effect
  • js to achieve simple magnifying glass effects
  • js magnifying glass magnifying picture effect
  • A simple example of using js to achieve the effect of a magnifying glass
  • JavaScript image magnifying glass effect code [the code is relatively simple]
  • A magical Javascript image magnifier
  • JavaScript image magnifier (drag and drop, zoom effect)
  • Magnifying glass effect written in native js
  • JavaScript to achieve a simple magnifying glass effect

<<:  Analysis of MySQL multi-table joint query operation examples

>>:  How to configure Hexo and GitHub to bind a custom domain name under Windows 10

Recommend

Detailed explanation of linux nslookup command usage

[Who is nslookup?] 】 The nslookup command is a ve...

Three ways to share component logic in React

Without further ado, these three methods are: ren...

How to view the network routing table in Ubuntu

What are Routing and Routing Table in Linux? The ...

Methods and steps for deploying multiple war packages in Tomcat

1 Background JDK1.8-u181 and Tomcat8.5.53 were in...

Guide to using env in vue cli

Table of contents Preface Introduction-Official E...

How to prevent event bubbling in JavaScript

What we need to pay attention to is that the char...

CSS realizes div completely centered without setting height

Require The div under the body is vertically cent...

JavaScript canvas to achieve raindrop effects

This article example shares the specific code of ...

How to install openssh from source code in centos 7

Environment: CentOS 7.1.1503 Minimum Installation...

About the problem of writing plugins for mounting DOM in vue3

Compared with vue2, vue3 has an additional concep...

Three BOM objects in JavaScript

Table of contents 1. Location Object 1. URL 2. Pr...

React internationalization react-intl usage

How to achieve internationalization in React? The...

How to avoid garbled characters when importing external files (js/vbs/css)

In the page, external files such as js, css, etc. ...

How to use html2canvas to convert HTML code into images

Convert code to image using html2canvas is a very...

Research on the value of position attribute in CSS (summary)

The CSS position attribute specifies the element&...