2012-11-12 46 views
1

我運行一個頁面(#home)下面的代碼應該平滑地注入滑塊,或者如果頁面容器下480像素離開頁面原樣。

我無法使調整大小事件順利進行100%。

如果我縮小窗口腳本(js.slide.js)不會被觸發,但內容將在(slide.php)被加載。如果我繼續將窗戶縮小一點,它就會一切正常。

任何人都可以請告訴我如何才能順利運作。代碼如下

$(document).ready(function(){ 

if ($("#home").length > 0){ 

var homeSlideShow = { 
    $promoArea: $('#promo-area'), 
    $currentContent: $('#promo-area').contents(), 
    $pageContainer: $('.page'), 

    init: function(){ 
     var hSS = homeSlideShow;    
     if (hSS.$pageContainer.width() > 480){ 
      hSS.setTheSlideShow(); 
     } else{ 
      hSS.$promoArea.html(hSS.$currentContent); 
     } 

    }, 

    setTheSlideShow: function(){ 
     var hSS = homeSlideShow;       
     $.getScript(myscript_wp_vars.temp_dir + '/js/slide.js', function(){ 
     hSS.$promoArea.load(myscript_wp_vars.temp_dir + '/libs/slide.php #c4u-slide', 
     function(){ 
     var options = { 
        preloader: false, 
        nextButton: true, 
        prevButton: true, 
        animateStartingFrameIn: true, 
        transitionThreshold: 250       
       }; 

       var sequence = $("#sequence").sequence(options).data("sequence"), 
        $slideShow = $("#c4u-slide"); 
       }); 

     }); 
    } 

}; 




// 
// Check page size 
// 
if (homeSlideShow.$pageContainer.width() > 480){ 
    homeSlideShow.setTheSlideShow(); 
} 



// 
// On window resize 
// 
$(window).resize(function() { 
    homeSlideShow.init(); 
}); 

}// END home.length 

});//End $(document).ready(function() 

在此先感謝您的任何幫助或建議。

乾杯

諾埃爾

回答

2

window.resize事件被觸發多次,這取決於瀏覽器的行爲。我會建議你試試這個:

var timeoutResize; 
$(window).resize(function(){ 
     if(typeof timeoutResize != 'undefined') clearTimeout(timeoutResize); 
     timeoutResize = setTimeout(function(){homeSlideShow.init();},50); 

    }); 
+1

有距離Ben Alman一個偉大的jQuery的擴展限制和消除抖動事件:http://benalman.com/projects/jquery-throttle-debounce-plugin/ – nxtwrld

+0

乾杯我烤顛簸超時一點點,但它的偉大工程。 @nxtwrld感謝您的節流並消除抖動的事件,非常有用的信息特別是與窗口大小調整的頭。乾杯 – noelmcg

+0

下劃線也有反跳:http://underscorejs.org/#debounce –

相關問題