2016-07-16 87 views

回答

1

看看这个Pen

var nextimage = 0; 
var timer = 0; 
doSlideshow(); 

function doSlideshow() { 
    if (nextimage >= images.length) { 
    nextimage = 0; 
    } 
    $('.col-md-8').css('background-image', 'url("' + images[nextimage++] + '")').fadeIn(3000, function() { 
    timer = setTimeout(doSlideshow, 3000); 
    }); 
} 
$(".col-md-8").hover(function() { 
    clearTimeout(timer); 
}); 

$(".col-md-8").mouseout(function() { 
    setTimeout(doSlideshow, 3000); 
}); 

var elems = document.getElementsByClassName("col-md-8"); 
for (var i = 0; i < elems.length; i++) { 
elems[i].style.backgroundRepeat = "no-repeat"; 
elems[i].style.backgroundSize = "100%"; 
} 
+0

我感谢你的帮助。顺便说一句,你为什么创建计时器并分配到零? – NZMAI

+0

您分配了timer = setTimeout(doSlideshow,3000);它总是返回1.如果将其设置为0,它将精确返回定时器值。然后我们可以清除计时器 – cheralathan