Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. import React, { Fragment, useEffect, useState } from 'react';
  2. import PropTypes from 'prop-types';
  3.  
  4. function Parallax(props) {
  5. const [YPos, setYPos] = useState(0);
  6. const { uppermodulevisible, addclass } = props;
  7.  
  8. function handleScroll() {
  9. if (uppermodulevisible === 1) {
  10. console.log("you were module visible", uppermodulevisible);
  11. let pos = Math.floor(window.scrollY * -1.5);
  12. // console.log('on scroll scroll scroll', pos);
  13. window.requestAnimationFrame(() => setYPos(pos));
  14. }
  15. }
  16.  
  17. function setUp() {
  18. window.addEventListener('scroll', () => {
  19. handleScroll();
  20. }, false);
  21. }
  22.  
  23. function tearDown() {
  24. window.removeEventListener('scroll', handleScroll, false);
  25. }
  26.  
  27. useEffect(() => {
  28. setUp();
  29. return (() => tearDown());
  30. }, [uppermodulevisible]);
  31.  
  32. return (
  33. <Fragment>
  34. <div
  35. style={ { transform: `translateY(${ YPos }px)` } }
  36. className={ addclass }
  37. { ...props }
  38. />
  39. </Fragment>
  40. );
  41. }
  42.  
  43. Parallax.propTypes = {
  44. addclass: PropTypes.string,
  45. uppermodulevisible: PropTypes.number,
  46. };
  47.  
  48. export default Parallax;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement