我是 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/