div ID位于窗口顶部时的条件addEventListener

时间:2018-04-24 20:19:16

标签: javascript reactjs addeventlistener

我想在用户addEventListener进入查看scroll时使用<div id="container">。我是通过滚动高度完成的,但当addEventListener位于窗口顶部时我尝试<div>

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});
    }
}

滚动setState时会500px

当用户将500px作为窗口顶部时,如何替换要设置的id="container"条件?当用户到达div的底部时,也将isStuck状态替换为isStuckBottom

完整的代码是

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});
     }
  }

2 个答案:

答案 0 :(得分:4)

&#13;
&#13;
  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")
      }
    });
&#13;
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;
    }
&#13;
    <div class="example">

    </div>
    <div id=container>
    <div id=information></div></div>
    <div class="bottom">

    </div>
&#13;
&#13;
&#13;

答案 1 :(得分:2)

您将"scroll"添加到document并检查div当前是否相对于当前视口为0。要获取相对于用户当前视图的位置,您可以使用getBoundingClientRect()。您可以通过以下方式实现它:

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.
  }
})