【问题标题】:How can I scroll to a specific part of a webpage? [duplicate]如何滚动到网页的特定部分? [复制]
【发布时间】:2020-01-21 01:51:07
【问题描述】:

我正在制作一个网站,我需要一个导航项来向下滚动网站。 我已经尝试过一种简单的方法,那就是使 href 链接到一个元素并提供滚动行为:smooth;。 这个解决方案的问题是我有一个很大的标题,所以我需要滚动在实际元素之前停止一点。 所以,我想知道一种滚动到网页上特定点的方法,而不是实际的元素。 谢谢

【问题讨论】:

标签: html


【解决方案1】:

好的,这是我使用 JavaScript(JQuery) 进行平滑滚动监视的方式,也许它可以帮助你。

$(document).ready(function () {
$('body').scrollspy({
    // Your navbar
    target: ".navbar",
    offset: 200
});
 // links in navbar
$("#myNavbar a").on('click', function (event) {
    if (this.hash !== "") {
        event.preventDefault();
        var hash = this.hash;
        $('html, body').animate({
            scrollTop: $(hash).offset().top
        }, 800, function () {


            window.location.hash = hash;
        });
    }
  });
});

【讨论】:

    猜你喜欢
    • 2019-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-08
    • 1970-01-01
    • 2014-06-22
    • 2023-01-23
    相关资源
    最近更新 更多