I try to set up this LESS mixin for CSS animation keyframes:
.keyframes(@name, @from, @to) {;
@-webkit-keyframes "@name" {
from {
@from;
}
to {
@to;
}
}
}
but there is some problem with name pharse, is there any option to do this corectly?
As of LESS >= 1.7 you can use variables for keyframe keywords (names).
Some changes have been made in LESS 1.7 to how directives work, which allows to use variables for the name/keyword of
@keyframes
(so the example from the question should work now). DEMOHence, the normal way of going about keyframes would use hardcoded names and you would only call for specific "for" and "to" mixins, like this:
But you can use a workaround to dynamically generate the names
where you inject the name into the rule name, this however requires an declaration of the next rule that supplies the closing bracket
}
at the end of the keyframes declaration. The most convenient is if you just build the animation calling that keyframeNote that you also need to define
.from(){}
and.to(){}
mixins, and not just use@from
and@to
like you did in your example (because LESS also does not allow for dynamically generated properties) ... this mixins can now construct the desired properties and values ... to use specific property you can use guards or name-specific mixins like these:Now we can call our mixin in LESS:
and get CSS:
this will work also in LESS 1.4, but note that we used javascript interpolation for line breaks, which requires a javascript implementation of LESS.
Edit: to your additional question about prefixes
Mixin with vendor prefixes
Here I made two mixins ... one without vendor prefixes and one with them both calling a general
.keyframes
mixin:The
.animation-keyframes
will now produce keyframes for all vendor prefixes and an animation selector with vendor prefixed properties. And as expected the.animation-keyframes-novendor
gives the same output as the above simple solution (without vendor prefixes).Some notes:
For your animation to actually work you need to set other animation parameters like timing-function, duration, direction, iteration-count (requires at least a duration time in addition to the name that we already set).
For example:
If you wrap above mixins in namespaces make sure you change the mixin references inside other mixins to their whole path (including the namespaces).
For example:
DEMO
Before-mentioned https://github.com/kuus/animate-me.less does things!
You can also check out this one written by me (seems to be neater): https://github.com/thybzi/keyframes
I think you should do this
change
"@name"
to@name
and you should delete
;
afterAlso thanks to the great answer by Martin Turjak, (thank you for that) I just put on github a less mixin which generate keyframes and animation's css code without hacks and in a flexible way, you can find it here github.com/kuus/animate-me.less.
With this mixin you can write this code to obtain valid and cross browser css (see the github repo for a complete explanation):
I am currently working on a mixin library
The source can be found here https://github.com/pixelass/more-or-less
My keyframe mixin looks like this:
WORKS FOR LESS 1.7.x
MIXIN
INPUT
OUTPUT
How about this:
You need to do it for each animation. Taken from: http://radiatingstar.com/css-keyframes-animations-with-less