【问题标题】:jQuery offset fixed header using scrollTop使用 scrollTop 的 jQuery 偏移固定标题
【发布时间】:2012-09-27 12:06:36
【问题描述】:

我正在尝试使用 jQuery 创建一个垂直平滑滚动的网站。我正在使用此 JavaScript 和本教程 Smooth Scrolling Website 创建站点。

但是我在使用固定标题时遇到了问题,滚动工作正常,但它出现在相关 div 的中间,因为 div 与页面顶部对齐,而不仅仅是在固定标题下方我也想要。

我尝试向 scrollTop 添加偏移量,但页面上的一切都乱了套,出现在固定标题上方的东西等等。真的只是一个页面混搭。如果有人能提供任何启示,将不胜感激。

$(function() {  
        $('ul.menu a').bind('click',function(event){
                    var $anchor = $(this);

                    $('html, body').stop().animate({
                        scrollTop: $($anchor.attr('href')).offset().top
                    }, 1500,'easeInOutExpo');
                    /*
                    if you don't want to use the easing effects:
                    $('html, body').stop().animate({
                        scrollTop: $($anchor.attr('href')).offset().top
                    }, 1000);
                    */
                    event.preventDefault();
                });
            });

我在 StackOverflow (+ $('.fixedheader').outerHeight()) 上找到了这段代码,并将其添加到我的代码中(在 scrollTop: $($anchor.attr('href')).offset().top 之后)它确实有效,但似乎产生了相反的效果。有人知道为什么吗?

【问题讨论】:

    标签: javascript jquery html css


    【解决方案1】:

    我已经解决了,

    + $('.fixedheader').outerHeight()
    

    应该是

    - $('.fixedheader').outerHeight()
    

    我太傻了,无论如何都欢呼。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多