jQuery FadeIn效果更改图像
我有问题。我试图通过以下代码将div上的最后一个图像更改为新图像:jQuery FadeIn效果更改图像,jquery,html,Jquery,Html,我有问题。我试图通过以下代码将div上的最后一个图像更改为新图像: var rand = Math.floor(8*Math.random()+1); $("#foto-"+ rand).fadeIn("slow", function() { $("#foto-"+ rand).html('<img style="position: relative;" height="100%" src="slides/'+ Math.floor(fotos*Math.random()+1) +
var rand = Math.floor(8*Math.random()+1);
$("#foto-"+ rand).fadeIn("slow", function() {
$("#foto-"+ rand).html('<img style="position: relative;" height="100%" src="slides/'+ Math.floor(fotos*Math.random()+1) +'.jpg" />');
});
var rand=Math.floor(8*Math.random()+1);
$(“#foto-”+rand).fadeIn(“慢”,函数(){
$(“#foto-”+rand).html(“”);
});
此代码更改图像,但没有显示效果。我认为您不能像那样使用
fadeIn
。您可以使用fadeIn
使项目可见,但不能更改DOM。您应该使用animate
进行自定义动画,如我在此处演示的那样:
然而,在这种情况下,animate
可能有些过分,因为您实际上只需要淡入淡出。只需这样做:
var rand = Math.floor(8*Math.random()+1);
$("#foto-"+ rand).fadeOut();
$("#foto-"+ rand).html('<img style="position: relative;" height="100%" src="slides/'+ Math.floor(fotos*Math.random()+1) +'.jpg" />');
$("#foto-"+ rand).fadeIn();
var rand=Math.floor(8*Math.random()+1);
$(“#foto-”+rand).fadeOut();
$(“#foto-”+rand).html(“”);
$(#foto-“+rand).fadeIn();
试试这个:
var rand = Math.floor(8*Math.random()+1);
$("#foto-"+ rand).hide();
$("#foto-"+ rand).html('<img style="position: relative;" height="100%" src="slides/'+ Math.floor(fotos*Math.random()+1) +'.jpg" />');
$("#foto-"+ rand).fadeIn("slow");
var rand=Math.floor(8*Math.random()+1);
$(“#foto-”+rand).hide();
$(“#foto-”+rand).html(“”);
美元(“#foto-”+rand).fadeIn(“慢”);