使用 jQuery 制作淡出动画时遇到问题
我正在尝试淡出页面上的一些元素,使用 AJAX 获取新元素,然后淡入新元素。淡入效果很好,但淡出效果不行。我尝试使用 fadeOut,因为 fadeIn 工作正常,但淡出根本不起作用 - 元素就消失了。我现在正在尝试制作不透明度变化的动画。它对于淡入效果很好。代码如下:
$(document).ready(function() {
setTimeout("getTestimonial()", 10000);
});
function getTestimonial() {
var counter = $('#products #cart-widget .counter').html();
$('#products #cart-widget p > span').each(function(index) {
if($(this).is('.counter')) {
} else {
$(this).animate({opacity: 0}, 5000, function(){});
}
});
$.get("testimonials_include.php5", {'counter':counter}, function(data) {
$('#products #cart-widget p').replaceWith(data);
$('#products #cart-widget p').children().css("opacity",0);
$('#products #cart-widget p > span').each(function(index) {
if($(this).is('.counter')) {
} else {
$(this).animate({opacity: 1}, 5000, function(){});
}
});
});
setTimeout("getTestimonial()", 10000);
}
请注意,新元素的不透明度默认为 1,因此我必须将它们设置为 0,然后淡入才能起作用。有谁知道为什么它不消失?
I'm trying to fade out some elements on a page, fetch new ones with AJAX, and then fade in the new ones. The fade in is fine, but the fadeout just won't work. I tried using fadeOut, because fadeIn worked fine, but the fadeout simply wouldn't work - the elements just vanished. I'm now trying to animate an opacity change. It works fine for the fade in. Here is the code:
$(document).ready(function() {
setTimeout("getTestimonial()", 10000);
});
function getTestimonial() {
var counter = $('#products #cart-widget .counter').html();
$('#products #cart-widget p > span').each(function(index) {
if($(this).is('.counter')) {
} else {
$(this).animate({opacity: 0}, 5000, function(){});
}
});
$.get("testimonials_include.php5", {'counter':counter}, function(data) {
$('#products #cart-widget p').replaceWith(data);
$('#products #cart-widget p').children().css("opacity",0);
$('#products #cart-widget p > span').each(function(index) {
if($(this).is('.counter')) {
} else {
$(this).animate({opacity: 1}, 5000, function(){});
}
});
});
setTimeout("getTestimonial()", 10000);
}
Note that the new elements' opacity was by default 1, so I had to set them to 0 before the fade in could work. Does anyone have any ideas why it isn't fading out?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
啊 - 问题是元素在淡入淡出完成之前就被交换了。我将整个 AJAX 函数放在 animate 方法的完成函数中,嘿,快点!
Ah - the problem was that the elements were being swapped before the fade could complete. I put the entire AJAX function in the completion function for the animate method and hey presto!