将.toggle()添加到jQuery中

时间:2012-06-18 16:36:31

标签: javascript jquery

我首先尝试在此代码中添加.toggle(),以便菜单在点击时生成动画,但也会在点击时关闭,然后意识到我无法在.animate内使用.toggle() {1}}非常好,所以我正在尝试.click()

如果有办法用我正在寻找的点击替换我的mouseentermouseleave功能。

我已经搜索了但是我无法在动画菜单中找到任何地方,所以这里是函数的javascript:

$('#sdt_menu > li').bind('mouseenter',function(){
    var $elem = $(this);
    $elem.find('img')
         .stop(true)
         .animate({
            border: '5px solid #000',

            'width':'150px',
            'height':'170px',
            'left':'0px'
         },400,'easeOutBack')
         .andSelf()
         .find('.sdt_wrap')
         .stop(true)
         .animate({'top':'140px'},500,'easeOutBack')
         .andSelf()
         .find('.sdt_active')
         .stop(true)
         .animate({'height':'170px'},300,function(){
        var $sub_menu = $elem.find('.sdt_box');
        if($sub_menu.length){
            var left = '170px';
            if($elem.parent().children().length == $elem.index()+1)
                left = '-170px';
            $sub_menu.show().animate({'left':left},200);
        }    
    });
}).bind('mouseleave',function(){
    var $elem = $(this);
    var $sub_menu = $elem.find('.sdt_box');
    if($sub_menu.length)
        $sub_menu.hide().css('left','0px');

    $elem.find('.sdt_active')
         .stop(true)
         .animate({'height':'0px'},300)

         .andSelf().find('img')
         .stop(true)
         .animate({
            border: "2px solid #FFFFFF",
            'width':'150px',
            'height':'150px',
            'left':'0px'},400)
         .andSelf()
         .find('.sdt_wrap')
         .stop(true)
         .animate({'top':'25px'},500);
});

3 个答案:

答案 0 :(得分:1)

下面我添加一个类,如果它打开,并删除一个类,如果它关闭,告诉onclick事件该做什么。

结帐http://api.jquery.com/toggle-event/。很简单。

归功于apsillers

$('#sdt_menu > li').toggle(function(){
    var $elem = $(this);
    var $sub_menu = $elem.find('.sdt_box');
    $elem.find('img')
         .stop(true)
         .animate({
            border: '5px solid #000',

            'width':'150px',
            'height':'170px',
            'left':'0px'
         },400,'easeOutBack')
         .andSelf()
         .find('.sdt_wrap')
         .stop(true)
         .animate({'top':'140px'},500,'easeOutBack')
         .andSelf()
         .find('.sdt_active')
         .stop(true)
         .animate({'height':'170px'},300,function(){
        if($sub_menu.length){
            var left = '170px';
            if($elem.parent().children().length == $elem.index()+1)
                left = '-170px';
            $sub_menu.show().animate({'left':left},200);
        }    
    });
}, function(){      
    var $elem = $(this);
    var $sub_menu = $elem.find('.sdt_box');
    if($sub_menu.length)
        $sub_menu.hide().css('left','0px');     
    $elem.find('.sdt_active')
         .stop(true)
         .animate({'height':'0px'},300)     
         .andSelf().find('img')
         .stop(true)
         .animate({
            border: "2px solid #FFFFFF",
            'width':'150px',
            'height':'150px',
            'left':'0px'},400)
         .andSelf()
         .find('.sdt_wrap')
         .stop(true)
         .animate({'top':'25px'},500);
});

答案 1 :(得分:0)

您可以定义全局变量,例如var isOpen = true;,然后更改您的代码以进行点击

$('#sdt_menu > li').on('click', isOpen, function() {
    if (isOpen) {
        // Close
    } else {
        // Open
    }
    isOpen = !isOpen;
});

答案 2 :(得分:0)

 var isOpen = true;
 $('#sdt_menu > li').click(function(){
       if(isOpen ==  true){
           $(this).hide();
           isOpen = false;
       }else{
           $(this).show();
           isOpen = true;
       }
    }

您可以根据需要设置isOpen变量的默认值。

.show()和.hide是切换功能的扩展功能。