效果:实现放大2倍的效果
注意:图片用一张小图,一张大图。
效果图:
代码实现:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
.box{
width: 400px;
height: 400px;
border:1px solid;
overflow: hidden;
position: relative;
left: 20px;
top:50px;
float: left;
box-sizing:border-box;
}
.move{
width: 200px;
height: 200px;
background:#000;
position: absolute;
left:0;
top:0;
opacity: 0.5;
}
.box img{
width: 400px;
height: 400px;
}
.bigbox{
height: 400px;
width: 400px;
border:1px solid;
position: relative;
left:50px;
top:50px;
overflow:hidden;
float: left;
box-sizing:border-box;
}
.bigbox img{
height: 800px;
width: 800px;
position: absolute;
top:0;
left:0;
}
</style>
</head>
<body>
<div class="box">
<img src="../images/middle.jpg">
<div class="move"></div>
</div>
<div class="bigbox">
<img src="../images/big.jpg" class="img2">
</div>
<script src="../lib/jquery/jquery-1.12.4.js"></script>
<script type="text/javascript">
$(function(){
$(".box").mouseenter(function(){
$(".move").show();
$(".bigbox").show();
$(document).mousemove(function(e){
e = e || event;
var _left = e.pageX-100-$(".box").offset().left,
_top = e.pageY-100-$(".box").offset().top;
if(_left<0){
_left =0;
}
if(_top<0){
_top =0;
}
if(_left>$(".box").outerWidth()-$(".move").outerWidth()){
_left =$(".box").outerWidth()-$(".move").outerWidth();
}
if(_top>$(".box").outerHeight()-$(".move").outerHeight()){
_top=$(".box").outerHeight()-$(".move").outerHeight();
}
$(".move").css({
left:_left+"px",
top:_top+"px"
});
$(".img2").css({
left:-2*_left+"px",
top:-2*_top+"px"
});
});
$(".box").mouseleave(function(){
$(document).off();
$(".move").hide();
$(".bigbox").hide();
});
});
});
</script>
</body>
</html>