[英]Conditional addEventListener when div ID is at top of window
I am trying to use addEventListener
when the user scroll
into view <div id="container">
. 当用户
scroll
到视图<div id="container">
时,我试图使用addEventListener
。 I have done so by scroll height but my attempts to addEventListener
when <div>
is on the top of the window. 我是通过滚动高度完成的,但是当
<div>
位于窗口顶部时我尝试addEventListener
。
handleHeaderStuck = () => {
if (this.innerContainer.scrollTop <= 500 && this.state.isStuck === true) {
this.setState({isStuck: false});
}
else if (this.innerContainer.scrollTop >= 500 && this.state.isStuck !== true) {
this.setState({isStuck: true});
}
}
This will setState
when scrolled 500px
. 滚动
500px
时这将是setState
。
How can I replace the condition of 500px
to be set when the user is with id="container"
as the top of the window? 当用户将
id="container"
作为窗口的顶部时,如何替换要设置的500px
的条件? Also replace isStuck
state to isStuckBottom
when the user reaches the bottom of the div. 当用户到达div的底部时,也将
isStuck
状态替换为isStuckBottom
。
The full code is 完整的代码是
constructor(props) {
super(props)
this.state = {
isStuck: false,
}
this.handleHeaderStuck = this.handleHeaderStuck.bind(this)
}
innerContainer = null
componentDidMount () {
this.innerContainer.addEventListener("scroll", this.handleHeaderStuck);
}
componentWillUnmount () {
this.innerContainer.removeEventListener("scroll", this.handleHeaderStuck);
}
handleHeaderStuck = () => {
if (this.innerContainer.scrollTop <= 500 && this.state.isStuck === true) {
this.setState({isStuck: false});
}
else if (this.innerContainer.scrollTop >= 500 && this.state.isStuck !== true) {
this.setState({isStuck: true});
}
}
var atTop = false; var atBotton = false; document.addEventListener("scroll", e => { var elemInfo = document.getElementById("container").getClientRects()[0]; if (parseInt(elemInfo.bottom) >= window.innerHeight) { if (!atBottom){ console.log("I touche the bottom") document.getElementById("information").innerHTML = "mouse reached the bottom"; atBottom = true; } atTop = false; } else if (parseInt(elemInfo.top) <= 0) { if (!atTop) { console.log("Mouse at top") document.getElementById("information").innerHTML = "I touched the top"; atTop = true; } atBottom = false; } else { console.log("I touched the nothing") } });
body { margin: 0px; padding: 0px; border: 0px; } .first, .bottom { width: 100%; height: 200px; background-color: Green; } #container { width: 100%; background-color: Yellow; margin: 0px; padding: 0px; border: 1 px solid black; height: 200px; } #infomation { font-size: 50px; }
<div class="example"> </div> <div id=container> <div id=information></div></div> <div class="bottom"> </div>
You add an "scroll"
onto document
and check if the div is currently at 0 relative to the current viewport. 在
document
上添加"scroll"
并检查div当前是否相对于当前视口为0。 To get the position relative to the current view of the user you can use getBoundingClientRect()
. 要获取相对于用户当前视图的位置,可以使用
getBoundingClientRect()
。 You can implement it in this way: 您可以通过以下方式实现它:
document.addEventListener("scroll", () => {
div = document.querySelector("#container")
if(div.getBoundingClientRect().top == 0) {
//Do what you need to do here, this executes whenever the
//top of the container is at the top of the screen.
}
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.