Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 连续淡入淡出随机图片_Javascript_Jquery - Fatal编程技术网

Javascript 连续淡入淡出随机图片

Javascript 连续淡入淡出随机图片,javascript,jquery,Javascript,Jquery,我正在尝试创建一个函数,该函数将连续淡入淡出随机选择的图片,以便相同的图片不会出现两次。我可以让它淡出/更改图像/淡入一次,如果用 $("img").click(function() { CODE }); 但是每次我都要点击图像,我一直在尝试将其变成一个函数,当页面加载时调用该函数: $(document).ready(function() { $("img").fadeOut(2000, function() { while (picPicker1 === picPicker2)

我正在尝试创建一个函数,该函数将连续淡入淡出随机选择的图片,以便相同的图片不会出现两次。我可以让它淡出/更改图像/淡入一次,如果用

$("img").click(function() { CODE });
但是每次我都要点击图像,我一直在尝试将其变成一个函数,当页面加载时调用该函数:

$(document).ready(function() {
  $("img").fadeOut(2000, function() {
    while (picPicker1 === picPicker2) {
      picPicker2 = (Math.floor(Math.random() * 5) - 1);
    }

    var source = pics[picPicker2];
    $("img").attr("src", source);
    $("img").fadeIn(2000);
    picPicker1 = picPicker2;
  });
});

pics[]
是一个数组,包含五个不同图片的网址。我还尝试使用
setInterval
,但也不起作用。所有这些语言都是新手,所以提前感谢您的耐心

您可以使用
setInterval()
for(x=5;x>0;x++){}
var x=0;do{}while(x=0),或
变量x=0;而(x=0){}

setInterval
是正确使用的函数。您在问题中提供的代码缺少一个结束符(代码>)

下面是一些使用
setInterval
更改图像的基本代码:

JQuery:

var pics = [
  "https://placehold.it/350x150/ff0000",
  "https://placehold.it/350x150/00ff00",
  "https://placehold.it/350x150/0000ff",
  "https://placehold.it/350x150/F2F5A9",
  "https://placehold.it/350x150/FF00FF",
];

setInterval(function() {
  var picPicker2 = (Math.floor(Math.random() * 5) + 1);

  var source = pics[picPicker2];
  $("img").fadeOut(500).attr("src", source).fadeIn(500);
}, 2000);
注意:图像偶尔会重复,但我提供的代码只是供您改进的基础

$(文档).ready(函数(){
var picPicker,源;
函数generateImage(){
$(“img”).fadeOut(2000年,函数(){
picPicker=(Math.floor(Math.random()*200));
资料来源:https://unsplash.it/400/300/?image=“+picPicker;
$(“img”).attr(“src”,来源);
$(“img”)。fadeIn(2000年);
});
设置超时(generateImage,5000);
}
生成图像();
});

查看
setInterval()
js函数。