gpt4 book ai didi

jquery - 中心缩放图像的更好方法?

转载 作者:行者123 更新时间:2023-12-01 00:49:10 24 4
gpt4 key购买 nike

我编写了一个 jquery 函数,用于在鼠标悬停时放大和缩小图像,同时保持约束框大小不变。

在这里演示:http://jsfiddle.net/q9jHu/

它工作得很好,但如果你在图像之间快速摇动光标,它就会变得困惑。处于中间缩放状态时,脚本存储的尺寸不正确。我尝试将 .stop() 添加到方程中,但似乎无法修复该故障。

我猜我应该使用 .each() 存储数据,而不是在鼠标悬停时存储数据,但我不知道该怎么做。

有什么想法吗?

最佳答案

看这里 http://jsfiddle.net/q9jHu/4/

$('.pixbox img').on({
mouseover: function(){
var $scale = 1.5;
if (!$(this).data('w'))
{
$(this).data('w', $(this).width())
.data('h', $(this).height());
}
$(this).stop(true).animate({
width: $(this).data('w')*$scale,
height: $(this).data('h')*$scale,
left: -$(this).data('w')*($scale - 1)/2,
top: -$(this).data('h')*($scale - 1)/2
}, 'fast');
},
mouseout: function(){
$(this).stop(true).animate({
width: $(this).data('w'),
height: $(this).data('h'),
left: 0,
top: 0
}, 'fast');
}
});

关于jquery - 中心缩放图像的更好方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9147691/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com