Tried searching for this but it's difficult given the syntax. Is there any way to generate a random number in LESS? I checked the documentation and don't see anything, but wondered if anyone knew of a trick or undocumented solution.
相关问题
- Adding a timeout to a render function in ReactJS
-
Why does the box-shadow property not apply to a
- Add animation to jQuery function Interval
- jQuery hover to slide?
- Issue with star rating css
According to the documentation:
So this should work:
By a LESS Mixin for Variation
By making a LESS mixin to generate the random number, you can call it each place as needed with easier control of the output. This code was built in part from the help of this SO answer, which allows you to control the output range of the random number, and whether it outputs decimals or integers.
LESS Define Mixin
The above will output a variable labeled
@randNum
for each time the mixin is called. So then this can be done:LESS Use Mixin
Which yields an output something along these lines (of course, the numbers will change with each compilation from LESS):
CSS Output
Of course, I realize you would probably in most cases not be directly outputting these random numbers, but rather using them in some other calculation. But this illustrates how the mixin can be used.
I also realize this does not resolve any randomness with respect to the same class usage. In other words, any element with
.rand3
class above will have15
as its number. I believe this is the issue you ran into based on your comment:That is just the fact of life for LESS being a preprocessor of CSS. To get randomness across similar elements via LESS you would need to generate the random numbers from this mixin by a series of classes via some sort of a loop structure and apply each class of the series to the various elements to get the randomness.