[英]how to show a div after 500px using pure JavaScript?
i have a <button>
calls a function
that scroll to the top of the page, how to appear this button automatically after 800px.我有一个<button>
调用function
滚动到页面顶部,如何在 800px 后自动显示此按钮。 using pure JavaScript Only?仅使用纯 JavaScript?
<button class="button">
GO To UP
</button>
var button = document.querySelector(".button");
button.onclick = function () {
window.scrollTo(0, 0);
};
One approach could be having a hidden class on the button, and in css we can give the visibility of this class to be hidden.一种方法是在按钮上隐藏 class,在 css 中,我们可以隐藏此 class 的可见性。
.hidden {
visibilty: hidden;
}
For handling the scroll functionality we can use this function为了处理滚动功能,我们可以使用这个 function
var button = document.querySelector(".button");
button.onclick = function(){
window.scrollTo(0, 0);
}
window.addEventListener("scroll", () => {
var y = window.scrollY;
if (y >= 500) {
button.classList.remove("hidden");
} else {
button.classList.add("hidden");
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.