admin管理员组文章数量:1315778
I have built a React ponent which is suppose to call the function on window scroll event.
I would like to call the function, "getCards('default'), when the user scroll to 90% of the scroll height.
The ponent looks as shown in below:
class Home extends React.Component {
constructor(props) {
super(props);
this.handleScroll = this.handleScroll.bind(this);
}
ponentDidMount() {
// test
this.props.getCards('default');
window.addEventListener('scroll', this.handleScroll);
}
handleScroll(event) {
console.log('scroll event');
}
I have built a React ponent which is suppose to call the function on window scroll event.
I would like to call the function, "getCards('default'), when the user scroll to 90% of the scroll height.
The ponent looks as shown in below:
class Home extends React.Component {
constructor(props) {
super(props);
this.handleScroll = this.handleScroll.bind(this);
}
ponentDidMount() {
// test
this.props.getCards('default');
window.addEventListener('scroll', this.handleScroll);
}
handleScroll(event) {
console.log('scroll event');
}
Could anyone help me achieve this?
Thank you.
Share Improve this question asked Sep 11, 2017 at 15:11 EunicornEunicorn 6216 gold badges17 silver badges29 bronze badges2 Answers
Reset to default 4You have to work with your ponent, please refer to this code:
class Home extends React.Component {
constructor(props) {
super(props);
}
ponentDidMount() {
// test
this.props.getCards('default');
}
render() {
return (<div onScroll={this.handleScroll}>...</div>)
}
handleScroll(event) {
var heightBound = window.height * 0.8
if (heightBound > window.scrollY) {
// Probably you want to load new cards?
this.props.getCards(...);
}
}
The scroll event must be placed inside your ponent and binded using onScroll evt. The handler will check for percentage of used screen size and will load others elements if the lower bound limit is reached.
I hope this can help :)
I've got a similar problem and nothing with onScroll
work to me.
constructor(props) {
super(props);
window.addEventListener('scroll', this.handleScroll, true);
}
handleScroll = (event) => {
// Your code
}
https://gist.github./koistya/934a4e452b61017ad611
本文标签: javascriptReactjs Call function after scroll eventStack Overflow
版权声明:本文标题:javascript - React.js Call function after scroll event - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741991616a2409218.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论