Created
January 23, 2019 18:00
-
-
Save smashercosmo/47d0502f0805b3567f663271598ee6fe to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const set = new Set() | |
| if (typeof window !== 'undefined') { | |
| window.addEventListener('scroll', () => { | |
| Array.from(set).forEach(fn => fn()) | |
| }) | |
| } | |
| class ItemsListCursorTracker extends Component { | |
| constructor(props) { | |
| super(props) | |
| set.add(this.handleScroll) | |
| } | |
| componentDidMount() { | |
| this.top = this.el.getBoundingClientRect().top | |
| } | |
| componentWillUnmount() { | |
| set.delete(this.handleScroll) | |
| } | |
| handleScroll = () => { | |
| if (!this.el) return | |
| const { top: prevTop } = this | |
| const { top: nextTop } = this.el.getBoundingClientRect() | |
| if ((prevTop > 0 && nextTop <= 0) || (prevTop <= 0 && nextTop > 0)) { | |
| console.log('Верхняя граница элемента пересекла границу вьюпорта...') | |
| if (prevTop > 0 && nextTop <= 0) { | |
| console.log('...и стала не видна пользователю') | |
| } else if (prevTop <= 0 && nextTop > 0) { | |
| console.log('...и стала видна пользователю') | |
| } | |
| this.top = nextTop | |
| } | |
| } | |
| getNode = el => { | |
| this.el = el | |
| } | |
| render() { | |
| return ( | |
| <div | |
| ref={this.getNode} | |
| style={{ width: 1, height: 1, pointerEvents: 'none' }} | |
| /> | |
| ) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment