React-Spring 3 useSpringValue(), useChain(), useTrail() indeterpolation and Parallax.

The useSpringValue() hook sets singular style properties, it accesses useSpring() methods imperatively.

//It doesn't require the API call
//it won't trigger on the updated component, we must use its methods.

let valore = useSpringValue(0, {
  config: config.wobbly,
})

let valore1 = useSpringValue(0, {
  config:{ mass: 20, friction: 3, tension: 10 },
})

function starto(){
  valore.start(300)
  valore1.start(300)
}

<div>
  <animated.div className="barra1" style={{ width: valore }} >
  </animated.div>

  <animated.div className="barra2" style={{ width: valore1 }} >
  </animated.div>
</div>
useSpringValue() on animation-timing function

Animation sequences with useChain()

The useChain( [useSpringRef() array], [timesets], timeframe ) hook links multiple animations in a specified order, controlling their timing.

We useState() as a trigger for the useChain(), which will revert the animation's order and delays.

The useSpring() values and the useTransition() element depend on the useState(), to not trigger before the useChain() timing.

We change the order of the useTrasition() elements with its second property (item).

We use display:grid on the useTransition() elements.

useChain() on useTransition() and useSpring()
chevron-rightThe useChain() on useTransition() grid elementshashtag

We import an array of gradient objects for the useTransition().

We set the useState() trigger like the previous useChain() example.

With the same grid style.

useChain() grid effect

Staggering animations with useTrail()

The useTrail() hook generates a trail of sequential animations.

We set the starting useTrail() properties and animate them on api methods (from/to would trigger the animation on start), each element's duration adds up to the previous ones.

We need position absolute to avoid margins being influenced by the container area. We extract useTrail() style props and index to access the initial array.

useTrail() elements on animated style properties
chevron-rightBackface-visibility on useTrail() rotated elementshashtag

We set and api animate the useTrail() props like the previous example.

We rotateX() 2 opposite useTrail() elements with the to() API method.

We use backface-visibility to hide the rotated useTrail() absolute element.

Scroll sections with Parallax and ParallaxLayers.

We npm install @react-spring/parallax and extract the Parallax and ParallaxLayer.

A Parallax container animates its ParallaxLayer children onScroll() position. It comprises pages, each 100% height/width of the viewpoint, and fires its scroll events from the container, not the window.

The Parallax page has to include all the different offset positions and factor parallaxLayers.

Using the offset position, the Parallax ref.current can trigger scrollTo() events. We remove the included scrollbar with a pseudo:selector and set left to fill the gap.

Sticky Parallax scroll elements
chevron-rightHorizontal Parallax and sticky parallaxLayer viewpoint areahashtag

The ParallaxLayer speed attribute adds a translate effect on a scrolled element. The <Page/> container returns multiple ParallaxLayers sharing the same offset.

The horizontal sticky absolute space occupies the entire viewpoint, independently from its width/height, we reduce it to not interfere with the <Page/> onClick().

Each <Page/> has its clip-path on its background.

horizontal Parallax with sticky top layer

Sticky ParallaxLayers, SVG images, and backgrounds

Don't use background on the sticky layer component, it will inherit the "container" layer.

It needs display: "inline-block" and height: 0, to not conflict with the other layer elements, its height is set only by its content.

We use vertical-align: "top" to align the inline-block elements to the sticky layer linebox, check CSS-1 for more vertical-align.

Check the Intersection API section to see how Intersection Observer API interacts with <Parallax>

We import SVG icons in the ParallaxLayer, as image src or as a component, and modify their svg properties.

The parallaxLayer's order is backgroundColor > SVG icons > SVG backgroundImage. We scrollTo() an entire Page by including specific offset ranges in a <div> container.

More images in the same ParallaxLayer won't overlay and will sum their height, z-index works between layers and can override the overflow:hidden.

chevron-rightMultiple SVG icons on ParallaxLayer and onScroll entire pageshashtag

Multiple SVG backgroundImage can be added at the end of the offset.

Parallax and ParallaxLayers scroll animation

Last updated