Parallax

  • 1

  • 1

  • 1

A single useEffect() can log DOM userRef()/querySelector() elements.

The ParallaxLayer children components need an useState() dependency, that triggers only after the page is useEffect() mounted, to be logged.

//Remember React.strictmode is active when testing

useEffect(() => {
  setParallaxLayerMounted(true); 
}, []);

const stratosRefs = useRef([]);

useEffect(()=> {
  console.log( stratosRefs )
}, [parallaxLayerMounted])

The navbar has to be outside the <Parallax> container, it will cover part of the first ParallaxPage and won't create additional scrollbars.

//It needs a higher zIndex to be visible 
//We modify the intersectionObserver() option object for Parallax elements
let options = {
  root: base,
  rootMargin: "0px 0px -85% 0px",
  threshold: 0,
}

<div>
  <div className='position-fixed' style={{ height: "5em", top: 0, zIndex: 5}}>
    Navbar
  </div>

  <Parallax pages={3.2} id='finestra' ref={parallaxRef} style={{ height: "100vh" }}>
    ...
  </Parallax>
</div>
Position-fixed navbar on <Parallax/>

A Parallax component won't respond to a scroll event, only to a wheel.

chevron-rightWheel event on Parallaxhashtag

The wheel event is attached to the useRef() of the <Parallax> container.

We useRef([]) <ParallaxLayer/> child elements, check useRef() callback function in React4.

We reduce() loop the useRef([]) intersectionObserver target.id, and we assign them to className DOM element. We cache the object result before the intersect.

chevron-rightReturning Objects from reduce() and for/in loopshashtag

In both methods, the assign operation inside the loop implicitly creates a name:value pair in the object.

On reduce() the accumulator is the returned object with the set name:value pairs.

We use the current boundingClientRect.top of the current intersect <ParallaxLayer/> to edit the useRef().style of its paired column (from the cached reduce() object).

We use an array threshold to simulate the scroll event, and remove the navbar height from the calc().

Threshold useRef().style columns on <ParallaxLayer> intersect

1

1

1

We create an absolute <parallaxLayer/> between the pages, that ignores the overflow.

1

1

1

1

We can't relative import files from the public folder, so we create a src folder.

We can overlay <parallaxLayer/> as absolute layers, their display depends on their order. When overlaying svg files we cut the svg extra borders we use https://svgcrop.com/arrow-up-right, which will keep the svg aspect ratio with less space.

Cut vs uncut SVG files, with aspect ratio example

1

1

1

1

1

Last updated