前端JS电商放大镜效果:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>-电商放大镜</title>
<style type="text/css"> *{
padding: ;
margin: ;
}
#left{
padding: ;
margin: ;
width: 400px;
height: 400px;
border: 2px solid blue;
background: url(http://chuantu.biz/t6/17/1503469475x2063891122.jpg) no-repeat;
float: left;
cursor: crosshair;
position: relative;
box-sizing: border-box;
}
#box{
width: 200px;
height: 200px;
background: white;
opacity: 0.6;
position: absolute;
top: ;
left: ;
display: none;
box-sizing: border-box;
}
#cover{
width: 400px;
height: 400px;
background: red;
position: absolute;
left: ;
top: ;
opacity: ;
box-sizing: border-box;
}
#right{
width: 400px;
height: 400px;
border: 2px solid black;
overflow: hidden;
position: relative;
display: none;
box-sizing: border-box;
}
#rpic{
position: absolute;
}
</style> <script type="text/javascript"> window.onload = function(){
var left = document.getElementById("left");
var right = document.getElementById("right");
var rpic = document.getElementById("rpic");
var box = document.getElementById("box");
var cover = document.getElementById("cover"); // 给左侧加鼠标移动事件
cover.onmousemove = function(){ //获得事件对象
var ev = window.event;
var mouse_left = ev.offsetX || ev.layerX;
var mouse_top = ev.offsetY || ev.layerY;
// document.title = mouse_left + '|' + mouse_top; //计算色块的位置
var box_left = mouse_left - ;
var box_top = mouse_top - ; // 判断是否超出
if (box_left < ) {
box_left = ;
}
if (box_left > ) {
box_left = ;
}
if (box_top < ) {
box_top = ;
}
if (box_top > ) {
box_top = ;
} // 让色块移动
box.style.left = box_left + 'px';
box.style.top = box_top + 'px'; //计算右侧图片位置
var rpic_left = box_left*-;
var rpic_top = box_top*-; // 让右侧移动
rpic.style.left = rpic_left + 'px';
rpic.style.top = rpic_top + 'px'; } //给左侧加鼠标移入事件
cover.onmouseover = function(){
// 让左侧色块和右侧隐藏
box.style.display = 'block';
right.style.display = 'block';
} // 给左侧加鼠标移出事件
cover.onmouseout = function(){
// 让左侧色块和右侧隐藏
box.style.display = 'none';
right.style.display = 'none';
}
} </script>
</head>
<body>
<div id="left">
<div id="box"></div>
<div id="cover"></div>
</div>
<div id="right">
<img src="http://chuantu.biz/t6/17/1503469419x2063891122.jpg" id="rpic">
</div>
</body>
</html>