【问题标题】:Smooth scroll to anchor after loading new page加载新页面后平滑滚动到锚点
【发布时间】:2015-07-29 09:38:26
【问题描述】:

我想导航到新页面上的锚点,但我希望页面加载到顶部,然后立即平滑滚动到相关锚点。这个可以吗?

我是 Javascript 的新手。

这是我目前用于在当前页面内平滑滚动的 js。 我只是在链接上应用了一类“滚动”。

非常感谢!

<script>
$(function(){
  $('.scroll').on('click',function(e) {
    e.preventDefault();
    $('html, body').animate({ scrollTop: $($(this).attr('href')).offset().top + 'px' }, 1000, 'swing');
  });
});
</script>

【问题讨论】:

    标签: jquery scroll fragment-identifier


    【解决方案1】:

    由于浏览器会自动检测到hash 并将您带到该位置...
    我突然想到,您可以先将滚动位置重置为 0,然后再进行平滑滚动。

    类似...

    // to top right away
    if ( window.location.hash ) scroll(0,0);
    // void some browsers issue
    setTimeout( function() { scroll(0,0); }, 1);
    
    $(function() {
    
        // your current click function
        $('.scroll').on('click', function(e) {
            e.preventDefault();
            $('html, body').animate({
                scrollTop: $($(this).attr('href')).offset().top + 'px'
            }, 1000, 'swing');
        });
    
        // *only* if we have anchor on the url
        if(window.location.hash) {
    
            // smooth scroll to the anchor id
            $('html, body').animate({
                scrollTop: $(window.location.hash).offset().top + 'px'
            }, 1000, 'swing');
        }
    
    });
    

    编辑:将scroll(0,0)移出$(function(){...});以防止闪烁。
    此外,还添加了带有工作示例的片段。
    在全屏查看时效果最佳

            html, body {
                margin: 0;
                padding: 0;
            }
            .hidden-code {
                display: none;
                visibility: hidden;
                opacity: 0;
            }
            .header {
                background-color: #ccc;
                padding: 5px;
            }
            .header li {
                padding: 5px;
                margin: 5px;
                display: inline-block;
            }
            .articles > div {
                border: 1px solid;
                margin: 10px;
                padding: 250px 50px;
                background-color: #ccc;
            }
            div:before {
                content: attr(id);
            }
            .footer {
                text-align: center;
            }
        <div class="header">
            <ul>
                <li>page header title/navbar</li>
                <li><a class="scroll" href="#text-1">#text-1</a></li>
                <li><a class="scroll" href="#text-2">#text-2</a></li>
                <li><a class="scroll" href="#text-3">#text-3</a></li>
                <li><a class="scroll" href="#text-4">#text-4</a></li>
                <li><a class="scroll" href="#text-5">#text-5</a></li>
                <li><a class="scroll" href="#text-6">#text-6</a></li>
                <li><a class="scroll" href="#text-7">#text-7</a></li>
                <li><a class="scroll" href="#text-8">#text-8</a></li>
            </ul>
        </div>
    
        <div class="container">
    
            <div class="content">
    
                <div class="articles">
                    <div id="text-1"></div>
                    <div id="text-2"></div>
                    <div id="text-3"></div>
                    <div id="text-4"></div>
                    <div id="text-5"></div>
                    <div id="text-6"></div>
                    <div id="text-7"></div>
                    <div id="text-8"></div>
                </div>
    
            </div>
    
            <div class="footer">company &copy; 2015</div>
    
        </div>
    
        <div class="hidden-code">
    
            <script type="text/javascript" src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
            <script type="text/javascript">
    
                // to top right away
                if ( window.location.hash ) scroll(0,0);
                // void some browsers issue
                setTimeout( function() { scroll(0,0); }, 1);
    
                // any position
                $(function() {
                    // your current click function
                    $('.scroll').on('click', function(e) {
                        e.preventDefault();
                        $('html, body').animate({
                            scrollTop: $($(this).attr('href')).offset().top + 'px'
                        }, 1000, 'swing');
                    });
                    // *only* if we have anchor on the url
                    if(window.location.hash) {
                        // smooth scroll to the anchor id
                        $('html, body').animate({
                            scrollTop: $(window.location.hash).offset().top + 'px'
                        }, 1000, 'swing');
                    }
                });
            </script>
    
        </div>

    【讨论】:

    • 滚动在同一页面上完美运行,但我似乎仍然在外部链接上获得初始闪光。它在前几次有效,但最终开始这样做。奇怪 - 我一定做错了什么!
    • 在本地试试这个代码,如果你发现同样的问题,告诉我。您使用什么操作系统和浏览器? pastebin.com/raw.php?i=3JbPp9wn
    • @gmo 你能解释一下为什么当锚点存在时你会滚动(0,0)两次吗?为什么在检查哈希时不考虑超时?
    • @gmo 在我删除了前 4 行代码后它起作用了。谢谢!
    • @JaySmoke 当然,只需将您想要的任何像素求和scrollTop 函数,例如 ie。 150 -> ...offset().top + 150 + 'px'
    猜你喜欢
    • 1970-01-01
    • 2015-07-26
    • 1970-01-01
    • 2022-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多