jquery .stop() not working

烂漫一生 提交于 2019-12-19 19:44:57

问题


I'm trying to build a menu where only the first item is shown by default, and when you hover over it the rest of the items slide out, and are hidden again when the mouse leaves. It's mostly working but if the mouse exits before it's finished sliding out then the hide function isn't called. I thought stop() was supposed to take care of this but it doesn't seem to have any affect.

$(function(){
    $("#menubar").children(".breadcrumbs").children("li + li").hide();

    $("#menubar .breadcrumbs").hover(function() {
        $(this).children("li + li").stop().show("slide", {}, 'slow');
    }, function() {
        $(this).children("li + li").stop().hide("slide", {}, 'slow');
    });
});

jsFiddle demo

Can anybody see what I'm doing wrong?


回答1:


I'm not the biggest animation genius, but I bet it has to do with the fact that you're rebuilding the jQuery object in each handler. Try this:

$(function(){
    var children = $('#menubar .breadcrumbs').children('li + li');
    children.hide();

    $("#menubar .breadcrumbs").hover(function() {
        children.stop().show("slide", {}, 'slow');
    }, function() {
        children.stop().hide("slide", {}, 'slow');
    });
});

edit — @melee points out in comments that the behavior of this particular setup is not always stable. If you carefully hold the mouse over towards the right edge of the "Home" <li> (near the ">" character), then sometimes the animation sort-of freaks out. It's not clear why, but the browser just gets very confused about where the elements should be rendered.




回答2:


There are parameters to the STOP(), function, (true,true), (false,false) have you tried altering these to change the desired effect?




回答3:


The definition of the stop function is

.stop( [ clearQueue ], [ jumpToEnd ] )

The oficial site goes...

clearQueue: A Boolean indicating whether to remove queued animation as well. Defaults to false.

jumpToEnd: A Boolean indicating whether to complete the current animation immediately. Defaults to false.

This is from http://api.jquery.com/stop/



来源:https://stackoverflow.com/questions/5406903/jquery-stop-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!