【问题标题】:Ease scroll to anchor轻松滚动到锚点
【发布时间】:2012-09-30 19:30:57
【问题描述】:

我正在尝试使用 jquery 在单击链接时轻松滚动到锚点。但我不希望它一直滚动到顶部。它必须在距离顶部至少 90px 处停止,这样锚定的 div 就不会落后于我的 90px 高的固定标题菜单栏。有什么想法吗?

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

        $('html, body').stop().animate({
            scrollTop:  $($anchor.attr('href')).offset().top
        }, 1500,'easeInOutExpo');

        event.preventDefault();
    );
});

【问题讨论】:

    标签: jquery jquery-animate bind scrolltop


    【解决方案1】:

    您是否尝试过在顶部位置减去 90 像素,如下所示:

    scrollTop: ($($anchor.attr('href')).offset().top - 90)

    如果可行,您可以动态获取标题菜单栏的高度:

    scrollTop: ($($anchor.attr('href')).offset().top - $('menubar').outerHeight()))

    【讨论】:

    • 是的,它有效。我会摆弄这个。我将与 cmets 一起回来了解它的进展情况。 tnx
    • 太棒了!有用!但我没有使用+,而是使用了-。 scrollTop: ($($anchor.attr('href')).offset().top - $('menubar').outerHeight() 非常感谢。
    • 很高兴它成功了。现在我想起来了:是的,你应该减去。添加一个值会将页面向上移动。
    猜你喜欢
    • 1970-01-01
    • 2017-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-06
    • 1970-01-01
    • 2020-10-17
    相关资源
    最近更新 更多