0

快速提问。我已经实现了一个滚动到方法,以从导航中滚动到具有该特定 ID 的我的 div。由于我的导航栏固定它的滚动到很远..我如何偏移以便它滚动到正确的位置..上传一些图像和js代码..

希望你能帮助...

这就是我得到的...

在此处输入图像描述

这就是我想要的......

在此处输入图像描述

.js 文件..

    $('a[href^="#"]').on('click',function (e) {
    e.preventDefault();
    var target = this.hash,
    $target = $(target);
    $('html, body').stop().animate({
        'scrollTop': $target.offset().top
    }, 900, 'swing', function () {
        window.location.hash = target;
    });
  });
4

1 回答 1

1

我曾经遇到过同样的问题,所以我使用了这个功能:

var scrolldown = function(item, tuner) {
            $('html, body').animate({
                scrollTop: $(item).offset().top - tuner
            }, 2000, 'easeOutCirc');
    }

其中 item 是要滚动到的 id,而 tuner 是调整滚动结束位置的像素值。

你可以像这样使用它:

$('#id-of-nav').click(function(e) {
    e.preventDefault();
    scrolldown("#id-of-target", "150")
});

这是您可以在其中看到它的网站:https ://www.tabapp.com/

当然你可以删除动画。

于 2013-05-22T15:44:31.193 回答