Jquery 调整图像大小
我有 4 张水平图像。调整大小功能适用于鼠标悬停事件。这意味着如果我在这些图像上快速或缓慢地移动鼠标,它们都会调整大小。因此,我需要用户将鼠标悬停在给定图像上至少 1.5 秒,然后继续调整大小。这是不正确的代码:
$('a img').mouseover(function(){
$(this).delay(1500).animate({
width: "315px",
height: "225px",
marginLeft: "-50px"
}, 1500 );
});
$('a img').mouseout(function(){
$(this).animate({
width: "210px",
height: "150px",
marginTop: "0px",
marginLeft: "0px"
}, 500 );
});
I have horizontal set of 4 images. The resize function works on mouseover event. This means that if I move the mouse very fast or slow over these images they will All resize. Because of this I need the user to keep the mouse over for at least 1.5 sec over a given image and then proceed with the resize. This is the unproper code:
$('a img').mouseover(function(){
$(this).delay(1500).animate({
width: "315px",
height: "225px",
marginLeft: "-50px"
}, 1500 );
});
$('a img').mouseout(function(){
$(this).animate({
width: "210px",
height: "150px",
marginTop: "0px",
marginLeft: "0px"
}, 500 );
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 setTimeout 和clearTimeout 来实现此目的:
.hover() 也是 jQuery 中处理的快捷方法mouseOver 和 mouseOut 同时出现。
you can use setTimeout and clearTimeout for this:
also .hover() is a shortcut method in jQuery to handle mouseOver and mouseOut at the same time.
我会使用
.setTimeout()
I would use
.setTimeout()
我不确定您想要的确切逻辑,但这是一种方法。我没有连接实际的动画,只是向您展示它何时触发。
HTML:
JS:
通过在触发动画之前检查鼠标是否仍在图像上,可以使这一点更加简单,以防万一 mouseleave 事件以某种方式错过。
您可以在这里看到它的实际效果: http://jsfiddle.net/jfriend00/9q36R/
I'm not sure of the exact logic you want, but here's one way to do it. I didn't hook up the actual animation, but rather just show you when it would trigger.
HTML:
JS:
This could be made a tiny bit more foolproof by checking if the mouse was still over the iamges before firing the animation just in case the mouseleave event was missed somehow.
You can see it in action here: http://jsfiddle.net/jfriend00/9q36R/