I've created a dynamic one with the example you gave, I calculate through JS the amount of li and then I calculate what's the % of the keyframe by dividing 100 for the number of li's
inside the ul
. You can check the example here:
https://jsfiddle.net/erv1b8nh/2/
The js is the following:
var style = document.documentElement.appendChild(document.createElement("style")),
rule1 = " ticker { 0% { margin-top:0; }";
rule2 = " 100% { margin-top:0; } }";
var n = $('#news li').length;
var rule3 = "";
var help = 1;
var nSpin = (100/n).toFixed(2);
while(n > 1)
{
rule3 = rule3 + " " + nSpin*help + "% { margin-top:" + help*-27 + "px; }";
help = help + 1;
n = n - 1;
}
rule = rule1 + rule3 + rule2;
if (CSSRule.KEYFRAMES_RULE) { // W3C
style.sheet.insertRule("@keyframes" + rule, 0);
} else if (CSSRule.WEBKIT_KEYFRAMES_RULE) { // WebKit
style.sheet.insertRule("@-webkit-keyframes" + rule, 0);
}
Of course then based on the amount of elements you should adjust the animation time.
Hope it will help you :)