<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js"></script>
<title>Document</title>
<style>
#master {
position: absolute;
}
#master>img {
width: 300px;
}
#magnify>img {
width: 1200px;
position: absolute;
left: 400px;
}
#magnify {
position: absolute;
width: 400px;
height: 400px;
left: 400px;
border: 2px solid black;
overflow: hidden;
display: none;
}
.mask {
width: 100px;
height: 100px;
background: #fff;
opacity: 0.7;
position: absolute;
display: none;
}
</style>
</head>
<body>
<div id="master">
<div class="mask"></div>
<img src="./image/0cb9bce9391be952629593168f49587a.jpg" alt="">
</div>
<div id="magnify">
<img src="./image/0cb9bce9391be952629593168f49587a.jpg" alt="">
</div>
<script>
$(function () {
$('#master').mouseenter(function () {
$('.mask,#magnify').show()
}).mouseleave(function () {
$('.mask,#magnify').hide()
}).mousemove(function (e) {
let l = e.pageX - $(this).offset().left - 50
let t = e.pageY - $(this).offset().top - 50
if (l <= 0) { l = 0 }
if (t <= 0) { t = 0 }
if (l >= 200) { l = 200 }
if (t >= 350) { t = 350 }
$('.mask').css(
{
left: l,
top: t
}
)
$('#magnify img').css({
left: -4 * l,
top: -4 * t
})
})
})
</script>
</body>
</html>
jQuery实现商品放大镜效果
最新推荐文章于 2022-08-05 17:29:58 发布