[英]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">
Alternatives备择方案
If you want to use JS you can如果你想使用JS ,你可以
document.getElementById("body").scrollIntoView()
Or JQuery或JQuery
$(window).scrollTo(0, {duration:0})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.