js的小效果-图片放大镜效果

Posted HuangRong

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了js的小效果-图片放大镜效果相关的知识,希望对你有一定的参考价值。

<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
*{margin: 0;padding:0;}
#small{
width:350px;
height:350px;
border:1px solid #000;
position:relative;
float: left;
margin-right: 20px;

}
#big img{
position: absolute;
}
#mask{
display: block;
width:50px;
height:50px;
opacity:0.5;
background: red;
display: none;
position: absolute;
left:0;
top:0;
}
#big{
width:350px;
height:350px;
float:left;
border:1px solid #000;
overflow: hidden;
display: none;
position: relative;
}
</style>
<script>
window.onload=function(){
var oSmall=document.getElementById(‘small‘);
var oMask=document.getElementById(‘mask‘);
var oBig=document.getElementById(‘big‘);
var oImg=document.getElementById(‘bigimg‘);
oSmall.onmouseover=function(){
oMask.style.display=‘block‘;
oBig.style.display=‘block‘;
};
oSmall.onmouseout=function(){
oMask.style.display=‘none‘;
oBig.style.display=‘none‘;
};
oSmall.onmousemove = function(ev){
var oEvent = ev || event;
var l = oEvent.clientX - oMask.offsetWidth/2;
var t = oEvent.clientY - oMask.offsetHeight/2;
if(l < 0){
l= 0;
}else if(l > oSmall.offsetWidth - oMask.offsetWidth){
l = oSmall.offsetWidth - oMask.offsetWidth;
}
if(t < 0){
t = 0;
}else if(t >oSmall.offsetHeight - oMask.offsetHeight){
t = oSmall.offsetHeight - oMask.offsetHeight;
}
oMask.style.left = l + ‘px‘;
oMask.style.top = t + ‘px‘;
oImg.style.left=l*(oBig.offsetWidth-oImg.offsetWidth)/
(oSmall.offsetWidth-oMask.offsetWidth)+‘px‘;
oImg.style.top=t*(oBig.offsetHeight-oImg.offsetHeight)/
(oSmall.offsetHeight-oMask.offsetHeight)+‘px‘;

}
oSmall.onmousemove=function(ev){
var oEvent=ev||event;
var l=oEvent.clientX-oMask.offsetWidth/2;
var t=oEvent.clientY-oMask.offsetHeight/2;
if(l<0){
l=0;
}else if(l > oSmall.offsetWidth - oMask.offsetWidth){
l=oSmall.offsetWidth-oMask.offsetWidth;
}
if(t<0){
t=0;
}else if(t>oSmall.offsetHeight-oMask.offsetHeight){
t=oSmall.offsetHeight-oMask.offsetHeight;
}
oMask.style.left=l+‘px‘;
oMask.style.top=t+‘px‘;
oImg.style.left=l*(oBig.offsetWidth-oImg.offsetWidth)/
(oSmall.offsetWidth-oMask.offsetWidth)+‘px‘;
oImg.style.top=t*(oBig.offsetHeight-oImg.offsetHeight)/
(oSmall.offsetHeight-oMask.offsetHeight)+‘px‘;
}
}
</script>
</head>
<body>
<div id="small">
<img src="img/s.jpg" />
<span id="mask"></span>
</div>
<div id="big">
<img src="img/b.jpg" id="bigimg"/>
</div>

</body>
</html>









































































































以上是关于js的小效果-图片放大镜效果的主要内容,如果未能解决你的问题,请参考以下文章

js 实现 放大镜 效果 (图片放大)

原生JS实现放大镜效果

JS实现淘宝商品图片放大效果(放大镜)

JS实现淘宝商品图片放大效果(放大镜)

JS实现淘宝商品图片放大效果(放大镜)

JS实现淘宝商品图片放大效果(放大镜)