我對JavaScript和jQuery非常陌生,可能會嘗試創建響應式幻燈片以跳入深層。我有以下代碼在大小調整滑塊以適應窗口,使用該設置各個幻燈片的函數通過使用的setInterval等於滑塊容器的寬度的術語:開始和停止setInterval和clearInterval
var slider = $('.slider'), // Get the div with a class of slider
sliderWidth = slider.width(), // Get the width of the slider
gallery = $('.slider ul'),
slides = $('.slider ul').children('li'),
noOfSlides = slides.length,
speed = 5000,
current = 0,
slideShowInt,
responseSlider;
// Initially set the width of the li to equal the width of the slider
$('.slider ul').children('li').width(sliderWidth);
// Run a function that keeps making the
// li width equal the slider when window is resized
function resizeSlider(){
responseSlider = setInterval(function(){
slider = $('.slider'),
sliderWidth = slider.width();
slides.width(sliderWidth);
console.log(sliderWidth);
},10);
}
$(window).resize(resizeSlider);
clearInterval(responseSlider);
這甚至可能不是正確的這樣做的方式,但我想知道是否有可能停止使用clearInterval運行setInterval,直到窗口再次調整大小。在目前的狀態下,看着控制檯,它只是繼續記錄寬度。
在此先感謝!
謝謝,所有的答案會給我正確的結果,但在我的情況下,我誤解.resize,它現在工作正常,不使用計時器。 – user2057507 2013-02-16 10:35:17