jquery - 如何在JQuery中使用回调?

标签 jquery callback

我是 JQuery 的 super 初学者,但我真的很喜欢使用它。我正在制作一个页面,它开始变得缓慢/出现故障,而且我还不知道如何使用回调,我认为这会有很大帮助。

我使用的代码如下所示:

$(".navHome").click(function(){
    var div=$(".navHome");
    div.animate({left:'190px'},"fast");

    var div=$(".content");
    div.animate({width:'700px'},"slow");
    div.animate({height:'250px'},"slow");

    $(".content").load("home.html");

    var div=$(".xIcon");
    div.animate({width:'20px'},"slow");
    div.animate({height:'20px'},"slow");
});

因此,我单击 div“navHome”,它会移动并且“content”div 会展开/加载。但它开始看起来不稳定。我认为回调是我所需要的,但不确定。

最佳答案

您不应在同一范围内多次定义一个变量,这是使用回调函数的代码,请注意,您可以使用一个动画方法为多个属性设置动画。

.animate( properties [, duration] [, easing] [, complete] )

$(".navHome").click(function(event){
    // event.preventDefault(); // prevents the default action of the event
    $(this).animate({left:'190px'},"fast", function(){
        $(".content").animate({width:'700px', height:'250px'},"slow", function(){
            $(this).load("home.html", function(){
                $(".xIcon").animate({width:'20px', height:'20px'},"slow");
            })
        })
    });
});

关于jquery - 如何在JQuery中使用回调?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13501706/

相关文章:

javascript - 在 Javascript 中如何先执行 image.onload 函数,然后再继续其余过程

java - 在 Struts 1.3 中处理从 Ajax 发布的 JSON 数组

javascript - .toggle() 函数

asp.net - 如何将 JSON 对象发送到 ASP.NET Web 服务并在那里处理数据?

javascript - 使用延迟动态添加顺序操作

Javascript 回调超时

c++ - GStreamer 似乎没有调用我的回调

javascript - 如何检查 Javascript 脚本是否已异步加载 (Async) 或是否存在异步属性?

Python 模型回调

javascript - js文件加载顺序