toggle() an animate() with jquery

不羁的心 提交于 2019-12-06 00:40:42

Here is a fiddle with properly working code: http://jsfiddle.net/6zg7K/1/

You need to add $('.sidebar_menu').animate({'left':(state ? -250: 0)}, 'slow'); to the code provided by adeneo, so your entire function would be:

$('#menu_button').on('click', function(){
    var state = parseInt($('.wrapper').css('margin-left'),10) > 200;
    $('.sidebar_menu').animate({'left':(state ? -250: 0)}, 'slow');
    $('.wrapper').animate({'margin-left': (state ? 0 : 250)}, 'slow');
});

This code is a modified version of adeneo's below. All credit for code goes to him.

You'll have to create some sort of toggling functionality based on the elements left margin :

$(document).ready(function(){
    $('#menu_button').on('click', function(){
        var state = parseInt($('.wrapper').css('margin-left'),10) > 200;

        $('.sidebar_menu').animate({left:'0'}, 'slow');

        $('.wrapper').animate({'margin-left': (state ? 0 : 250)}, 'slow');
    });
});

FIDDLE

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