search
HomeWeb Front-endJS TutorialJavaScript Taobao main image magnifying glass function

If a worker wants to do his job well, he must first sharpen his tools. To achieve a certain effect, we must first understand its principles.
The function of the magnifying glass is to obtain the position of the mouse in the small image, and then calculate the part of the large image that needs to be displayed based on the size ratio of the large and small images, and then use positioning to make the part to be displayed in the large image appear in the right border.
Then look at the code. It will be easier to understand based on the code.

html part

<!DOCTYPE html> 
<html lang="en"> 
<head> 
 <meta charset="UTF-8"> 
 <title>放大镜效果</title> 
 <link rel="stylesheet" href="magnifier.css"> 
</head> 
<body> 
 <div id="wrapper"> 
 <!--小图-->
  <div id="img_min"> 
  <!--图片-->
  <img src="/static/imghwm/default1.png"  data-src="test.jpg"  class="lazy"   alt="min"> 
  <!--跟随鼠标的白块-->
  <p id="mousebg"></p> 
  </div> 
  <!--大图-->
  <div id="img_max"><img src="/static/imghwm/default1.png"  data-src="test.jpg"  class="lazy"  id="img2_img"  alt="max"></div> 
 </div> 
 <script type="text/javascript" src="magnifier.js"></script> 
</body> 
</html>

css part

*{ 
 margin: 0; 
 padding: 0; 
} 
div{ 
 position: relative; 
} 
div>div{ 
 width: 300px; 
 height: 300px; 
 float: left; 
 margin: 100px; 
 overflow: hidden; 
} 
#img_min>img{ 
 /*display: block;*/
 width: 300px; 
} 
#img_max{ 
 display: none; 
   
} 
#img_max>img{ 
 position: absolute; 
 top: 0; 
 left: 0; 
 display: block; 
 width: 1500px; 
} 
#mousebg{ 
 display: none; 
 position: absolute; 
 width: 60px; 
 height: 60px; 
 background-color: rgba(255,255,255,.7); 
 top: 0; 
 left: 0; 
}

The most important javascript part

window.onload = function () { 
 var img1 = document.getElementById(&#39;img_min&#39;);//小图盒子 
 var img2 = document.getElementById(&#39;img_max&#39;);//大图盒子 
 var img2_img = document.getElementById(&#39;img2_img&#39;);//大图图片 
 var wrap = document.getElementById(&#39;wrapper&#39;); 
 var mousebg = document.getElementById(&#39;mousebg&#39;);//鼠标白块 
 var mul = 5; 
 //当某一个模块dispaly:none的时候不能使用offsetWidth获取它的宽高 
 img1.onmouseover = function () { 
  //鼠标进入 
  img2.style.display = &#39;block&#39;; 
  mousebg.style.display = &#39;block&#39;; 
    
 } 
 img1.onmouseout = function () { 
  //鼠标离开 
  img2.style.display = &#39;none&#39;; 
  mousebg.style.display = &#39;none&#39;; 
 } 
 img1.onmousemove = function (event) { 
  var _event = event||window.event;//兼容性处理 
  var mouseX = _event.clientX - wrap.offsetLeft - img1.offsetLeft; 
  //计算鼠标相对与小图的位置 
  var mouseY = _event.clientY - wrap.offsetTop - img1.offsetTop; 
  
  //特殊情况处理,分别靠近四条边的时候 
  if(mouseX<mousebg.offsetWidth/2){ 
   mouseX = mousebg.offsetWidth/2; 
  } 
  if(mouseX>img1.offsetWidth-mousebg.offsetWidth/2){ 
   mouseX = img1.offsetWidth-mousebg.offsetWidth/2; 
  } 
  if(mouseY<mousebg.offsetHeight/2){ 
   mouseY = mousebg.offsetHeight/2; 
  } 
  if(mouseY>img1.offsetHeight-mousebg.offsetHeight/2){ 
   mouseY = img1.offsetHeight-mousebg.offsetHeight/2; 
  } 
  //计算大图的显示范围 
  img2_img.style.left = -mul*mouseX+img2.offsetWidth/2+"px"; 
  img2_img.style.top = -mul*mouseY+img2.offsetHeight/2+"px"; 
  //使鼠标在白块的中间 
  mousebg.style.left = mouseX-mousebg.offsetWidth/2+"px"; 
  mousebg.style.top = mouseY-mousebg.offsetHeight/2+"px"; 
    
 } 
}

If you understand it after reading the code and comments, use Li Yunlong's words to say: "Oh, you kid tnd is really a genius." Then you can quickly browse through the following analysis part and it will be OK.

Analysis part:

The html and css parts are simple layout codes and will not be explained any more. The js part code is also relatively simple. Let’s directly explain the code of the mouse movement event part.
First, use a picture to explain the principle of obtaining the position of the mouse relative to the small picture:

JavaScript Taobao main image magnifying glass function

You can see that through the operation in the code, the value we obtain is the value of the mouse relative to the upper left corner of img1.
After understanding this step, we can actually say that half of our work is done.
Then, we skip the processing of special cases and directly perform the basic operation of positioning the image on the right.
Because the offsetWidth, offsetHeight, style.width, and style.height properties are used. The ranges of style.width, style.height and offsetWidth and offsetHeight are the same. I will describe the other differences in detail in another blog. Let’s first use a picture to understand these attributes, and compare them with the above attributes (pictures are from the Internet, deleted)

JavaScript Taobao main image magnifying glass function

Then we explain the code:

Use of pictures in the big picture frame on the right style.left is positioned in the large picture frame. The negative sign is because the movement direction of our mouse is exactly opposite to the movement direction of the picture in our large picture frame. mul is calculated based on the size of the large picture and the small picture. Proportion, what is calculated by -mul*mouseX is actually the relative position of the picture in the large frame, but at this time you will find that the position of your mouse on the right is in the upper left corner of the frame, so we need to add an img2 .offsetWidth/2 to center the image. Similarly, we can perform the same processing on the ordinate.

//计算大图的显示范围
  img2_img.style.left = -mul*mouseX+img2.offsetWidth/2+"px";
  img2_img.style.top = -mul*mouseY+img2.offsetHeight/2+"px";

Now we are going to deal with special situations. When you do the previous step, you will find that when the mouse moves to the edge, the small white block of the mouse sometimes runs out of the scope of the picture. So we have to process it and limit it to the scope of the picture. Because the mouse is in the middle of the white transparent block, we just limit the mouse to a position that is half the length/width of the white block above, below, left, and right of the picture border. .

//特殊情况处理,分别靠近四条边的时候
  if(mouseX<mousebg.offsetWidth/2){
   mouseX = mousebg.offsetWidth/2;
  }
  if(mouseX>img1.offsetWidth-mousebg.offsetWidth/2){
   mouseX = img1.offsetWidth-mousebg.offsetWidth/2;
  }
  if(mouseY<mousebg.offsetHeight/2){
   mouseY = mousebg.offsetHeight/2;
  }
  if(mouseY>img1.offsetHeight-mousebg.offsetHeight/2){
   mouseY = img1.offsetHeight-mousebg.offsetHeight/2;
  }

When the distance to the left is less than half the width, we make mouseX equal to half the width, so that the white block will not continue to move. The same applies to the other three directions.
After completing this step, our effects are complete.
ps: Abstract places can be understood by drawing pictures.


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
es6数组怎么去掉重复并且重新排序es6数组怎么去掉重复并且重新排序May 05, 2022 pm 07:08 PM

去掉重复并排序的方法:1、使用“Array.from(new Set(arr))”或者“[…new Set(arr)]”语句,去掉数组中的重复元素,返回去重后的新数组;2、利用sort()对去重数组进行排序,语法“去重数组.sort()”。

JavaScript的Symbol类型、隐藏属性及全局注册表详解JavaScript的Symbol类型、隐藏属性及全局注册表详解Jun 02, 2022 am 11:50 AM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于Symbol类型、隐藏属性及全局注册表的相关问题,包括了Symbol类型的描述、Symbol不会隐式转字符串等问题,下面一起来看一下,希望对大家有帮助。

原来利用纯CSS也能实现文字轮播与图片轮播!原来利用纯CSS也能实现文字轮播与图片轮播!Jun 10, 2022 pm 01:00 PM

怎么制作文字轮播与图片轮播?大家第一想到的是不是利用js,其实利用纯CSS也能实现文字轮播与图片轮播,下面来看看实现方法,希望对大家有所帮助!

JavaScript对象的构造函数和new操作符(实例详解)JavaScript对象的构造函数和new操作符(实例详解)May 10, 2022 pm 06:16 PM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于对象的构造函数和new操作符,构造函数是所有对象的成员方法中,最早被调用的那个,下面一起来看一下吧,希望对大家有帮助。

JavaScript面向对象详细解析之属性描述符JavaScript面向对象详细解析之属性描述符May 27, 2022 pm 05:29 PM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于面向对象的相关问题,包括了属性描述符、数据描述符、存取描述符等等内容,下面一起来看一下,希望对大家有帮助。

javascript怎么移除元素点击事件javascript怎么移除元素点击事件Apr 11, 2022 pm 04:51 PM

方法:1、利用“点击元素对象.unbind("click");”方法,该方法可以移除被选元素的事件处理程序;2、利用“点击元素对象.off("click");”方法,该方法可以移除通过on()方法添加的事件处理程序。

整理总结JavaScript常见的BOM操作整理总结JavaScript常见的BOM操作Jun 01, 2022 am 11:43 AM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于BOM操作的相关问题,包括了window对象的常见事件、JavaScript执行机制等等相关内容,下面一起来看一下,希望对大家有帮助。

foreach是es6里的吗foreach是es6里的吗May 05, 2022 pm 05:59 PM

foreach不是es6的方法。foreach是es3中一个遍历数组的方法,可以调用数组的每个元素,并将元素传给回调函数进行处理,语法“array.forEach(function(当前元素,索引,数组){...})”;该方法不处理空数组。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment