Advertisement

Tilting Diamond Range Slider Effect

| by Vladimir | 2 min read | code by @keyframers
A11y Ready Advanced

Tech & Dependencies

HTML SCSS Babel

Features

  • Lerp Animation
  • Kinetic Tilt
  • Native Input

Browser Support

Chrome 80+ Edge 80+ Firefox 75+ Safari 13+

Core

This is a Tilting Diamond Range Slider Effect. It tracks user input through a hidden native range element, overlaying a custom graphical interface. Its primary function is to inject physical inertia into a standard digital control. The tooltip mimics mass, lagging behind rapid movements and tilting in the direction of travel.

Specs

  • Weight: ~3 KB (Zero external dependencies for core logic).
  • Performance: High. The requestAnimationFrame loop strictly updates CSS variables. Transformations are GPU-accelerated (transform: translateX() rotate()).
  • Theming: Centralized via the --color-primary CSS variable.
  • Responsiveness: Fluid width. Scales perfectly across mobile and desktop interfaces.
  • Graceful Degradation: [!] If JS fails, the native <input type="range"> remains fully functional but invisible. It is recommended to add a fallback class to reveal the native input if the JS bundle does not load.

Anatomy

  • HTML (The Skeleton): Relies on a standard <input type="range"> for accessibility and mobile touch support. Custom visual layers (.slider-track, .slider-thumb, .slider-diamond) are absolute-positioned on top.
  • CSS (The Skin): Masks the native input (opacity: 0) while keeping it clickable. Maps JS-calculated variables (--d, --tilt) directly to CSS transform rules.
  • JS (The Nervous System): A custom linear interpolation (lerp) engine. It continuously calculates the distance between the user’s target value and the current visual value, broadcasting the state to the DOM.

Logic

The elegance lies in extracting the velocity from the interpolation gap.

const tilt = Math.min(6, target - current);
elDiamondSlider.style.setProperty("--tilt", tilt);

Instead of complex event listeners tracking mouse speed, the script simply compares the actual input value (target) against the visually rendered value (current). The larger the gap, the faster the slider is moving, which directly translates to a more extreme rotation angle (--tilt).

Feel

Tactile and elastic. Dragging the slider feels like pulling a physical object attached to a rubber band. The diamond tooltip springs out on hover, leans into the drag, and snaps smoothly into place when released. The visual feedback perfectly matches the physical intent.

Advertisement