3

我有 2 个功能可以打开和关闭页面上的侧边栏。

function closeSidebar(functionAfterClose) {
        var functionAfterClose = functionAfterClose || function() {};
        $("#sidebar").removeClass("open");
        $("#sidebar").animate({marginLeft: margin*-1},"fast", "swing", functionAfterClose);
        $("#dummy-column").animate({marginLeft: margin*-1},"fast", "swing");
}

function openSidebar() {
        $("#sidebar").addClass("open");
        $("#sidebar").animate({marginLeft:0},"fast", "swing");
        $("#dummy-column").animate({marginLeft:0},"fast", "swing");
}

我正在尝试将函数传递给 closeSidebar() 以在关闭动画完成后运行。但它似乎直接运行而不是等待侧边栏动画完成。

closeSidebar(function() {
                alert("function called");
                $(this).addClass("current");
                openSidebar();
            });

动画完成后,我缺少什么来调用函数?

jsFiddle - 单击右侧的按钮,它应该动画,调用函数,然后再次动画回来。

4

1 回答 1

6

你的 javascript 是正确的。问题出在 CSS 中。您为边距属性设置了过渡。删除它,你的 JS 就可以正常工作了。

于 2013-05-19T06:15:17.563 回答