Cra*_*rax 3 javascript image timer
我将如何为此js添加计时器,以便在'x'时间后图像会自动更改.目前,通过带有'rel'属性的'a href'进行更改,但仍需要具有'rel'的功能.
JS:
$(document).ready(function (){
$('#button a').click(function(){
var integer = $(this).attr('rel');
$('#myslide .cover').css({left:-1476*(parseInt(integer)-1)}).hide().fadeIn(); /*----- Width of div mystuff (here 160) ------ */
$('#button a').each(function(){
$(this).removeClass('active');
if($(this).hasClass('button'+integer)){
$(this).addClass('active')}
});
});
});
Run Code Online (Sandbox Code Playgroud)
HTML:
<div id="myslide">
<div class="cover">
<div class="mystuff">
<img src="images/header_01.jpg" rel="1"></img>
<img src="images/header_02.jpg" rel="1"></img>
<img src="images/header_03.jpg" rel="1"></img>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
您应该考虑使用setInterval和一组图像来更改源.这将迫使图像连续循环
var images = ['header_01.jpg','header_02.jpg','header_03.jpg'],
index = 0, // starting index
maxImages = images.length - 1;
var timer = setInterval(function() {
var curImage = images[index];
index = (index == maxImages) ? 0 : ++index;
// set your image using the curImageVar
$('div.mystuff img').attr('src','images/'+curImage);
}, 1000);
Run Code Online (Sandbox Code Playgroud)