-
-
Save koistya/934a4e452b61017ad611 to your computer and use it in GitHub Desktop.
import React from 'react'; | |
let lastScrollY = 0; | |
let ticking = false; | |
class App extends React.Component { | |
componentDidMount() { | |
window.addEventListener('scroll', this.handleScroll, true); | |
} | |
componentWillUnmount() { | |
window.removeEventListener('scroll', this.handleScroll); | |
} | |
nav = React.createRef(); | |
handleScroll = () => { | |
lastScrollY = window.scrollY; | |
if (!ticking) { | |
window.requestAnimationFrame(() => { | |
this.nav.current.style.top = `${lastScrollY}px`; | |
ticking = false; | |
}); | |
ticking = true; | |
} | |
}; | |
render() { | |
return ( | |
<div> | |
<nav ref={this.nav}> | |
</nav> | |
<div> | |
); | |
} | |
} | |
export default App; |
Hi @j6k4m8 , this.handleScroll = this.handleScroll.bind(this);
Work very well with the traditionnal method declaration Syntax
constructor(props) {
super(props);
this.handleScroll = this.handleScroll.bind(this);
}
handleScroll(event) {
console.log('the scroll things', event)
};
In case you're interested by making you're constructor more dry you could also use the ES6 fat arrow syntax.
constructor(props) {
super(props);
}
handleScroll = (event) => {
console.log('the scroll things', event)
};
Fat arrow automatically bind this to the method, allowing shorter constructor. Especially when you have a lot of methods.
@jylopez, worked for me too, thanks!
@jylopez worked perfectly for me. thank you so much!
@ffxsam your solution works for me ~thank you a lot
componentDidMount() {
// $FlowFixMe
const node = this.hscroll;
if (node) {
node.addEventListener('scroll', e => {
window.requestAnimationFrame(() => {
this.setState({ fixed: node.scrollTop > 10 });
});
});
}
}
componentWillUnmount() {
// $FlowFixMe
this.hscroll.removeEventListener('scroll');
}
render() {
return (
// $FlowFixMe
<Container innerRef={comp => (this.hscroll = comp)}>
this is my solution :
componentDidMount = () => {
// hadling cover parallax
window.addEventListener('scroll', this.handleOnScroll)
}
componentWillUnmount = () => {
window.removeEventListener('scroll', this.handleOnScroll)
}
// handle onScroll event
handleOnScroll = () => {
// console.log(this.coverRef.current.height)
const wScroll = window.scrollY
this.coverGradRef.current.style.cssText = `transform: translate(0px , -${wScroll/40}%)`
// console.log(window.scrollY)
}
coverGradRef
is a ref that is in my component's constructor and i pass that to child component, it works pretty smooth so far
and one more thing
after this.coverGradRef.current.style
you can add cssText
and add what every property you want like pure css , if you don't like that you can use something like this.coverGradRef.current.style.top = '10px'
and instead of top
you can put any valid css property
componentDidMount(){
document.addEventListener('scroll', this.handleScroll, true);
}
handleScroll = () => {
console.log('scrolling ...');
}
Note however that input events and animation frames are fired at about the same rate and therefore the optimization below is often unnecessary.
https://developer.mozilla.org/en-US/docs/Web/Events/scroll#Example
Hi @jylopez This is what I needed. Thanks! Appreciated the ES6 tip from @EEtancelin. I like not having to add the bindings in the constructor. Good stuff!
Good stuff @jylopez. Has anyone had any issue with the scroll event not firing on mobile. The touchmove event is what native js uses and I see react has onTouchMove but that is react native specific.
@jylopez Thank you so much.. it is working fine
@ffxsam solution worked fine for me too.
@jylopez @EEtancelin best solutions, thx!
solved my problem thanks
If anyone is thinking they are going crazy that it's not working for them, while everyone else is piling on saying it does, try this:
componentDidMount() {
window.addEventListener('scroll', this.handleScroll, true);
}
Note, the third argument of "true".
https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener
@paintedbicycle thank you! now it works.
If anyone is thinking they are going crazy that it's not working for them, while everyone else is piling on saying it does, try this:
componentDidMount() { window.addEventListener('scroll', this.handleScroll, true); }
Note, the third argument of "true".
https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener
Worked for me!
window.addEventListener('scroll', this.handleScroll, true);
Thanks!
@paintedbicycle Thank you!! This worked for me
Thanks this is exactly what I needed to do for a ShadowDOM supported div that used onScroll
.
Turns out ShadowDOM does not support scroll
listener, it's essentially blocked.
What I did was:
const listNode = ReactDOM.findDOMNode(this.list);
listNode.addEventListener('scroll', this.handleScroll);
and it worked! 🎉
If anyone is thinking they are going crazy that it's not working for them, while everyone else is piling on saying it does, try this:
componentDidMount() { window.addEventListener('scroll', this.handleScroll, true); }
Note, the third argument of "true".
https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener
Worked for me
thank you very much @paintedbicycle
This worked for me:
constructor(props) { super(props); this.handleScroll = this.handleScroll.bind(this); } componentDidMount() { window.addEventListener('scroll', this.handleScroll); }; componentWillUnmount() { window.removeEventListener('scroll', this.handleScroll); }; handleScroll(event) { console.log('the scroll things', event) };
your solution worked for me. Thank you for this
@raheemazeezabiodun Note, that if you use an arrow function syntax for declaring custom handlers, you won't need to bind them to this
inside of a constructor function:
constructor(props) {
supert(props);
this.handleScroll = this.handleScroll.bind(this);
}
handleScroll(event) { ... };
vs
handleScroll = (event) => { ... };
Is anyone having issues with the event returning as undefined?
@jylopez That a perfect Simple React Solution. +1 +1 +1 Thanks