我正在尝试实现一种效果,其中图像(它可以具有不同的大小)在悬停时缩放,并在其周围出现边框。非缩放图像的最大尺寸限制为 250 像素,缩放后的图像最大为 550 像素。
我通过在 mouseenter 上将一个类应用于 div 并在 mouseleave 上将其删除来做到这一点。这工作得很好,但问题是我想检测 div 是否部分离开屏幕。为此,我需要转换后的 div 的偏移量:
$(document).ready(function () {
$('.image-container').on('mouseenter', function () {
var y = $(this).offset().top,
x = $(this).offset().left;
console.log('old', x, y);
$(this).addClass('image-hover');
y = $(this).offset().top;
x = $(this).offset().left;
console.log('new', x, y);
});
$('.image-container').on('mouseleave', function () {
$(this).removeClass('image-hover');
});
});
如果您查看下面的 jsfiddle,我会在添加类后得到旧的偏移量。
那么,如何获得更新的偏移量?有没有更好的方法来做我想要实现的目标?