jq放大镜

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>jQuery放大镜/title>
<script type="text/javascript" src="jquery-1.12.4.js"></script>
<style>
 * {
	margin:0;
	padding:0;
}
.small_box {
	width:250px;
	height:250px;
	margin-left:10px;
	margin-top:10px;
	position:relative;
}
.small_box img {
	width:250px;
	height:250px;
}
.small_box .mask {
	position:absolute;
	width:100%;
	height:100%;
	background:rgba(0,0,0,0.5);
	opacity:0;
	z-index:2;
	cursor:move;
}
.small_box .float_layer {
	position:absolute;
	width:100px;
	height:100px;
	background:rgba(0,0,0,0.5);
	display:none;
}
.big_box {
	position:absolute;
	left:200px;
	top:10px;
	width:250px;
	height:250px;
	overflow:hidden;
	display:none;
}
.big_box img {
	position:absolute;
}
</style>
</head>
<body>
<div class="small_box">
    <span class="mask"></span>
    <span class="float_layer"></span>
    <img src="image/01283a598c19b90000002129618e9d.jpg@1280w_1l_2o_100sh.png">
</div>
<div class="big_box">
    <img src="image/01283a598c19b90000002129618e9d.jpg@1280w_1l_2o_100sh.png">
</div>

<script>
$(".mask").mouseover(function() {//鼠标移入事件,让放大镜和放大区显示!
    $(".float_layer").show()
    $(".big_box").show()
})
$(".mask").mouseout(function() {//鼠标移出后,放大镜和放大区隐藏
    $(".float_layer").hide()
    $(".big_box").hide()
})



$(".mask").mousemove(function(e) {
    //获得鼠标移上去 mask盒子的实际移动距离  鼠标横纵坐标 - 盒子到坐上边界距离 - 盒子的实际宽高距离的二分之一
    var l = e.pageX - $(".small_box").offset().left - ($(".float_layer").width() / 2)
    var t = e.pageY - $(".small_box").offset().left - ($(".float_layer").height() / 2)
    //防止小盒子移出边界
    if (l < 0) {
        l = 0
    }
    if (l > $(this).width() - $(".float_layer").width()) {
        l = $(this).width() - $(".float_layer").width()
    }
    if (t < 0) {
        t = 0
    }
    if (t > $(this).height() - $(".float_layer").height()) {
        t = $(this).height() - $(".float_layer").height()
    }

    $(".float_layer").css({
        "left": l,
        "top": t
    })
    //求出实际移动距离/小盒子最大移动距离的比例值
    var pX = l / ($(".mask").width() - $(".float_layer").width())
    var pY = t / ($(".mask").height() - $(".float_layer").height())
    $(".big_box img").css({
        //获得右边大盒子与大图片的差值 * 比例 达到放大镜效果
        "left": -pX * ($(".big_box img").width() - $(".big_box").width()),
        "top": -pY * ($(".big_box img").height() - $(".big_box").height())
    })



})
</script>

</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值