Home  >  Article  >  Web Front-end  >  Native js code to achieve image magnification effect

Native js code to achieve image magnification effect

高洛峰
高洛峰Original
2016-12-08 13:56:051005browse

Today I will share with you a picture magnifier effect I wrote in js. I made two types of magnification effects. In fact, their principles are similar. They both use two pictures to set the corresponding sizes for the two pictures. Finally, it is displayed in different positions to achieve the magnification effect.

The first one is that I imitated the magnifying glass effect on Taobao shopping page. When the mouse moves over the product picture, a rectangular area will appear on the picture, and this area is the area you want to zoom in, and then the right side of the product picture An enlarged image of the product will appear. This magnification method only requires you to calculate the magnification ratio, and then achieve the corresponding magnification effect by modifying the scrollLeft and scrollTop values ​​of the magnification area.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>图片放大器</title>
<style media="screen">
*{
margin: 0;
padding: 0;
}
/*可视区域的父级标签*/
.wrap{
width: 400px;
height:auto;
border: 1px solid black;
display: inline-block;
position: absolute;
left: 0;
top: 0;
}
.wrap>img{
width: 100%;
height: auto;
}
/*锁定放大的矩形区域*/
.box{
border: 1px solid black;
width: 100px;
height: 100px;
background: rgba(0, 0, 0, 0.5);
opacity: 0.8;
position: absolute;
cursor: pointer;
display: none;
}
/*要显示放大图片的父级*/
.main{
width: 700px;
height: 700px;
margin-left: 420px;
overflow:hidden;
display:none;
position: absolute;
top: 0;
}
.main>img{
width:2800px;
height:auto;
}
</style>
</head>
<body>
<div class="wrap">
<img src="dog.jpg" alt="" />
<div class="box"></div>
</div>
<div class="main">
<img src="dog.jpg"alt="" />
</div>
<script type="text/javascript">
//获取四个对象
var wrap=document.querySelector(&#39;.wrap&#39;);
var box=document.querySelector(&#39;.box&#39;);
var main=document.querySelector(&#39;.main&#39;);
var mainImg=document.querySelector(&#39;.main img&#39;);
//添加移动事件
wrap.onmousemove=function(){
//鼠标移入可视图片后出现 锁定放大区域及放大图片
box.style.display=&#39;block&#39;;
main.style.display=&#39;block&#39;;
var event=window.event || event;
//获取鼠标距离可视区域边缘的偏移量
var disx=event.clientX-box.offsetWidth/2;
var disy=event.clientY-box.offsetHeight/2;
//矩形区域的最大可移动范围
var distanceMaxX=wrap.offsetWidth-box.offsetWidth;
var distanceMaxY=wrap.offsetHeight-box.offsetHeight;
// 判断让锁定放大的矩形区域不能出框
if (disx<=0) {
disx=0;
}
if (disy<=0) {
disy=0;
}
if(disx>=distanceMaxX) {
disx=distanceMaxX;
}
if(disy>=distanceMaxY) {
disy=distanceMaxY;
}
box.style.left=disx+&#39;px&#39;;
box.style.top=disy+&#39;px&#39;;
 
//获取放大比例
var scalex=box.offsetLeft/distanceMaxX;
var scaley=box.offsetTop/distanceMaxY;
main.scrollLeft=(mainImg.clientWidth-main.clientWidth)*scalex;
main.scrollTop=(mainImg.clientHeight-main.clientHeight)*scaley;
}
 
//添加移出事件
wrap.onmouseout=function(){
box.style.display=&#39;none&#39;;
main.style.display=&#39;none&#39;;
}
</script>
</body>
</html>

Effect preview:

Native js code to achieve image magnification effect

The second method is to directly enlarge on the original image, like a magnifying glass on it. This is an extension of the first method. There is no difference between the previous methods. Finally, there is no need to place a visible area for it. The enlarged image is displayed directly on the area you originally set to enlarge. When you modify the left and top values ​​​​of the enlarged area, the corresponding left and top values ​​​​of the image are automatically modified at the same time to achieve a synchronous amplification effect.

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>放大镜</title>
<style type="text/css">
.main{
width: 500px;
height: 570px;
border: 2px solid black;
position: relative;
/*overflow: hidden;*/
}
#img1{
width: 100%;
height: 100%;
}
.box{
width: 200px;
height: 200px;
border-radius: 200px;
/*border: 1px solid black;*/
display: none;
position: absolute;
overflow: hidden;
cursor:move;
}
//放大图片 给绝对定位让移动时它也能跟着移动
实现和我们锁定的区域同步
#img2{
width: 1200px;
height: 1400px;
position: absolute;
/*left: 0;
top: 0;*/
/*display: none;*/
}
</style>
</head>
<body>
<div class="main">
<img  id="img1" src="dog.jpg"/ alt="Native js code to achieve image magnification effect" >
<div class="box">
<img  id="img2" src="dog.jpg"/ alt="Native js code to achieve image magnification effect" >
</div>
</div>
</body>
</html>
<script type="text/javascript">
var main=document.querySelector(&#39;.main&#39;);
var box=document.querySelector(&#39;.box&#39;);
var boximg=document.querySelector(&#39;#img2&#39;);
//添加鼠标移动事件
main.addEventListener(&#39;mousemove&#39;,move,false);
function move(){
//显示放大的圆形区域
box.style.display=&#39;block&#39;;
var event=window.event||event;
//获取鼠标距离可视区域边缘的偏移量
var disx=event.clientX-box.offsetWidth/2;
var disy=event.clientY-box.offsetHeight/2;
var dismax=main.offsetWidth-box.offsetWidth;
var dismay=main.offsetHeight-box.offsetHeight;
//矩形区域的最大可移动范围
if (disx<=0) {
disx=0;
}
if (disx>=dismax) {
disx=dismax-2;
}
if(disy<=0){
disy=0;
}
if(disy>=dismay){
disy=dismay-2;
}
//当你移动的时候修改圆形区域的left以及 top值。
box.style.left=disx+&#39;px&#39;;
box.style.top=disy+&#39;px&#39;;
// var scalx=main.offsetWidth/box.offsetWidth
// var scaly=main.offsetHeight/box.offsetHeight;
//同理当你移动时放大的图片它的图片也要修改left和top值
boximg.style.left=-event.clientX*2+&#39;px&#39;;
boximg.style.top=-event.clientY*2+&#39;px&#39;;
// box.scrollLeft=(boximg.offsetWidth-box.offsetWidth);
// box.scrollTop=(boximg.offsetHeight-box.offsetHeight);
}
// 添加鼠标移出事件
main.addEventListener(&#39;mouseout&#39;,out,false);
function out(){
box.style.display=&#39;none&#39;;
}
</script>

Effect preview:

Native js code to achieve image magnification effect

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