2013-11-26 34 views
1

我能做些什麼來加快它的速度?或者有沒有你注意到的新手錯誤?我的jQuery運行速度有點慢。我如何加快速度?

我注意到我重複了很多變量。這樣我就不必寫出每一行,但我不確定是否有更好的解決方案?

該問題通常發生在切換回標籤後不在上面。它需要幾秒鐘點擊外環來響應,但這很好。幾乎就像剩下的代碼運行它需要重寫?我不確定,但如果有經驗的用戶能夠在我的代碼中看到可以改進的東西,我會很高興。

這裏有一個工作jsFiddle http://jsfiddle.net/eA6x6/1/,以下是我的JavaScript代碼的完整。

jQuery(document).ready(function() { 

var timeoutHandle; 

// hide stuff 
    var hideServices = function() { 
      jQuery(".services-inner").css({"opacity": "0"}); 
      jQuery(".insignia-inner").css({"opacity": "0"}); 
      jQuery(".insignia-inner-text").css({"opacity": "0"}); 
    }; 

    var hideAll = function() { 
      jQuery(".military-kit-inner").css({"opacity": "0"}); 
      jQuery(".property-inner").css({"opacity": "0"}); 
      jQuery(".home-contents-inner").css({"opacity": "0"}); 
      jQuery(".travel-inner").css({"opacity": "0"}); 
      jQuery(".events-inner").css({"opacity": "0"}); 
      jQuery(".adventurous-training-inner").css({"opacity": "0"}); 
      jQuery(".personal-injury-inner").css({"opacity": "0"}); 
      jQuery(".challenge-pursuits-inner").css({"opacity": "0"}); 
      jQuery(".sports-and-tours-inner").css({"opacity": "0"}); 
      jQuery(".winter-sports-inner").css({"opacity": "0"}); 

      jQuery(".now-available").css({"opacity": "0"}); 
      jQuery(".now-available-background").css({"opacity": "0"}); 
      jQuery(".launched-shortly").css({"opacity": "0"}); 
      jQuery(".launched-shortly-background").css({"opacity": "0"}); 
    }; 

    var showServicesDelay = function() { 
      timeoutHandle = setTimeout(function() { 
        jQuery(".services-inner").css({"opacity": "0.5"}); 
       jQuery(".insignia-inner").css({"opacity": "1"}); 
       jQuery(".insignia-inner-text").css({"opacity": "1"}); 
       hideAll(); 
     }, 5000);  
    }; 

// show messages 
    var showLaunchedShortly = function() { 
      jQuery(".launched-shortly").css({"opacity": "1"}); 
      jQuery(".launched-shortly-background").css({"opacity": "1"}); 
    }; 

    var showNowAvailable = function() { 
      jQuery(".now-available").css({"opacity": "1"}); 
      jQuery(".now-available-background").css({"opacity": "1"}); 
    }; 

// show services 
    var showMilitaryKit = function() { 
      jQuery(".military-kit-inner").css({"opacity": "1"}); 

      clearTimeout(timeoutHandle); 
    }; 

    var showProperty = function() { 
      jQuery(".property-inner").css({"opacity": "1"}); 

      clearTimeout(timeoutHandle);  
    }; 

    var showHomeContents = function() { 
      jQuery(".home-contents-inner").css({"opacity": "1"}); 

      clearTimeout(timeoutHandle); 
    }; 

    var showTravel = function() { 
      jQuery(".travel-inner").css({"opacity": "1"}); 

      clearTimeout(timeoutHandle); 
    }; 

    var showEvents = function() { 
      jQuery(".events-inner").css({"opacity": "1"}); 

      clearTimeout(timeoutHandle); 
    }; 

    var showAdventurousTraining = function() { 
      jQuery(".adventurous-training-inner").css({"opacity": "1"}); 

      clearTimeout(timeoutHandle); 
    }; 

    var showPersonalInjury = function() { 
      jQuery(".personal-injury-inner").css({"opacity": "1"}); 

      clearTimeout(timeoutHandle); 
    }; 

    var showChallengePursuits = function() { 
      jQuery(".challenge-pursuits-inner").css({"opacity": "1"}); 

      clearTimeout(timeoutHandle); 
    }; 

    var showSportsAndTours = function() { 
      jQuery(".sports-and-tours-inner").css({"opacity": "1"}); 

      clearTimeout(timeoutHandle); 
    }; 

    var showWinterSports = function() { 
      jQuery(".winter-sports-inner").css({"opacity": "1"}); 

      clearTimeout(timeoutHandle); 
    }; 





// military kit 
    jQuery(".military-kit-hover").click(function() {   
     hideAll(); 
     hideServices(); 
     showMilitaryKit(); 
     showServicesDelay(); 
     showNowAvailable(); 
    }); 

// property 
    jQuery(".property-hover").click(function() { 
     hideAll(); 
     hideServices(); 
     showProperty(); 
     showServicesDelay(); 
     showNowAvailable(); 
    }); 

// home contents 
    jQuery(".home-contents-hover").click(function() { 
     hideAll(); 
     hideServices(); 
     showHomeContents(); 
     showServicesDelay(); 
     showNowAvailable(); 
    }); 

// travel 
    jQuery(".travel-hover").click(function() { 
     hideAll(); 
     hideServices(); 
     showTravel(); 
     showServicesDelay(); 
     showNowAvailable(); 
    }); 

// events 
    jQuery(".events-hover").click(function() { 
     hideAll(); 
     hideServices(); 
     showEvents(); 
     showServicesDelay(); 
     showLaunchedShortly(); 
    }); 

// adventurous training 
    jQuery(".adventurous-training-hover").click(function() { 
     hideAll(); 
     hideServices(); 
     showAdventurousTraining(); 
     showServicesDelay(); 
     showLaunchedShortly(); 
    }); 

// personal injury 
    jQuery(".personal-injury-hover").click(function() { 
     hideAll(); 
     hideServices(); 
     showPersonalInjury(); 
     showServicesDelay(); 
     showNowAvailable(); 
    }); 

// challenge pursuits 
    jQuery(".challenge-pursuits-hover").click(function() { 
     hideAll(); 
     hideServices(); 
     showChallengePursuits(); 
     showServicesDelay(); 
     showNowAvailable(); 
    }); 

// sports and tours 
    jQuery(".sports-and-tours-hover").click(function() { 
     hideAll(); 
     hideServices(); 
     showSportsAndTours(); 
     showServicesDelay(); 
     showLaunchedShortly(); 
    }); 

// winter sports 
    jQuery(".winter-sports-hover").click(function() { 
     hideAll(); 
     hideServices(); 
     showWinterSports(); 
     showServicesDelay(); 
     showLaunchedShortly(); 
    }); 
}); 
+3

事情會很多如果您將補充類添加到在類似情況下顯示/隱藏的元素,則更簡單。這樣你只需要一個選擇器。 – Pointy

+3

這是一個選項卡式界面嗎?如果是的話,你可能會將代碼降低到15行左右。 –

回答

1

的一種方法,以加快jQuery是到緩存DOM元素以減少選擇相同元件不止一次,其中每個時間涉及搜索DOM的該元素 - 這可以是慢和資源 - 重量取決於DOM的大小和複雜程度。

根據您的代碼示例:

var insigniaInner = jQuery('.insignia-inner'); //cache the div to a variable 

... 

//use the variable in your code instead of jQuery('.insignia-inner'); 
insigniaInner.css({"opacity":0}); 

,你應該會看到一個顯著的速度增加,如果你使用了很多選擇的。

編輯:另一種方式來加快特別是你的代碼將是給所有你想要隱藏一個通用類的元素,所以你只需要像做:

jQuery('.hide').hide(); 

這也將減少在選擇器呼叫下。另一種方法是定位父元素並淡入或隱藏,而不是單個元素。

0

對於初學者,只要不與任何其他庫衝突,您可以通過用($)替換jQuery來節省下載時間。喜歡的東西

var hideAll = function() { 
      jQuery(".military-kit-inner").css({"opacity": "0"}); 
      jQuery(".property-inner").css({"opacity": "0"}); 
      jQuery(".home-contents-inner").css({"opacity": "0"}); 
      jQuery(".travel-inner").css({"opacity": "0"}); 
      jQuery(".events-inner").css({"opacity": "0"}); 
      jQuery(".adventurous-training-inner").css({"opacity": "0"}); 
      jQuery(".personal-injury-inner").css({"opacity": "0"}); 
      jQuery(".challenge-pursuits-inner").css({"opacity": "0"}); 
      jQuery(".sports-and-tours-inner").css({"opacity": "0"}); 
      jQuery(".winter-sports-inner").css({"opacity": "0"}); 

      jQuery(".now-available").css({"opacity": "0"}); 
      jQuery(".now-available-background").css({"opacity": "0"}); 
      jQuery(".launched-shortly").css({"opacity": "0"}); 
      jQuery(".launched-shortly-background").css({"opacity": "0"}); 
    }; 

而且,你可以通過更換指定一下子

var hideAll = function() { 
      $('[class $="-inner"]').css({"opacity": "0"}); 
      $(':not[class $="-inner"]').css({"opacity": "0"}); 
      //or, replace the line above with 
      $('[class ^="now-available"]').css({"opacity": "0"}); 
      $('[class ^="launched-shortly"]').css({"opacity": "0"}); 
    }; 

這僅僅是例子,但邏輯可能不會爲你而改變什麼尋找(基本上,這個例子中有一個類的所有東西的不透明度都是0)。

+0

'[class $ =「-inner」]' –

+0

我在Wordpress上運行這個,所以我必須調用一個非交叉模式。最簡單的方法是用jQuery替換$。 – pappy

+0

@ A.沃爾夫,對不起,更正。 '^'開頭,其中'$'結束於。 – Sablefoste

0

我的猜測是,當您「同時隱藏」,然後「同時顯示一個」(動畫通常是異步的)時,會導致圖形斷續。我不認爲代碼運行緩慢,這是一個圖形衝突。可能會將一個參數「exceptThisOne」添加到hideAll方法和hideServices方法,並使它們成爲case語句。由你決定你如何處理衝突。

0

有很多事情你可以做,但一個好的開始將是確保你在生產中使用自定義的.min版本的jquery。另一種可能性是合併一些這樣的,例如:

jQuery(".services-inner").css({"opacity": "0"}); 
jQuery(".insignia-inner").css({"opacity": "0"}); 

成爲

jQuery(".insignia-inner, .services-inner").css({"opacity": "0"}); 

jQuery的文檔:多重選擇器(「selector1,選擇器2,選擇器N」) http://api.jquery.com/multiple-selector/

相關問題