Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/69.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 如何每6秒更改一次图像?_Javascript_Jquery - Fatal编程技术网

Javascript 如何每6秒更改一次图像?

Javascript 如何每6秒更改一次图像?,javascript,jquery,Javascript,Jquery,如何为下面的代码添加一个间隔,使其每6秒自动更改一次图像 我使用的代码来自 $(窗口)。加载(函数(){ var theImage=$('ul li img'); var theWidth=theImage.width(); //裹在母亲的衣服里 $('ul')。包装(''); //将“高度-宽度”和“溢出隐藏”指定给“母亲” $(“#母亲”).css({ 宽度:函数(){ 返回宽度; }, 高度:函数(){ 返回图像高度(); }, 位置:'相对', 溢出:“隐藏” }); //获取图像大小的

如何为下面的代码添加一个间隔,使其每6秒自动更改一次图像

我使用的代码来自

$(窗口)。加载(函数(){
var theImage=$('ul li img');
var theWidth=theImage.width();
//裹在母亲的衣服里
$('ul')。包装('');
//将“高度-宽度”和“溢出隐藏”指定给“母亲”
$(“#母亲”).css({
宽度:函数(){
返回宽度;
},
高度:函数(){
返回图像高度();
},
位置:'相对',
溢出:“隐藏”
});
//获取图像大小的总和并设置为ul的宽度
var totalWidth=图像长度*宽度;
$('ul').css({
宽度:函数(){
返回总宽度;
}
});
$(图像)。每个(函数(索引){
$(this.nextAll('a'))
.bind(“单击”),函数(){
if($(this).is(“.next”)){
$(this).parent('li').parent('ul').animate({
“左边距”:(-(索引内+1)*宽度)
}, 1000)
}如果($(this).is(“.previous”)){
$(this).parent('li').parent('ul').animate({
“左边距”:(-(索引内-1)*宽度)
}, 1000)
}如果($(this).is(“.startover”)){
$(this).parent('li').parent('ul').animate({
“左边距”:(0)
}, 1000)
}
});//close.bind()
});//关闭。每个()
}); //医生准备好了吗

如前所述,使用setInterval()javascript函数。

下面是一个扩展答案

var intNum = 6000; //repeat every 6 seconds
function startInterval(){
    window.int = setInterval(function(){
        //code to move to next image
    },intNum);
}
这将设置图像的间隔,自动转到下一个,与开关的单击事件相比,可能需要进行小的调整,因此我将内部留空

当您知道其余代码已加载并准备就绪(设置了单击事件等)时,应调用函数startInterval()

当您执行单击事件以手动来回切换时,您希望使用以下命令

clearInterval(int);

//code to switch image from click

startInterval();

您需要使用setInterval()函数

基本上,它看起来像:

var currentImg=0;//Current image tracker
var imgList["img1.jpg","img2.jpg","img3.jpg"];//Image names

var changeImage = function(){
     //Change src attribute on img element
     $('ul li img').attr('src','/imgdir/'+imgList[currentImg]);
     if(currentImg>=imgList.length-1)//Check if current image is the last in the list
         currentImg=0;//Sets to first images if true
     else
         currentImg++;//Sets to next image if false
}
//Sets an interval of 6000ms on the window object that calls the function changeImage()
//on every trigger
window.setInterval(changeImage(),6000);


希望这能有所帮助,我建议你也去看看……请不要参考W3学校(因为)好的,我不知道W2学校的问题。我更改了对mozilla文档的引用。很抱歉,我不理解代码,应该将其添加到哪里?帮助你在找类似的东西吗?我真的建议你用它。我试着把代码粘贴在它们中间,我试着把代码粘贴在你的中间,从头到尾都没用。或者我应该在“/”代码上粘贴什么以移动到下一个图像“?难道没有什么东西可以调用函数“.bind”(“click”,function(){)或在两者之间添加的东西吗?基本上,您希望调整代码,使其运行时就像在其中单击“next”一样。您需要调用函数“startInterval()”有人来做这个run@Roberto:您只将代码放入回调中,该回调应每X秒执行一次。为此,您可能需要理解您正在使用的代码。请花时间理解它并阅读jQuery文档。否则,从长远来看,您将很难理解它。
var currentImg=0;//Current image tracker
var imgList["img1.jpg","img2.jpg","img3.jpg"];//Image names

var changeImage = function(){
     //Change src attribute on img element
     $('ul li img').attr('src','/imgdir/'+imgList[currentImg]);
     if(currentImg>=imgList.length-1)//Check if current image is the last in the list
         currentImg=0;//Sets to first images if true
     else
         currentImg++;//Sets to next image if false
}
//Sets an interval of 6000ms on the window object that calls the function changeImage()
//on every trigger
window.setInterval(changeImage(),6000);