代码之家  ›  专栏  ›  技术社区  ›  The Muffin Man

将jQuery toggle设置回开头

  •  0
  • The Muffin Man  · 技术社区  · 15 年前

    我有以下代码,可以在单击链接时显示/隐藏(切换)div:

    $('#complete-services-link').toggle(
        function () {
             $('#complete-services-box').fadeIn('fast');
        }, 
        function () {
             $('#complete-services-box').fadeOut('fast');
        }
    );
    

    #complete-services-box 是正在显示/隐藏的div,通常您会再次单击链接以关闭该框,但如果我这样做,则您可以单击该框以关闭它,然后单击切换链接,我将不得不双击它,因为该链接正在侦听第二个函数 $('#complete-services-box').fadeOut('fast');

    $('#complete-services-link').toggle(
        function () {
            $('#complete-services-box').fadeIn('fast');
        }, 
        function () {
            $('#complete-services-box').fadeOut('fast');
            $('#complete-services-box').click(function () {
                 $('#complete-services-box').fadeOut('fast');
            });
        }
    );
    
    1 回复  |  直到 15 年前
        1
  •  0
  •   Reigel Gallarde    15 年前

    使用 .stop(true,true)

    $('#complete-services-link').toggle(
        function () {
             $('#complete-services-box').stop(true,true).fadeIn('fast');
        }, 
        function () {
             $('#complete-services-box').stop(true,true).fadeOut('fast');
        }
    );