admin管理员组文章数量:1391951
Here's my jQuery
$('#samsungShine').mouseover(function () {
$('#samsungShineImage').animate({
"margin-left": "304px"
}, 700);
}).mouseout(function () {
$('#samsungShineImage').css("margin-left", "-304px");
});
When I mouseover, it works excellent, when I mouseout, it doesn't reset, it re-plays the mouseover... here is a fiddle of the problem so you can see what I mean:
/
Here's my jQuery
$('#samsungShine').mouseover(function () {
$('#samsungShineImage').animate({
"margin-left": "304px"
}, 700);
}).mouseout(function () {
$('#samsungShineImage').css("margin-left", "-304px");
});
When I mouseover, it works excellent, when I mouseout, it doesn't reset, it re-plays the mouseover... here is a fiddle of the problem so you can see what I mean:
http://jsfiddle/2tujd/
Share Improve this question asked Apr 16, 2013 at 16:55 user2287474user2287474 6462 gold badges8 silver badges15 bronze badges 3-
2
Try the
mouseenter
event – Ian Commented Apr 16, 2013 at 16:59 -
3
And try
mouseleave
event – Dom Commented Apr 16, 2013 at 16:59 - 1 you are having bubbling issues, since your mouseover handler introduces a new div that will then fire another mouseover. – iGanja Commented Apr 16, 2013 at 17:01
3 Answers
Reset to default 9Try mouseenter
and mouseleave
instead: http://jsfiddle/2tujd/11/
$('#samsungShine').mouseenter(function () {
$('#samsungShineImage').animate({
"margin-left": "304px"
}, 700);
}).mouseleave(function () {
$('#samsungShineImage').stop().css("margin-left", "-304px");
});
On the jQuery site, it says about using mouseout
and simliarly for mouseover
:
This event type can cause many headaches due to event bubbling. For instance, when the mouse pointer moves out of the Inner element in this example, a mouseout event will be sent to that, then trickle up to Outer. This can trigger the bound mouseout handler at inopportune times.
Edit: Add .stop()
inside mouseleave
to make sure any current animation is stopped prior to setting the margin-left
.
Try this, using stop
too:
DEMO
$('#samsungShine').mouseenter(function() {
$('#samsungShineImage').animate({"margin-left":"304px"}, 700);
}).mouseleave(function(){
$('#samsungShineImage').stop().css("margin-left", "-304px");
});
http://jsfiddle/2tujd/10/
I think its better to use just one handler. So you dont have any bubbling or problems with asynchronous methods.
$('#samsungShine').mouseenter(function () {
$('#samsungShineImage').animate({
"margin-left": "304px"
}, 700, function() {
$('#samsungShineImage').css("margin-left", "-304px")
});
});
本文标签: javascriptjQuery mouseover runs on mouseoutStack Overflow
版权声明:本文标题:javascript - jQuery mouseover runs on mouseout - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744710999a2621127.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论