Include a parameter in the @keyframes attribute to reduce the amount of code

After compiling my property @keyframes with autoprefixer to add the necessary prefixes, I am looking to add an argument to the animation name (or wherever possible) in order to change a value of properties within the keyframes key.

This is the current state of my code:

@keyframes loader {
  0% { transform: translate(0, -50%) rotate(0deg); }
  100% { tranform: translate(0, -50%) rotate(360deg); }
}

What I am aiming for is to achieve the following:

@keyframes loader(@transform) {
  0% { transform: @transform rotate(0deg); }
  100% { tranform: @transform rotate(360deg); }

Answer №1

When it comes to passing arguments to @keyframes within Less, direct implementation is not supported. However, one workaround is to encapsulate the entire @keyframes rule in a parent mixin, pass the argument to that mixin, and then utilize it within the frames.

.loader(@transform){ /* Parent mixin accepting input parameter */
   @keyframes loader {
     0% { transform: @transform rotate(0deg); }
     100% { transform: @transform rotate(360deg); }
   }
}

.loader(translate(0, -50%)); /* Invoking the mixin */

(An initial answer by Curt was provided but has been removed for undisclosed reasons.)


For those curious, generic keyframe mixins can also be defined in Less as demonstrated below.

Sample 1:

.generickeyframe(@name; @from; @to){ /* Accepts name, from frame rules, and to frame rules */
  @keyframes @name{
    0% { @from(); }
    100% { @to(); }
  }
}
.generickeyframe(loader; {transform: translate(0,-50%) rotate(0deg)}; 
            {transform: translate(0,-50%) rotate(360deg)});

Sample 2:

.keyframefromto(@name; @from; @to){
  @keyframes @name{
    0% { transform: @from; }
    100% { transform: @to; }
  }
}
.keyframefromto(loader; translate(0,-50%) rotate(0deg); translate(0,-50%) rotate(360deg));

In scenarios where multiple frames are needed in the @keyframes rule, array-lists and loops can be employed similar to the following snippet. This mixin requires the animation name, a list of frames (percentage numbers), and properties for each frame (in the form of rulesets) as its parameters.

.generickeyframe(@name; @framelist; @frameprops){
  @keyframes @name{
    .loop-framelist(@index) when (@index <= length(@framelist)){
      @framepos: extract(@framelist, @index) * 1%;
      @{framepos}{
        @props: extract(@frameprops, @index);
        @props();
      }
      .loop-framelist(@index + 1);
    }
    .loop-framelist(1);
  }
}
.generickeyframe(loader; 
                0,25,50,75,100; 
                {transform: translateX(10px);},
                {transform: translateX(20px);},
                {transform: translateX(50px);},
                {transform: translateX(20px);},
                {transform: translateX(10px);}
                );

Compiled CSS:

@keyframes loader {
  0% {transform: translateX(10px);}
  25% {transform: translateX(20px);}
  50% {transform: translateX(50px);}
  75% {transform: translateX(20px);}
  100% {transform: translateX(10px);}
}

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

The Bootstrap dropdown menu unexpectedly appears in front of the panel located above

Having trouble with the Bootstrap dropdown panel. My web page has 4 main panels, each containing additional bootstrap panels. When I open one panel at a time, everything works fine. However, when I try to open multiple panels simultaneously, the positions ...

Struggling to create a photo grid design that meets my expectations

I'm struggling to recreate this particular photo grid. Any tips on how I can achieve it? Check out the image here Just to clarify, I had a working prototype see image description using flexbox. However, one image didn't fit well so I resorted to ...

Is anyone else experiencing differences in the appearance of my mega menu between Safari, Firefox, and Chrome? It seems like

When viewing in Safari, the text overlaps for some reason. It works fine on Firefox and Chrome. Here's a screenshot of the issue: Safari: https://i.stack.imgur.com/hf7v2.png Firefox: https://i.stack.imgur.com/NrOOe.png Please use Safari to test th ...

Images displayed in the Slick carousel are automatically given a stylish padding

I'm facing an issue with my slick-carousel where there seems to be a large padding on the right side of the images, extending all the way up to the next product image. I've tried removing margins and paddings from my code but it hasn't resol ...

Modifying iframe elements dynamically across domains

I am faced with the challenge of embedding an iframe form from a web application that I use onto my website. This custom inquiry form is integrated into my diary within the web app, and now I want it to be displayed on my main website while maintaining a c ...

Truncate a string in Kendo Grid without any white spaces

While using a Kendo-Grid for filtering, I've come across an issue where my cell does not display the full string if there is no white space. The problem can be seen in the image below: https://i.sstatic.net/0h1Sg.png For example, the string "https:/ ...

Order of stacked divs with absolute positioning

I'm struggling to make a photo expand when hovered over without it expanding under the existing content. My current HTML and CSS code is as follows: .userCard {width:360px;height:180px;border:1px solid black;float:left;margin:10px;position:relative ...

Adjust the height of the page to fit the available screen space on mobile devices

Seeking a solution to ensure my web app fills the entire height of the device. Currently utilizing 100vh on the body tag, however, Safari tab on iOS adds an additional height which causes users to have to scroll down to view the full page. Any suggestions ...

Eliminating the standard padding on a Vuetify v-app-bar

When using Vuetify's v-app-bar, there are default css classes named v-toolbar__content and v-toolbar__extension that apply padding of 16px on the x-axis and 4px on the y-axis, which I aim to eliminate. I attempted to override these classes in my CSS ...

Is there a way to reveal multiple inputs by simply pressing a button?

Currently, I am working on developing a website using HTML, CSS, and JavaScript. As part of the process, I am looking to implement a login interface. I have already included some input fields and written a sign-in.js script that verifies whether a user h ...

What is the best way to make a scrollable div with top and bottom padding without adding extra elements inside?

I have created a fiddle to demonstrate my question. Essentially, I am looking to create a scrollable container with padding at the top and bottom that remains visible during scrolling, ensuring there is always a consistent distance from the edge to the con ...

The flex-direction property is not behaving as anticipated in the Chrome browser

Hi everyone, I really need some assistance with my code. The flex-direction property is not working for me no matter what I try. I've tested it on both Chrome and Microsoft Edge, but it displays the same result. I've been stuck on this for the pa ...

Chrome not displaying scrollbar when using overflow-y property set to scroll

I am experiencing an issue with the scrolling functionality in Chrome version 84.0.4147.89. I have a div that is set to overflow-y:scroll;, but unfortunately, the scroll bar is not visible. This poses a challenge for users without a mouse or touchscreen as ...

Consistent height for responsive images

How do I maintain the same height and proportions of images? Setting a specific height like 70vh achieves the desired height but distorts image proportions: https://i.sstatic.net/8eJq2.jpg On the other hand, using height: auto; creates responsive and pro ...

Card Resizes when Search Button is Pressed

Recently, I've delved into Bootstrap after focusing primarily on backend development. As part of my front end work, I am implementing a feature where users can search for a place and a list of nearby venues will appear in a card layout using Bootstrap ...

Is it permissible to have <ul> tags within an H1 element?

I am curious about whether adding a list inside an H1 heading could cause any issues: <h1> <ul> <li><a href="...">some link</a></li> <li><a href="...">another link</a></li> <li>curre ...

I am looking to create a header design that is fully responsive and resembles the example shown in the image. How can I achieve

Currently, I am working on creating a responsive header for a website. The image provided is a rough draft of the header design that I am aiming to achieve. I attempted to form the shape using an SVG path but encountered difficulties in making it fully res ...

Is it possible to embed a section of code from a different file?

Looking for a solution to streamline the process of updating multiple web pages with identical header and footer content. Currently have 5-10 pages with the same information, making it time-consuming to manually update each one when changes are needed. I ...

Animate the object in SVG with periodic movements

Illustrate once and move items every five seconds in the sequence shown from left to right. I have looked extensively but cannot find a method to draw outer boundaries and position objects close to them. <animate xlink:href="#blue-rectangle" a ...

Replace Css styling with custom styling in external stylesheet

Here is the code from the custom.css file, an external CSS within an HTML page: button { cursor: pointer; appearance: none; -moz-appearance: none; -webkit-appearance: none; border: none; padding: 0; margin: 0; background: none; -webkit-user-select: no ...