[英]How do I make the div disappear after scrolling on top
How do I make the circle disappear after scrolling on top? 在顶部滚动后,如何使圆圈消失? I'm able to scroll the circle on top but unable to make it disappear at the same time. 我可以在顶部滚动圆圈,但无法使其同时消失。 I noticed that after the circle has been clicked, it scrolls on to the top, And then on the second click, the circle disappears. 我注意到单击圆圈后,它会滚动到顶部,然后再次单击,圆圈就会消失。
html: 的HTML:
<div class="divOne"></div>
<div class="circleShape" id="circleShapeId">:)</div>
<div class="divTwo"></div>
js: js:
window.addEventListener('scroll', function(e) {
document.getElementById('circleShapeId').style.display = "block"
document.getElementById('circleShapeId').style.position = "fixed"
})
document.getElementById('circleShapeId').addEventListener('click', function(){
document.getElementById('circleShapeId').style.display = "none"
window.location = '#'
})
css: CSS:
.circleShape{
height: 50px;
background-color: red;
width: 50px;
border-radius: 30px;
text-align: center;
border: 2px solid white;
cursor: pointer;
float: left;
margin-left: 30px;
position: static;
display: none;
}
.divOne {
height: 300px;
background-color: yellow;
}
.divTwo{
height: 300px;
background-color: pink;
}
Check the scroll position in the scroll event in an if statement. 在if语句中检查滚动事件中的滚动位置。
window.addEventListener('scroll', function(e) {
// 10 is a threshold, probably in pixels
if (document.body.scrollTop < 10)
document.getElementById('circleShapeId').style.display = "none";
else
document.getElementById('circleShapeId').style.display = "block";
});
Like the other answer, it would seem that by setting the window.location on click, you're effecting a scroll, thus keeping #circleShapeId
visible. 像其他答案一样,通过单击设置window.location似乎可以实现滚动,从而保持#circleShapeId
可见。 Presumably you only want the #circleShapeId
to appear when you begin scrolling down and you want it to disappear if you go back to the top. 大概您只希望在开始向下滚动时显示#circleShapeId
,并且如果返回顶部则希望它消失。
window.addEventListener('scroll', function(e) {
if (this.oldScroll < this.scrollY){
document.getElementById('circleShapeId').style.display = "block"
document.getElementById('circleShapeId').style.position = "fixed"
}
else if(this.scrollY == 0){
document.getElementById('circleShapeId').style.display = "none"
document.getElementById('circleShapeId').style.position = "static"
}
this.oldScroll = this.scrollY;
})
document.getElementById('circleShapeId').addEventListener('click', function(){
window.location = '#'
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.