Skip to content

Instantly share code, notes, and snippets.

@nicooprat
Last active March 9, 2021 07:58
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nicooprat/11271821 to your computer and use it in GitHub Desktop.
Save nicooprat/11271821 to your computer and use it in GitHub Desktop.
A simple and elegant keyframes mixin for LESS
// --------------
// Needs LESS compiler >= 1.7.0
// Docs : http://lesscss.org/features/#detached-rulesets-feature
// --------------
// 1. Define the mixin for cross browser keyframes
.keyframes(@name,@rules) {
@-webkit-keyframes @name {
@rules();
}
@-moz-keyframes @name {
@rules();
}
@-ms-keyframes @name {
@rules();
}
@-o-keyframes @name {
@rules();
}
@keyframes @name {
@rules();
}
}
// 2. Use the mixin to create an animation
.keyframes(pulse, {
0% , 50% , 100% {
opacity: 0;
}
25% , 75% {
opacity: 1;
}
});
// 3. Apply your animation
.pulse {
animation: pulse 0.5s 1 both;
-ms-animation: pulse 0.5s 1 both;
-moz-animation: pulse 0.5s 1 both;
-webkit-animation: pulse 0.5s 1 both;
-o-animation: pulse 0.5s 1 both;
}
/* It should output something like this... Yeah ! */
@-webkit-keyframes pulse {
0%,
50%,
100% {
opacity: 0;
}
25%,
75% {
opacity: 1;
}
}
@-moz-keyframes pulse {
0%,
50%,
100% {
opacity: 0;
}
25%,
75% {
opacity: 1;
}
}
@-ms-keyframes pulse {
0%,
50%,
100% {
opacity: 0;
}
25%,
75% {
opacity: 1;
}
}
@-o-keyframes pulse {
0%,
50%,
100% {
opacity: 0;
}
25%,
75% {
opacity: 1;
}
}
@keyframes pulse {
0%,
50%,
100% {
opacity: 0;
}
25%,
75% {
opacity: 1;
}
}
.pulse {
animation: pulse 0.5s 1 both;
-ms-animation: pulse 0.5s 1 both;
-moz-animation: pulse 0.5s 1 both;
-webkit-animation: pulse 0.5s 1 both;
-o-animation: pulse 0.5s 1 both;
}
@aigzhep
Copy link

aigzhep commented Mar 12, 2019

Error: expected @keyframes identifier
Less thinks it is a variable

@ERrorMAKros
Copy link

Error: expected @Keyframes identifier

@nicooprat
Copy link
Author

nicooprat commented Feb 27, 2020

Sorry this code is 5 years old and I don't use Less anymore. I think you no longer need to use vendor prefixes for animations though.

@StrayLove
Copy link

id love to know how to use it keyframes on spin and slidein personally. there really needs to be a small library for keyframes in less i tell ya. if it werent for a school project i wouldnt even be bothering.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment