js的原生代码放大镜实例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<meta http-equiv="X-UA-Compatible" content="ie=edge"/>
<title>Document</title>
<style>
#small{width: 300px; height: 560px; position: absolute; left: 100px; top: 100px}
#small img{width: 100%; height: 100%}
#mark{display: none;width: 200px; height: 200px; background-color: white; opacity: 0.5; filter: alpha(opacity=50); position: absolute; left: 0px; top: 0px}
#big{display: none;width: 400px; height: 400px; border: 1px solid black; left: 500px; top: 100px; position: absolute; overflow: hidden;}
#big img{width: 600px; height: 1120px; position: absolute; left: 0px; top: 0px}
</style>
<script>
window.onload = function(){
var oSmall = document.getElementById("small");
var oBig = document.getElementById("big");
var oMark = document.getElementById("mark");
var oBigImg = oBig.getElementsByTagName("img")[0];
oSmall.onmouseover = function(){
oMark.style.display = 'block';
oBig.style.display = 'block';
}
oSmall.onmouseout = function(){
oMark.style.display = 'none';
oBig.style.display = "none";
}
oSmall.onmousemove = function(ev){
var e = ev || window.event;
var l = e.clientX - oSmall.offsetLeft - 100;
var t = e.clientY - oSmall.offsetTop - 100;
if(l <= 0){
l = 0;
}
if(l >= 100){
l = 100;
}
if(t <= 0){
t = 0;
}
if(t >= 360){
t = 360;
}
oMark.style.left = l + 'px';
oMark.style.top = t + 'px';
oBigImg.style.left = l * -2 + 'px';
oBigImg.style.top = t * -2 + 'px';
}
}
</script>
</head>
<body>
<div id = 'small'>
<img src="timg.jpeg" alt=""/>
<div id = 'mark'></div>
</div>
<div id = 'big'>
<img src="timg.jpeg" alt=""/>
</div>
</body>
</html>
jQuery版本的放大镜
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<meta http-equiv="X-UA-Compatible" content="ie=edge"/>
<title>Document</title>
<style>
#small{width: 250px; height: 365px; border: 1px solid black; position: absolute; left: 100px; top: 100px}
#small img{width: 100%; height: 100%;}
#big img{width: 1000px; height: 1460px; position: absolute}
#big{display:none; overflow:hidden;width: 400px; height: 400px; border: 1px solid black; position: absolute; left: 400px; top: 100px}
#mark{display:none;width: 100px; height: 100px;background-color: white; opacity: 0.5; filter: alpha(opacity=50); position: absolute; left:0; top: 0}
</style>
<script src = "jquery-1.10.1.min.js"></script>
<script>
$(function(){
$("#small").mouseenter(function(){
$("#mark ,#big").show();
}).mouseleave(function(){
$("#mark ,#big").hide();
}).mousemove(function(ev){
var l = ev.pageX - $(this).offset().left - 50;
var t = ev.pageY - $(this).offset().top - 50;
if(l <= 0){
l = 0;
}
if(l >= 150){
l = 150;
}
if(t <= 0){
t = 0;
}
if(t >= 265){
t = 265;
}
$("#mark").css({
left: l,
top: t
});
$("#big img").css({
left: -4 * l,
top: -4 * t
})
})
})
</script>
</head>
<body>
<div id="small">
<img src="1.jpg" alt=""/>
<div id="mark"></div>
</div>
<div id="big"><img src="1.jpg" alt=""/></div>
</body>
</html>