Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/82.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 js/jquery:如何随机化图像_Javascript_Jquery_Html_Image_Random - Fatal编程技术网

Javascript js/jquery:如何随机化图像

Javascript js/jquery:如何随机化图像,javascript,jquery,html,image,random,Javascript,Jquery,Html,Image,Random,我有一个脚本,每次倒计时到0时都会替换图像。虽然我想知道这是否可能,以及如何让这些图像(我只是按顺序列出)在每次刷新页面时随机化 以下是所有代码(图像位于此数组中“//填充图像”之前): $(文档).ready(函数(){ //用于构造和启动计数器的jquery代码 //已包装在函数中,因此我们可以随时调用它 功能启动计数器(图像加载器){ $('计数器2')。倒计时({ 图片:“img/digits.png”, 开始时间:“00:10”, timerEnd:function(){callba

我有一个脚本,每次倒计时到0时都会替换图像。虽然我想知道这是否可能,以及如何让这些图像(我只是按顺序列出)在每次刷新页面时随机化

以下是所有代码(图像位于此数组中“//填充图像”之前):


$(文档).ready(函数(){
//用于构造和启动计数器的jquery代码
//已包装在函数中,因此我们可以随时调用它
功能启动计数器(图像加载器){
$('计数器2')。倒计时({
图片:“img/digits.png”,
开始时间:“00:10”,
timerEnd:function(){callback(imageLoader)},
格式:“mm:ss”
})
}
//每次都照顾好需要做的事情
函数回调(imageLoader){
//替换图像
$('#image').attr('src',imageLoader.nextImage());
$('#counter_2').empty();//清除完成的计数器,以便可以在其位置构造新的计数器
startCounter(imageLoader);//构造一个新计数器并启动它
}
函数ImageLoader(图像){
这个。图像=图像;
该电流=0;
this.nextImage=函数(){
this.current=(this.current+1)%this.images.length;
返回this.images[this.current];;
}
}
//在此数组中填充图像
imageLoader=新的imageLoader(['img/wallpaps/2.jpg','img/wallpaps/3.jpg','img/wallpaps/4.jpg','img/wallps/5.jpg','img/wallps/6.jpg','img/wallps/7.jpg','img/wallps/9.jpg','img/wallps/10.jpg','img/wallps/11.jpg','img/wallps/12.jpg','img/wallps/13.jpg','img/wallps/14.jpg','img/wallps/15.jpg','img/wallps/16.jpg/Wallpaps/17.jpg、img/wallps/18.jpg、img/wallps/19.jpg、img/wallps/20.jpg、img/wallps/21.jpg、img/wallps/22.jpg、img/wallps/23.jpg、img/wallps/24.jpg、img/wallps/25.jpg、img/wallps/26.jpg、img/wallps/27.jpg、img/wallps/28.jpg、img/wallps/29.jpg、img/wallps/30.jpgg/wallpaps/31.jpg、img/wallps/32.jpg、img/wallps/33.jpg、img/wallps/34.jpg、img/wallps/35.jpg、img/wallps/36.jpg、img/wallps/37.jpg、img/wallps/38.jpg、img/wallps/39.jpg、img/wallps/40.jpg、img/wallps/41.jpg']);
startCounter(imageLoader);//设置所有设置!(构造一个新计数器并启动它)
});       
更换

this.current = (this.current+1) % this.images.length;


您可以使用
Math.random
函数生成随机数。例如,要在
0
9
之间生成随机数,可以执行以下操作:

var num=Math.floor(Math.random()*10);

我已经实现了一个
ImageLoader
,它将返回随机图像,在所有图像都显示出来之前不会返回相同的图像


请删除帖子中不必要的空格。这样更容易阅读。另外,如果脚本出现在容器中不会产生任何影响,则不应将其放在容器中。请将它们移到页面的开头或结尾。好的,谢谢,很抱歉,这是一个新问题。脚本是在头部还是正文中会有区别吗?我个人更喜欢在头,有人发誓要把它放在标签上以加速页面的加载,但是你把它放在哪里是很烦人的,因为它在HTMLY的中间!谢谢!但是在哪里我可以把它放在我的代码中工作呢?@ USE229 791,只需替换我自己的代码> IMAGEOLADER < /Cord>对象定义,基本上替换<代码>函数。在上面的代码中,IMAGEOLADER(图像){…} <代码>。我很高兴我能帮上忙!它确实起作用了吗?请考虑把我的答案标记为“接受”;哦,对了,啊哈,对不起,我是新来的,是的,我确信它在起作用(但是,我能再问一件事吗?每次我打开页面,它都会转到^image。有没有办法让它转到上面提到的随机图像之一?你可以在页面加载
$('#image').attr('src',imageLoader.nextImage())时设置图像的
src
属性
,或者您可以使用
setInterval
而不是
倒计时
插件,或者找到一种方法来设置插件,使其在页面加载后立即调用
回调
函数。
this.current = (this.current+1) % this.images.length;
this.current = Math.floor(Math.random() * this.images.length);
function ImageLoader(images) {
    this.images = images;
    this.usedIndexes = {};
    this.displayedCount = 0;
}

ImageLoader.prototype.nextImage = function () {
    var self = this,
        len = self.images.length,
        usedIndexes = self.usedIndexes,
        lastBatchIndex = self.lastBatchIndex,
        denyLastBatchIndex = self.displayedCount !== len - 1,
        index;

    if (this.displayedCount === len) {
        lastBatchIndex = self.lastBatchIndex = self.lastIndex;
        usedIndexes = self.usedIndexes = {};
        self.displayedCount = 0;
    }

    do {
        index = Math.floor(Math.random() * len);
    } while (usedIndexes[index] || (lastBatchIndex === index && denyLastBatchIndex));

    self.displayedCount++;
    usedIndexes[self.lastIndex = index] = true;

    return self.images[index];
};