Progressively increasing the time by 15 seconds during each iteration of the CSS @each loop

I recently made changes to a CSS script that involves the repetition of an animation task. Rather than repeating a segment of code 30 times, I decided to streamline it using a loop. Now, there are 30 <div></div> segments and each one undergoes the animation as follows:

@each $i from 1 through 30 

#dna div:nth-child(#{$i}) {
    animation-delay: -59.85s;
}
#dna div:nth-child(#{$i})::before {
    animation-delay: -59.85s;
}
#dna div:nth-child(#{$i})::after {
    animation-delay: -59.85s;
}


@-webkit-keyframes rotation {
    from {
        transform: rotateX(0deg);
    }
    to {
        transform: rotateX(359deg);
    }
}

The challenge I'm facing now is finding a way to incrementally add 15 seconds to the -59.85 second animation delay with each iteration of the loop. Since this is new territory for me in working with CSS capacities like this, I'm unsure about where to begin.

Any guidance or assistance on how to achieve this would be greatly appreciated!

Answer №1

I don't have much experience with variables in css, but I recently discovered that the calc() function can be quite useful.

Give this a try:

#dna div:nth-child(#{$i}) {
    animation-delay: calc(-59.85s + $i * 30);
}
#dna div:nth-child(#{$i})::before {
    animation-delay: calc(-59.85s + $i * 30);
}
#dna div:nth-child(#{$i})::after {
    animation-delay: calc(-59.85s + $i * 30);
}  

If you're interested in exploring more advanced programming features of css, take a look at: this resource.

Answer №2

If you want to create loops within your scss files, you can follow this structure:

$fade-delay: -59.85; // initial value, adjust as needed for the starting point in the first loop
$fade-delay-seconds-to-add: 15; // number of seconds to add in each iteration
$number-of-loops: 30;

@for $i from 1 through $number-of-loops {

    $fade-delay: $fade-delay + $fade-delay-seconds-to-add;

    #dna div:nth-child(#{$i}) {
        animation-delay: #{$fade-delay}s;
    }
    #dna div:nth-child(#{$i})::before {
        animation-delay: #{$fade-delay}s;
    }
    #dna div:nth-child(#{$i})::after {
        animation-delay: #{$fade-delay}s;
    }
}

When compiled by Sass, it will result in:

#dna div:first-child,
#dna div:first-child:after,
#dna div:first-child:before {
    -webkit-animation-delay: -44.85s;
    animation-delay: -44.85s;
}

#dna div:nth-child(2),
#dna div:nth-child(2):after,
#dna div:nth-child(2):before {
    -webkit-animation-delay: -29.85s;
    animation-delay: -29.85s;
}

#dna div:nth-child(3),
#dna div:nth-child(3):after,
#dna div:nth-child(3):before {
    -webkit-animation-delay: -14.85s;
    animation-delay: -14.85s;
}

/* Repeat pattern for subsequent elements */

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

Discovering components with identical html attributes in Selenium

I am currently developing a web crawler using Python 3.8. My goal is to convert the table below into a pandas dataframe using Selenium, Pandas, and BeautifulSoup. <table class="no_border_top" width="95%" align="center"> ...

Navigate to a list item once Angular has finished rendering the element

I need to make sure the chat box automatically scrolls to the last message displayed. Here is how I am currently attempting this: akiRepair.controller("chatCtrl", ['$scope', function($scope){ ... var size = $scope.messages.length; var t ...

What is the best way to gradually transform a continuously shifting text color into a single, consistent hue?

Hey there, wonderful people of StackOverflow! I am absolutely stumped and cannot figure out how to smoothly transition this color-changing text from its current state into a different solid color when the submit button is clicked. Is there a skilled indiv ...

Animation for Images in Angular v1.2

After reviewing tutorials and previous questions, I have been trying to pinpoint where my code is going wrong in applying an animation to transition between the images I want to display. The ng-show function effectively displays the selected picture, but ...

Make sure to have the list available while choosing an item

I want to design a unique CSS element with the following characteristics: Include a button. When the button is clicked, a list of items (like a dropdown list) should appear. We should be able to select items by clicking on them, and the parent component s ...

Ensure that the IP address is properly formatted in HTML forms

I need to validate the IP address entered by a user in a form. If the IP format is not like 192.168.0.100 (with periods), I want to display an error message. How can I achieve this? <form name="add" > <input name="ip" type="text" value="" /&g ...

How can I troubleshoot the overflow-y problem in a tab modal from w3 school?

https://www.example.com/code-sample overflow: scroll; overflow-y: scroll; When I type a lot of words, they become hidden so I need to use overflow-y=scroll. Despite trying both overflow: scroll; and overflow-y: scroll;, I have been unable to achieve the ...

Optimal ffmpeg configurations for encoding videos into mp4 and ogg formats for seamless playback on HTML5 platforms

Despite the buzz surrounding it, the HTML5 video tag is facing a minor issue. To ensure cross-browser compatibility, multiple video formats - like mp4 and ogg - need to be included for its use. Unfortunately, I couldn't find optimal settings for each ...

object stored in an array variable

I am looking to find out if there is a way to access a variable or function of an object that has been added to an array. I want to display all the objects in the array on a web page using a function that will return a string containing their content. Thi ...

External elements - My physical being is in a state of chaos

Hello there, I hope everything is well with you. I have been working on creating a form using MaterializeCss to calculate an invoice for a craftsman. The user is required to input a number or select an item. Everything seems to be going smoothly so far. ...

JavaScript for Verifying Phone Numbers

After extensive research and tutorials, I am still unable to figure out what is going wrong with my work. I have a button that looks like this: Despite Stack Overflow causing some trouble for me, please ignore the missing class as it is there. This is no ...

Is the Three.JS camera update causing issues with the shader?

Attempting to replicate the Bubble shader from https://github.com/stemkoski/stemkoski.github.com/blob/master/Three.js/Bubble.html, but encountering issues due to outdated code. The example should refract whatever is behind it, like this: https://i.sstatic ...

Choose the first and last div element in the selection

Is there a way to target the first and fourth div elements using the :nth-child selector for styling purposes? .main{ width:460px; height:240px; } .box{ width:100px; height:100px; background:red; float:left; margin:10px; } /*I tried */ . ...

execute a different function once the animation has finished running with the .bind

When attempting to detect the completion of a CSS animation in order to remove a class, I encountered an issue where using .bind causes the animation end event to trigger even for the other function. You can view the complete code here. How can I ensure ...

Enhance the functionality of jQuery sortable by including additional details

I have a li list that I have implemented sortable functionality using jQuery. In order to ensure that the updated data is sent to the correct destination, I need to include some hidden values in the serialized data. How can I achieve this? HTML <ul i ...

What is the process for inserting a scroll bar within a div element?

   I have recently created a webpage using some divs, along with a bit of CSS and JavaScript. I am struggling to figure out how to add a scrollbar to one of my divs. The code is not overly complex, as it includes both CSS and JavaScript. <html> & ...

Preventing default behavior in a JQuery post request

Encountering an issue with jQuery functionality on a mobile device. function send() { $.post("scripts/post.php", { username: $("input[name=username]").val(), password: $("input[name=password]").val() }, function(data) { if ($(".data div" ...

Populate a MySql database with 2 input values

I am facing an issue with inserting values into MySql from a table. When trying to insert two values, only one gets stored in the database and the other remains empty. Here is the structure of my MySql table: https://i.sstatic.net/yWLbO.png Below is the ...

Implementing a full-width search bar within the Bootstrap navbar

I'm trying to create a navbar using Bootstrap 3.7.7 with a logo on the left, links on the right in two rows, and a large search bar in the center. I've managed to align the logo and links correctly, but I'm struggling with sizing the search ...

*ngIf doesn't seem to be functioning as expected compared to other *ngIf directives

Encountering a peculiar issue with my *ngIf related to the isAdmin variable which determines whether the list of users in userList should be displayed or not. Strangely, it seems to behave differently compared to other *ngIf statements within the same comp ...