简体   繁体   English

Javascript滚动到顶部,然后滚动到底部

[英]Javascript scroll to the top and scroll to the bottom

I try to create a javascript code to do that: When the user scrolls down 500px from the top of the document, show the button and when the user click this button he go to the top of the page. 我尝试创建一个javascript代码来做到这一点:当用户从文档顶部向下滚动500px时,显示按钮,当用户单击此按钮时,他将转到页面顶部。

when the user don't scroll down and he is in the top of the page it's appear a button to go to the bottom to the page when the user click this button he go to the bottom of the page and the button disapear 当用户不向下滚动并且位于页面顶部时,当用户单击此按钮时,将显示一个按钮以转到页面的底部,该用户将转到页面底部并且该按钮消失

 window.onscroll = function() { scrollFunction() }; window.onscroll = function() { downFunction() }; function scrollFunction() { if (document.body.scrollTop > 500 || document.documentElement.scrollTop > 500) { document.getElementById("myBtn").style.display = "block"; } else { document.getElementById("myBtn").style.display = "none"; } } function downFunction() { if (document.body.scrollTop = 0 || document.documentElement.scrollTop = 0) { document.getElementById("myBottomBtn").style.display = "none"; } else { document.getElementById("myBottomBtn").style.display = "block"; } } // When the user clicks on the button, scroll to the top of the document function topFunction() { document.body.scrollTop = 0; // For Chrome, Safari and Opera document.documentElement.scrollTop = 0; // For IE and Firefox } function bottomFunction() { document.body.scrollTop = 100; // For Chrome, Safari and Opera document.documentElement.scrollTop = 100; // For IE and Firefox } 
 #myBtn { display: none; position: fixed; bottom: 20px; right: 30px; z-index: 99; border: none; outline: none; background-color: #333; color: white; cursor: pointer; padding: 10px; border-radius: 10px; } #myBtn:hover { background-color: #555; } #myBottomBtn { display: block; position: fixed; right: 30px; z-index: 99; border: none; outline: none; background-color: #333; color: white; cursor: pointer; padding: 10px; border-radius: 10px; } #myBottomBtn:hover { background-color: #555; } 

The problem is in the top button when i click this button it didn't go to the bottom and when i scroll it still appear 问题是在顶部按钮中,当我单击此按钮时,它没有移至底部,并且在我滚动时仍然出现

but the button in the button work fine when i scroll for 500 px the button appear and when i click this button i go to the top 但是当我滚动500 px时,该按钮中的按钮工作正常,当我单击该按钮时,该按钮将显示到顶部

can you help me to solve the problem of the first button ? 您能帮我解决第一个按钮的问题吗? to do what i want 做我想做的

You write twice on the same event window.onscroll , only downFunction() will work. 您在同一事件window.onscroll上写了两次,只有downFunction()起作用。 You should use object.addEventListener("scroll", myScript); 您应该使用object.addEventListener("scroll", myScript); EventListener Doc . EventListener文档

 window.addEventListener("scroll", function() { scrollFunction(); downFunction(); });; function scrollFunction() { console.log(document.documentElement.scrollTop); if (window.scrollY > 200) { document.getElementById("myBtn").style.display = "block"; } else { document.getElementById("myBtn").style.display = "none"; } } function downFunction() { if (window.scrollY === 0) { document.getElementById("myBottomBtn").style.display = "none"; } else { document.getElementById("myBottomBtn").style.display = "block"; } } // When the user clicks on the button, scroll to the top of the document function topFunction() { document.body.scrollTop = 0; // For Chrome, Safari and Opera document.documentElement.scrollTop = 0; // For IE and Firefox } function bottomFunction() { window.scrollY = 220; // For Chrome, Safari and Opera document.documentElement.scrollTop = 220; // For IE and Firefox } 
 <input type="button" value="bottom" onclick="bottomFunction()" /> <br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>&nbsp; <input type="button" id="myBtn" value="myBtn" /> <input type="button" id="myBottomBtn" value="myBottomBtn" /> <br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>&nbsp; <input type="button" value="top" onclick="topFunction()" /> 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM