What is the best way to activate CSS keyframe animation depending on the scroll location?

My goal is to create a circle animation that fades in based on the user's scroll position. As the user scrolls down, the circle grows larger, and as they scroll up, it shrinks. The animation will pause when the user stops scrolling, requiring them to scroll multiple times to complete it.

Currently, I only have basic CSS and HTML implemented. I need a JavaScript scroll function to trigger the increase or decrease in size of the circle based on the user's scrolling behavior.

.section {
      animation: 3s fadeInAnimation reverse;
      animation-delay:0s;
      animation-timing-function: ease-in-out;
        animation-iteration-count: 1;
      background: yellow;
      height: 100vh;
    }
    
    @keyframes fadeInAnimation {
        0% { 
                clip-path: circle(75%); 
        }
      100% { 
            clip-path: circle(0%); 
        }
    }
<div class="section">
<h1>
Section Title
</h1>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut tincidunt commodo lacus vitae porttitor. Morbi lobortis diam lorem, sed faucibus leo gravida ac. Curabitur ex velit, consectetur vitae ligula in, fringilla tincidunt turpis. Curabitur sem turpis, scelerisque et pretium in, ornare at dui. Morbi pellentesque viverra rhoncus. 
</p>
</div>

Check out the Fiddle Here

Answer №1

Utilizing a CSS Custom Property and dynamically updating its value through JavaScript as the user scrolls can create an interactive web experience.

To begin, establish the initial value of the custom property like so:

  • :root { --clip-circle: 0% }

Subsequently, adjust this custom property whenever necessary by:

  • calculating the new value using clipCircleValue
  • document.documentElement.style.setProperty('--clip-circle', clipCircleValue);

Test it out with this example (click Full Page option for a better view) :

const updateClipCircleValue = () => {

  let documentScrollHeight = document.documentElement.scrollHeight;
  let documentScrollPosition = window.scrollY;
  let documentScrollPercentage = ((documentScrollPosition / documentScrollHeight) * 100);
  let clipCircleValue = documentScrollPercentage + '%';
  
  document.documentElement.style.setProperty('--clip-circle', clipCircleValue);
}

window.addEventListener('scroll', updateClipCircleValue);
:root {
  --clip-circle: 0%;
}

body {
  min-height: 400vh;
}

.section {
  position: fixed;
  top: 0;
  left: 0;
  background-color: rgb(255, 255, 0);
  clip-path: circle(var(--clip-circle));
}
<div class="section">
<h1>
Section Title
</h1>

<p>
// Repeat this lorem ipsum content multiple times to see the effects...
</p>

</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Keep fancybox items in their designated spot

When the window is opened at full size, everything looks fine. However, when you resize the window horizontally, the thumbnails shift and disappear. I want them to remain fixed in their position like the large pop-up image so that users can still see them ...

Paginating: Restrict the page number for smaller screens

When viewing on a small screen device (> 641 px), my pagination should resemble https://i.sstatic.net/dSSUX.png Conversely, on screens that are medium-sized and larger (< 641 px), the pagination should appear as shown here: https://i.sstatic.net/C1E ...

How to implement scrollspy in Bootstrap 4 without relying on nav or list-group components?

Is it possible to implement scrollspy functionality without utilizing the nav or list-group elements? According to Bootstrap's documentation on scrollspy, it is typically restricted to use within nav or list group components. DOCUMENTATION How Scroll ...

What is the best way to initiate an animation once the one before it has finished?

I am facing an issue with my animation function where all animations play simultaneously. This is not the desired behavior; I would like to play each animation after the previous one ends. Can someone guide me on how to achieve this? canvas = document.get ...

The values from the form fields are not being properly transferred into the JSON object

Check out my JSFiddle here: https://jsfiddle.net/inchrvndr/7pwh9p8g/ When you click the "+" button, the bordered form elements are getting cloned. All the values of the cloned form elements are being passed into JSON data except for the parent element fr ...

Positioning the image to appear behind the Canvas

As I near the end of the game, I'm encountering an issue where the background image overlaps the Canvas. How can I resolve this problem? -Translated by Google Link to game Link to game with background ...

What causes the lack of impact when editing bootstrap.css files in an ASP.NET Core Web App?

As I dive into ASP.NET, I decided to start by creating a default Web App template in Visual Studio. To my surprise, despite having bootstrap.css files in the wwwroot folder, any changes I make to them seem to have no effect on the website's appearance ...

Place picture in the bottom right corner of the div without using absolute positioning

I am looking for a way to position an image in the lower right corner of a div, but I want to avoid using absolute positioning. The reason for this is that when I use absolute positioning, the text wrap is affected due to the float right applied to the ima ...

Can the @keyframes in CSS3 be modified?

Exploring CSS3 animations has been a fun project for me, and I want to push the boundaries of what is possible. Currently, I have an infinite animation cycle that is working perfectly. However, I'm curious if it's possible to dynamically change ...

What is the best way to dynamically filter checkboxes based on user input using jQuery in the following code snippet?

In order to filter the checkboxes in the code below, I would like to only display the checkbox with a value matching the input entered in the input field with the class "js-filter-input". For example, if I type "uni1" in the input field, I want to show onl ...

Animation of underline on hover for NavLink

As a newcomer to React, I am currently exploring how to create an animated underline effect when hovering over a NavLink from reactstrap. I have managed to get the underline working on hover thanks to the solution provided at , but I am struggling with imp ...

Issue with IE7 causing div elements to slide off the screen on specific WordPress pages

Encountering a strange IE7 bug on two pages of a WordPress site I'm currently developing. Some divs are shifting to odd positions, with one completely off the screen: and another here: Additionally, possibly related or not, the "created by" link in ...

Selector in CSS targeted by using target selector

When you click on the anchor tag "Click me," is there a way to use only CSS to display the p tag with the text "This is my answer"? I am aware that placing the p tag after the anchor tag is a solution, but I am curious if there is a method to achieve this ...

Animate movement in CSS3 by utilizing the CSS grid class to handle x and y coordinates

Currently, I have organized my tile elements in a grid using absolute positioning with classes like .row1, .col1... .row6, .col6. If I want to move a tile from .row6, .col6 to .row2, col2 using CSS3 GPU accelerated transitions and transformations Can you ...

Android tablet (Nexus) optimized for full-height website viewing

I have been attempting to retrieve the Nexus tablet's height by using $(window).height();, but I am encountering difficulties because it seems to include the height of the URL bar in the result, which disappears when the user scrolls. My webpage is d ...

The issue of duplicate CSS arising during the compilation of SASS into a single CSS file with G

Just getting started with Stack Overflow and Gulp (using version 3.9.1). My goal is to compile all of my scss files into a single css file for my website. Here's what I have in my gulpfile so far: var gulp = require('gulp'); var sass = requ ...

Bootstrap 5 introduces an innovative animated hamburger icon that transforms into an X symbol when clicked

I have encountered an unusual behavior when trying to animate the hamburger button using Bootstrap 5. Previously, with Bootstrap 4, everything worked smoothly. However, in Bootstrap 5, I noticed that the icon initially displays as 'X' and then sw ...

Failure of Outlook 2007, 2010, and 2013 to Recognize Conditional CSS

I am currently working on a design for a responsive email. The layout is functioning well in all email clients tested using Litmus, except for Outlook 07/10/13 due to its challenging word rendering engine versions. To ensure correct display across differen ...

``CSS: Styling a Rounded Div with a Clipped Scrollbar

I am attempting to contain a scrollbar within a div so that it remains within the rounded corners without overflowing. Take a look at the code snippet below to see the problem in action. Is there a way for the scrollbar to be fixed in its position, while ...

Is the content within the div longer than the actual div itself when the width is set to 100%?

When working with a div of fixed width that contains only an input text box, and the width of the input is set to 100%, you may notice that it extends slightly beyond the boundaries of the div. For demonstration purposes, consider the following code: HTM ...