Overview
-DSAP is a small library that provides you with the tools to create powerful CSS-based animations
-Usage
-Add data-dsap
to elements you want to track in the viewport. DSAP will then calculate
- the position of the element in the viewport and keep track of it during scroll.
The following CSS variables are exposed to each instance of a DSAP element:
+DSAP is a streamlined library that equips you with a suite of functionalities for crafting compelling + CSS-based + animations.
+What DSAP is not
++ DSAP is a specialized utility designed to enhance CSS animations with scroll and visibility + tracking, complementing + rather than replacing robust animation libraries like GSAP. It's optimized for performance, yet + developers need to + integrate it thoughtfully, ensuring it contributes to improved website performance metrics such + as LCP and CLS, + thereby facilitating smooth and captivating user interactions.
+How to Implement DSAP
-
-
--dsap-scroll-delta
- This indicates how much of the element has been scrolled within the - viewport. This value ranges from 0 to--dsap-max-scroll-delta
(see below). This is not a percentage +-
+ Visibility Tracking:
+
Tag HTML elements with the
data-dsap
attribute to monitor their presence + within the viewport. +
+ -
+ Scroll Position Monitoring:
+
Apply the
+data-dsap-scroll
attribute to track an element's location in the + viewport as it + scrolls. This capability is essential for executing scroll-driven animations.
+
CSS Variables for DSAP Elements
+DSAP imparts each element with specific CSS variables:
+-
+
--dsap-scroll-delta
: Reflects the vertical scroll progress of an element within + the viewport, + spanning from 0 to--dsap-max-scroll-delta
. This measurement is not a + percentage.
+ --dsap-max-scroll-delta
: Defines the scale for scroll delta interpolation, with + a default setting + of 1000. This variable is crucial for formulating scroll-related animations.
The following global CSS variables are available globally to assist with calculations:
+Data Attributes for DSAP Elements
+DSAP enhances elements with a set of data attributes for detailed animation control:
-
-
--dsap-max-scroll-delta
- Used as the value a scroll delta should interpolate - between. By default, this is 1000.
+ data-dsap
: This attribute prompts DSAP to monitor the element. Upon initialisation, it will be assigned an + identifier for CSS targeting.
+ data-dsap-scroll
: Use this to opt-in to scroll position tracking
+ data-dsap-is
: Offers a simple descriptor of the element's vertical + position—whether it is "above", + "in", or "below" the viewport.
+ data-dsap-seen
: A boolean flag that determines if the element has ever been + visible in the + viewport.
The following data attributes are present on all DSAP elements:
+Performance Considerations
+DSAP has been fine-tuned for performance with the following methodologies:
-
-
data-dsap
- As well as using this attrivute to tell DSAP to track this element
- data-dsap-is
- A simple value to determine if the element is "above", "in" or - "below" the viewport. Useful for creating directional transitions. For example, Creating a fade - in effect when the element enters the viewport from the bottom
- data-dsap-seen
- A true/false value indicating whether the element has ever entered - the viewport. Useful for making one-time animations that don't trigger every time the element - comes into the viewport
+ - + Efficient Variable Updates: Instead of inline style adjustments for each + element, DSAP modifies + a single style element on scroll, enhancing efficiency. This approach relies on unique + identifiers for DSAP + elements, allowing precise targeting via the head style. + +
- + Optimized Scroll Event Handling: Scroll-related computations are delegated + to + requestAnimationFrame(), minimizing impact on the main thread and preventing performance + bottlenecks. This + function aligns with the user's display refresh rate, typically at 60Hz, to limit excessive + processing and + seamlessly adapts to higher refresh-rate screens, ensuring fluid animation transitions. +