简体   繁体   English

如何使用纯 JavaScript 在 500px 后显示 div?

[英]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.

相关问题 JavaScript命令:»如果div宽度&lt;500px ...« - JavaScript command: »If div width <500px …« 段落中的段落达到500像素的高度后,如何将div的高度(500像素)更改为自动? - How to change a div's height (500px) to auto after a paragraph inside it reaches 500px in height? 如何使用Javascript在视口下方获取所有500px的HTML元素 - How to get all HTML elements 500px below viewport using Javascript 用小于500像素的.prependto排列元素,当大于500像素时如何撤消? - Arranging elements with .prependto at less <500px, how to undo when >500px? 如何使用Meteor.js通过oauth 1.0向500px api进行身份验证的请求 - How to make an authenticated request to 500px api via oauth 1.0 using Meteor.js jQuery或Javascript:如何查找图像大于500像素且小于2000像素的所有html元素? - Jquery or Javascript: How to find all html elements which has image more than 500px and less than 2000px? Shift MapBox 500px - Shift MapBox 500px 如何为所有 3 个单选按钮保留 500px 的左边距 - How to leave left margin of 500px for all 3 radio buttons CSS 媒体查询,如果高度大于500px,如何在div 下添加阴影? - CSS media query, how can I add a shadow under a div if it's height is bigger than 500px? 使用javascript在700px之后显示div后,如何隐藏它? - after using javascript to show a div after 700px how do i hide it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM