2016-07-16 72 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