<script src="https://cdn.jsdelivr.net/jquery/3.5.1/jquery.min.js"></script>
<div class="image-container">
<img src="example.jpg" alt="翻滚的胖子博客" />
<div class="zoom-container"></div>
</div>
.image-container {
width: 400px;
height: 300px;
position: relative;
}
.zoom-container {
width: 200px;
height: 150px;
background-color: rgba(0,0,0,0.5);
position: absolute;
top: 0;
left: 0;
display: none;
}
$(document).ready(function() {
$('.image-container').mouseover(function() {
$('.zoom-container').show();
}).mouseout(function() {
$('.zoom-container').hide();
}).mousemove(function(e) {
var offsetX = e.pageX - $(this).offset().left;
var offsetY = e.pageY - $(this).offset().top;
var zoomX = offsetX / $(this).width() * 100;
var zoomY = offsetY / $(this).height() * 100;
$('.zoom-container').css({'background-position': zoomX + '% ' + zoomY + '%'});
});
});
本文为翻滚的胖子原创文章,转载无需和我联系,但请注明来自猿教程iskeys.com