页面加载后菜单跳转

时间:2012-04-12 19:59:56

标签: javascript jquery html css

问题是我的菜单在加载新页面时跳转(所选菜单项应该完全居中。菜单使用代码:

$(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 - &gt; Google网络字体)时,它不会发生。

1 个答案:

答案 0 :(得分:0)

这个jQuery语法在DOM准备就绪时运行代码。

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

在某些情况下,您需要在代码运行之前加载整个页面(以及它的图像依赖项)。在这些情况下,我们有以下几点:

$(window).load(function() { //code })
相关问题