简体   繁体   English

为什么我滚动到顶部总是动画? 我怎样才能立即到达顶部?

[英]Why is my scroll to top always animated? And how can I instantly travel to top?

So I have this heavily animated website made and every time I apply "Back to top" code on my button, it always animates scrolling through the entire page until it reaches the top.所以我制作了这个非常动画的网站,每次我在我的按钮上应用“返回顶部”代码时,它总是动画滚动整个页面直到它到达顶部。

I want it to instantly take them to the top without any fancy scroll up effects which is what I am getting..我希望它能立即将它们带到顶部,而没有任何花哨的向上滚动效果,这正是我得到的……

I have tried multiple scripts and they all get an animation for some reason (even if I search for script code that is supposedly not animated).我尝试了多个脚本,但出于某种原因它们都得到了 animation(即使我搜索了据说不是动画的脚本代码)。

this is the current script I am using: (I have tried multiple but they all resulted in the same animated scroll up effect)这是我正在使用的当前脚本:(我尝试了多次,但它们都产生了相同的动画向上滚动效果)

<!-- Back to top -->
    <script>
        var btn = $('#BackTop');
        $(window).scroll(function() {
            if ($(window).scrollTop() > 300) {
                btn.addClass('show');
            } else {
                btn.removeClass('show');
            }
        });

        btn.on('click', function(e) {
            // e.preventDefault();
            window.scrollTo({
                top: 0,
            })
        });
    </script>

These are the other scripts included in the page, could have any of these been effecting my scroll to top code perhaps?这些是页面中包含的其他脚本,这些脚本中的任何一个可能会影响我滚动到顶部代码吗?

<!-- Scripts -->
    <script type="text/javascript" src="./my-assets/js/jquery.js"></script>
    <script type="text/javascript" src="./my-assets/js/bootstrap.min.js"></script>
    <script type="text/javascript" src="./assets/js/lib/gsap3/gsap.min.js"></script>
    <script type="text/javascript" src="./my-assets/js/scroll.js"></script>

    <!-- .cd-vertical-nav -->
    <script src="./my-assets/js/velocity.min.js"></script>
    <script src="./my-assets/js/velocity.ui.min.js"></script>
    <script src="./my-assets/js/main.js"></script>

    <!-- fade effect js -->
    <script src="./my-assets/js/aos/aos.js"></script>

Would appreciate any tips with what exactly am I doing wrong, because I really want it to be instant to top and not animated because the page I am working on is huge in a sense where I dont want the costumer to see the entire page scroll back up..将不胜感激关于我到底做错了什么的任何提示,因为我真的希望它立即到顶部而不是动画,因为我正在处理的页面在某种意义上是巨大的,我不希望客户看到整个页面向后滚动向上..

It is just a matter of css properties on the document.这只是文档上 css 属性的问题。

You can either specify the behaviour for the particular operation您可以指定特定操作的行为

window.scrollTo({ top:0, left:0, behavior: "instant"}); // using built in
$(window).scrollTo(0, {duration:0}); // using jquery

Other than that you could try changing the css properties of the document除此之外,您可以尝试更改文档的 css 属性

html {
    scroll-behavior: auto;
}

For that you don't even need JS.为此,您甚至不需要 JS。 You can set empty or hidden div in top of you page and give it some id .您可以在页面顶部设置空的或隐藏的 div 并给它一些id And make your btn be a tag that links to it并使您的btn成为链接到它a标签

<a href="#id">

Here is the example 是例子

Alternatives备择方案

If you want to use JS you can如果你想使用JS ,你可以

document.getElementById("body").scrollIntoView()

Or JQueryJQuery

$(window).scrollTo(0, {duration:0})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使Chrome扩展程序窗口始终位于顶部 - How can I make my Chrome Extension window always on top 在AngularJS中始终滚动到顶部 - Scroll always on top in AngularJS 使用 CSS 设置平滑时立即滚动到顶部 - Scroll instantly to the top when smooth is set with CSS 在具有动画滚动功能的 ThreeJS 画布中,如何防止向上滚动超出页面顶部? - In a threeJS canvas with an animated scroll function, how do I prevent scrolling up beyond the top of the page? 滚动到顶部,动画滚动到#page链接冲突 - Scroll to top and animated scroll to #page link conflict 为什么我的数组项被添加到顶部,而滚动条停留在顶部? - Why are my array items getting added to the top, and the scroll is staying at the top? 在模态启动之前,如何使用“简单模态”滚动到页面顶部? - How can I use Simple Modal to scroll to the top of my page just before the modal launches? 如何在滚动到顶部按钮的顶部已经有图标的文本中添加文本 - How can I add text in my scroll to top button which already has an icon in it 如何使导航栏平滑滚动到页面顶部,请单击 - how can I make my navbar scroll smoothly to the top on page click 如何通过在Coldfusion代码中放置一个函数来使所有链接滚动到页面顶部 - How can I get all my links to scroll to the top of the page by placing one function in a coldfusion code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM