Utilizing @each in Sass to define browser prefixes as variables

We've got a lot of exciting animations on the horizon and I'm on the lookout for a more streamlined solution to cater to all browsers. Something with a hint of sassiness like this would be perfect:


@each $browser in '-webkit-', '-moz-', '-o-', '-ms-' {
  @#{$browser}keyframes rotate {
    from { #{$browser}transform: rotate(0deg);}
    to { #{$browser}transform: rotate(360deg);}
  }
}

Unfortunately, the @#{$vendor}keyfr... snippet throws an error expecting a number or function after the @. Is there a way to make the @ valid in the css attribute?

Alternatively, has anyone discovered a more elegant method to achieve this using @each, @mixin, or any other technique that avoids manually listing each animation block for different browsers (as shown below)?


@-webkit-keyframes rotate {
  from { -webkit-transform: rotate(0deg);}
  to { -webkit-transform: rotate(360deg);}
}

@-moz-keyframes rotate {
  from { -moz-transform: rotate(0deg);}
  to { -moz-transform: rotate(360deg);}
}

@-o-keyframes rotate {
  from { -o-transform: rotate(0deg);}
  to { -o-transform: rotate(360deg);}
}

@-ms-keyframes rotate {
  from { -ms-transform: rotate(0deg);}
  to { -ms-transform: rotate(360deg);}
}

Answer №1

To achieve this, you can utilize a mixin to preset the vendor keyframes rather than generating them dynamically in a loop. Here is an example implementation:

@mixin defineKeyframes($animationName) {
    @-webkit-keyframes #{$animationName} {
        $vendor: '-webkit-'; @content;
    }
    @-moz-keyframes #{$animationName} {
        $vendor: '-moz-'; @content;
    }
    @-o-keyframes #{$animationName} {
        $vendor: '-o-'; @content;
    }
    @keyframes #{$animationName} {
        $vendor: ''; @content;
    }
} $vendor: null;

@include defineKeyframes('spin') {
    from { #{$vendor}transform: rotate(0deg);}
    to { #{$vendor}transform: rotate(360deg);}
}

Check out the live DEMO here!

Answer №2

Just to keep the mods going...

@include makeKeyframes('badgeGlow') {
  from { @include box-shadow(0px 0px 10px rgba($glowToColor, 0.3), 0px 1px 2px rgba(0, 0, 0, .80));color:$glowBaseColor;border-color: $glowBaseColor;}
  50% { @include box-shadow(0px 0px 16px rgba($glowToColor, 0.8), 0px 1px 2px rgba(0, 0, 0, .80));color:white;border-color: lighten($glowBaseColor, 20);}
  to { @include box-shadow(0px 0px 10px rgba($glowToColor, 0.3), 0px 1px 2px rgba(0, 0, 0, .80));color:$glowBaseColor;border-color: $glowBaseColor;}
}

button.glow {
  @include setKeyframeType('badgeGlow',1.5s,infinite);
}

Note here that you call upon the keyFrame generator once, enabling you to reference the keyframes from various other classes using the setKeyframeType mixin and providing essential parameters. This is especially useful when dealing with numerous elements that require the same type of transitions.

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

Displaying default tab content while hiding other tab content in Javascript can be achieved through a simple code implementation

I have designed tabs using the <button> element and enclosed the tab content within separate <div></div> tags like shown below: function displayTab(evt, tabName) { var i, tabcontent, tablinks; tabcontent = document.getElementsB ...

Issue with TailwindCSS @apply not compiling in Angular v12 component styles

I am a computer science student working on a project for my studies. I am currently developing a component library for the Angular framework using Angular V12. To style the components, I have decided to use Tailwindcss as it simplifies some aspects like me ...

Infinite scrolling with a dynamic background

Hi there, I am working on my website and trying to create a smooth transition between sections similar to the one demonstrated here:. The challenge I'm facing is that the backgrounds of my sections cannot be fixed; they need to have background-attachm ...

How to Customize Bootstrap 4 Form Styles

Looking to customize the appearance of a checkbox in a web framework utilizing bootstrap 4, I attempted to override the custom-control-label class by setting the position property to auto instead of its default value of relative. Although I achieved the ...

Issue with AJAX request on Internet Explorer versions 8 and 9

This script is functioning properly in Internet Explorer 10 and above, as well as Chrome and other browsers. However, it encounters issues specifically with IE8 and IE9. <table id="table" border="1"> <tbody style="display: table-row-group"&g ...

Is there a way to create a div that resembles a box similar to the one shown in the

Hello, I am attempting to achieve a specific effect on my webpage. When the page loads, I would like a popup to appear at the center of the screen. To accomplish this, I have created a div element and initially set its display property to 'none'. ...

HTML list with padding

I need to display two lists side by side. In one list, I want to insert some space between two li elements (an empty li). For example, I am aiming for something that looks like this: A B A B A A B I tried adding an empty li, which worked ...

Tips for positioning text above a dashed line in the blade template using Laravel and Bootstrap 4

I am working on a small Laravel project where I need to pass the variable {{$docno}} to a blade template. Below is the code snippet in the blade file. <body> <div class="container-fluid"> <div class="text-center"> ...

Can you explain how the CSS hack *+html works?

While working on a project, I have encountered multiple rules that look like this: * + html { /.../ } Although I understand the purpose of using * and +, I am unsure about the significance of this particular construction. Additionally, I came across ...

The navigation bar remains fixed while the section heading fails to display properly

================================= My webpage acts like a homepage on the web. The issue arises when clicking on a new link, as my navbar is fixed and covers the section heading. I need the page to display the section heading properly even after clicking o ...

Adjusting Iframe to Fit Entire Screen Using CSS

I've been working on making an iframe adjust to fit the screen size, regardless of the user's resolution. However, no matter what I do, I can't seem to get it just right. It always ends up either too small or with a double scroll bar issue ( ...

Dynamic CSS Changes in AngularJS Animations

I am currently working on a multi-stage web form using AngularJS. You can see an example of this form in action by visiting the link below: http://codepen.io/kwakwak/full/kvEig When clicking the "Next" button, the form slides to the right smoothly. Howev ...

JavaScript-enhanced HTML form validation

I encountered a small glitch while working on simple form validation with JavaScript. I tried to catch the issue but have been unable to do so. Here is the code snippet, where the problem lies in the fact that the select list does not get validated and I ...

Tips for Centering a div When Dynamically Adding Contents

Take a look at this image https://i.sstatic.net/NUwDQ.png I utilized Bootstrap to align the divs and created only one div. The contents within this div are added dynamically from the admin side, then looped through. It's functioning properly. Howeve ...

Is there a way to remove text from a div when the div width is reduced to 0?

Upon loading the page, my menu is initially set to a width of 0px. When an icon is clicked, a jQuery script smoothly animates the menu's width to fill the entire viewport, displaying all menu items (links) perfectly. The issue I'm facing is that ...

Designing image hover effects using CSS

I am looking for guidance on how to achieve a similar effect to the one shown here: Specifically, I want to create a box with images that appears below a button when the user hovers over it. The box should remain visible as long as the user's mouse ...

Tips for efficiently moving through divs in a personalized single page application?

Exploring the world of JS frameworks and single page app functionality is a bit of a mystery to me. Currently, I have a pseudo single page app set up without any specific framework in place. The setup involves 3 tabs that toggle visibility for 3 different ...

Displaying and concealing a subcomponent within a parent element for a set duration of time

I have a notification component that is displayed as a child component in the parent component after a button click. It automatically hides after a certain number of seconds. Here is the code I have developed: MyCode Parent component: <button (click)= ...

PHP must be loaded before Javascript code will be executed

I am in the process of creating a high-tech "smart home" webpage, but I am facing an issue with the code execution order. Despite trying to use sleep() and other tactics, I can't seem to display the success message before running the script that reboo ...

What is the technique for rearranging columns to be at the top in Foundation for mobile devices?

On my desktop, I have a layout like this: [Column1 large-8] [Column2 large-4] For mobile view, I'd like it to be like this: [Column2 large-4] [Column1 large-8] Below is the code snippet I am working with: <div id="search-content" class="row ma ...