2012-04-12 107 views
2

問題是,當新的頁面加載(選定的菜單項應完全集中的菜單是http://maartenbrakkee.nl/portfolio/和使用代碼我的菜單跳躍:菜單頁面加載後跳轉

$(document).ready(function(){ 

    /* Menu klik animatie II */ 
    $("nav#menu a").click(function(e) { 
     e.preventDefault(); 
     var dit = $(this); 
     $(this).parent().siblings().removeClass("current_page_item"); 
     $(this).parent().addClass("current_page_item"); 
     geselecteerd = $('#menu li.current_page_item').sumOuterWidth(true); 
     voor = $('#menu li.current_page_item').prevAll().sumOuterWidth(true); 
     vader = $('#menu').parent().width(); 
     invoegen = parseInt(((vader-geselecteerd)/2)-voor); 
     $('nav#menu').prev().animate({ 
      width: invoegen + 'px', 
     }, { 
      duration: 400, 
      specialEasing: { 
       width: 'linear', 
       height: 'easeOutBounce' 
     }, 
     complete: function() { 
      $('#content').fadeTo('fast', 0.01, function() { 
       document.location = $(dit).attr('href'); 
      }); 
     } 
     }); 
    }); 

    /* Menu uitlijning: geselecteerde item centraal */ 
    $.fn.sumOuterWidth = function(useMargins) { 
     var sum = 0; 
     this.each(function() { 
     sum += $(this).outerWidth(useMargins); 
     }); 
     return sum; 
    }; 

    geselecteerd = $('#menu li.current_page_item').sumOuterWidth(true); 
    voor = $('#menu li.current_page_item').prevAll().sumOuterWidth(true); 
    vader = $('#menu').parent().width(); 
    invoegen = parseInt(((vader-geselecteerd)/2)-voor); 
    $('#menu').before("<div style='width:" + invoegen + "px;float:left'>&nbsp;</div>"); 
}); 

我不能真的認爲我做錯了;寬度(從菜單之前的div)應該與動畫的寬度(在新頁面加載之前)和新加載的頁面上的寬度相同。當我使用不同的字體(Varela Round - > Google網絡字體)時,它不會發生。

+1

也許試着在你的代碼中包裝它? $(window).load(function(){// code}); – hagope 2012-04-13 00:45:50

+0

off off!日Thnx – Waarten 2012-04-14 10:21:01

回答

0

這個jQuery語法在DOM準備就緒時運行代碼。

$(document).ready(function(){ } 

在某些情況下,您希望在代碼運行之前加載整個頁面(以及它的圖像依賴關係)。在這些情況下,我們有以下幾種:

$(window).load(function() { //code })