Join the Webstudio community

Updated 5 days ago

How to animate the blur of an image as you scroll?

Doing it raw, you'd just set the filter in css

CSS
filter: brightness(0.75) blur(var(--current-blur, 0px));   


And the javascript would be

JavaScript
document.addEventListener("DOMContentLoaded", function() {
  const heroSection = document.querySelector(".hero");
  const heroHeight = heroSection.offsetHeight;

  // We want the blur to go from 0px up to 35px 
  // once we've scrolled the full hero height.
  const maxBlur = 35;

  // Select the .bg container (our pseudo-element is inside it)
  const bg = document.querySelector(".bg");

  window.addEventListener("scroll", () => {
    const scrollY = window.scrollY;

    // 0..1 progress from top of page to bottom of hero
    let progress = scrollY / heroHeight;
    if (progress < 0) progress = 0;
    if (progress > 1) progress = 1;

    const currentBlur = maxBlur * progress;
    
    // We'll store that in a CSS variable for the pseudo-element
    bg.style.setProperty("--current-blur", `${currentBlur}px`);
  });
});


Now, as you scroll, the background image would progressively get blurred. How to do this in webstudio?

J
j
J
4 commentsยท2 replies
Exactly like that ๐Ÿ™‚ Just add that class to the hero and in advanced properties paste in filter: brightness(0.75) blur(var(--current-blur, 0px));

what do you mean by add that class to the hero? where do I add it in the Webstudio UI? thanks a lot

Be aware that this can be an expensive operation for the browser. The blur filter repaints the user's scroll and could cause jankyness or scroll lag. This isn't recommended on large images.

For a minor improvement, you can add "will-change: filter" to the element getting blurred, but don't over use it cause it can cause overloading the GPU memory.

where would i add this? under Advanced?

In Settings. Here's a short video about Tokens and Classes https://youtu.be/_1QSWHOtk08

Hey, sorry for sounding dense, but can I trouble you for step by step instructions and full names of the parts of the webstudio app where I need to do this?

Thanks a lot.