【问题标题】:Smooth scrolling to anchor on different page平滑滚动以锚定在不同页面上
【发布时间】:2016-01-31 09:33:42
【问题描述】:

我在 Google 上搜索了如何平滑滚动到不同页面上的锚点。我已经尝试了一些方法,但是,它们似乎不适用于我已经编写的代码,该代码可以在一页上平滑滚动。

基本上,我已将我的联系人部分锚定在索引页面上,并希望我的其他页面在我单击导航栏上的联系人链接时平滑滚动到该部分。但目前它所做的只是直接跳到锚点而不从顶部滚动。

这是我的代码:

$(document).ready(function() {
    $('a[href^="#"]').not("#quote-carousel a, ul.nav-pills a, div#accordian a").on("click", function(a) {
        a.preventDefault();
        var b = this.hash,
            c = $(b);
        $("html, body").stop().animate({
            scrollTop: c.offset().top - 50
        }, 900, "swing", function() {
            window.location.hash = b
        })
    })
});

【问题讨论】:

  • 您能否用最少的代码创建一个 jsfiddle 来说明您的问题? animate scrollTop 应该可以解决这个问题 AFAIK
  • 我目前没有发现您的代码存在问题。它正在做它应该做的事情:jsfiddle.net/536Lmzuv 唯一的问题可能是您是否有以前的 JS 错误或未包含 jQuery 脚本文件。
  • console 中是否有任何错误?
  • 是的,它适用于单页滚动,但是当我在另一个页面上并单击导航栏上的联系人链接时,它会直接跳转到索引页面上的锚点,而无需从顶部滚动。

标签: javascript jquery smooth-scrolling


【解决方案1】:

您可以在哈希更改和页面加载时开始滚动。因此,您可以滚动到目标元素并创建深层链接。

所以你的方法和我的主要区别是,首先更改哈希(或不阻止它)并监听哈希更改事件。

这就是我的做法:

JavaScript:

$(document).ready(function() {
    // check for hash when page has loaded
    if (getHash() != null) {
        checkForScrolling();
    }
});

// listen for hash change event here
window.onhashchange = function() {
    checkForScrolling();
};

// return hash if so or null if hash is empty
function getHash() {
    var hash = window.location.hash.replace('#', '');
    if (hash != '') {
        return hash;
    } else {
        return null;
    }
}

// this function handles your scrolling
function checkForScrolling() {
    // first get your element by attribute selector
    var elem = $('[data-anchor="' + getHash() + '"]');

    // cheeck if element exists
    if (elem.length > 0) {
        $('html, body').stop().animate({
            scrollTop: elem.offset().top
        }, 300);
    }
}

工作 HTML 示例:

<!DOCTYPE html>
<html>
    <head>
        <title>Smooth Scrolling</title>
        <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.1.4.js" type="text/javascript"></script>
        <script type="text/javascript">
            $(document).ready(function() {
                // check for hash when page has loaded
                if (getHash() != null) {
                    checkForScrolling();
                }
            });
            // check for hash when hash has changed
            window.onhashchange = function() {
                checkForScrolling();
            };
            // return hash if so or null if hash is empty
            function getHash() {
                var hash = window.location.hash.replace('#', '');
                if (hash != '') {
                    return hash;
                } else {
                    return null;
                }
            }
            // this function handles your scrolling
            function checkForScrolling() {
                // first get your element by attribute selector
                var elem = $('[data-anchor="' + getHash() + '"]');
                // cheeck if element exists
                if (elem.length > 0) {
                    $('html, body').stop().animate({
                        scrollTop: elem.offset().top
                    }, 300);
                }
            }
        </script>
        <style type="text/css">
            body { font-family: Helvetica }
            section { position: relative; margin-bottom:10px; padding:20px; height: 300px; background-color: #eee; }
            section a { display:block; position: absolute; bottom: 10px; }
        </style>
    </head>
    <body>
        <div>
            <h1 data-anchor="start">Smooth Scrolling</h1>
            <ul>
                <li><a href="#1">Scroll to Anchor 1</a></li>
                <li><a href="#2">Scroll to Anchor 2</a></li>
                <li><a href="#3">Scroll to Anchor 3</a></li>
            </ul>
            <section>
                <h2 data-anchor="1">First Anchor</h2>
                <a href="#start">Back to top</a>
            </section>
            <section>
                <h2 data-anchor="2">Second Anchor</h2>
                <a href="#start">Back to top</a>
            </section>
            <section>
                <h2 data-anchor="3">Third Anchor</h2>
                <a href="#start">Back to top</a>
            </section>
        </div>
    </body>
</html>

【讨论】:

    【解决方案2】:

    考虑到html如下,在点击锚标签时添加以下代码

    <div class="class_name"><a href="#contact">Contact</a></div>
    

    为上面的html添加代码

    $(document).on('click', '.class_name a', function(e) {
                e.preventDefault();
                var target = $(this).attr('href'), $target = $(target); {
                    $('html, body').stop().animate({
                        'scrollTop' : $target.offset().top - 50
                    }, 900, 'swing', function() {
                        window.location.hash = target;
                    });
                }
    });
    

    上面的 -50 值根据标题高度而变化。

    【讨论】:

    • 我一定遗漏了这种方法的某些部分,因为添加此 javascript 只会使来自不同页面的链接无济于事。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-29
    • 2015-08-11
    相关资源
    最近更新 更多