简体   繁体   English

如何使用javascript(无jQuery)动画化滚动到页面顶部的动画?

[英]How to animate scrolling to top of page using javascript (no jQuery)?

I would like execute a transition when pressing a button for the webpage to slowly scroll back to the top of the page. 我想在按下网页按钮以缓慢滚动回到页面顶部时执行过渡。 I know how to execute a transition using a change in class, but in this instance, how would i do it? 我知道如何使用类更改来执行过渡,但是在这种情况下,我将如何做?

document.documentElement.scrollTop = 0;
document.body.scrollIntoView({behavior: 'smooth', block: 'start'});

Works also for any other DOM element. 也适用于任何其他DOM元素。 And for horizontal scrolling too. 并且也用于水平滚动。

You can use below code to move the top of the page. 您可以使用以下代码移动页面顶部。

 // When the user scrolls down 20px from the top of the document, show the button window.onscroll = function() { scrollFunction() }; function scrollFunction() { if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) { document.getElementById("myBtn").style.display = "block"; } else { document.getElementById("myBtn").style.display = "none"; } } // When the user clicks on the button, scroll to the top of the document function topFunction() { document.body.scrollTop = 0; document.documentElement.scrollTop = 0; } 
 body { font-family: Arial, Helvetica, sans-serif; font-size: 20px; background: #ffffff; } #myBtn { display: none; position: fixed; bottom: 20px; right: 30px; z-index: 99; background-color: red; color: white; cursor: pointer; padding: 15px; border-radius: 4px; } #myBtn:hover { background-color: #555; } 
 <button onclick="topFunction()" id="myBtn" title="Go to top">Top</button> <div style="padding:30px">Scroll Down</div> <div style="padding:30px 30px 700px">This example demonstrates how to create a "scroll to top" button that becomes visible when the user starts to scroll the page.</div> 

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

相关问题 使用jQuery滚动而不使用animate() - Scrolling with jQuery Without using animate() 我在网站上有一个返回页面顶部的按钮,如何动画化返回顶部的动画? - I have a return to top of the page button in my website, how can I animate scrolling back to the top? 使用jquery将div动画化到页面顶部,但在Chrome中闪烁 - Using jquery to animate a div to the top a page, but it flickers in Chrome 动画{“ Top”}如何在最高位置为0 px时停止div滚动-Jquery - Animate {“Top”} how to stop scrolling div on top position being 0 px - Jquery 使用JavaScript / jQuery从底部到顶部对Canvas Fill()进行动画处理 - Animate Canvas Fill() From Bottom To Top Using JavaScript / jQuery 停止页面滚动到顶部jQuery - Stop page scrolling to the top jQuery 如何使用jQuery或javascript将div放在页面的顶部? - How to put a div on the very top of the page using jQuery or javascript? Wicket 6-使用AjaxCallListener防止jQuery调用滚动到顶部 - Wicket 6 - Prevent jQuery call scrolling page to top using AjaxCallListener 使用 Jquery 在页面刷新时停止 Div 滚动到顶部 - Stop Div scrolling to the top at Page Refresh using Jquery 使用JavaScript滚动时响应动画页面元素[EDIT:Parallax Scrolling] - Responsively animate page elements while scrolling using JavaScript [EDIT: Parallax Scrolling]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM