Set the webkit-keyframes from/to parameter with JavaScript

后端 未结 5 1824
无人共我
无人共我 2020-12-05 14:29

Is there any way to set the from or to of a webkit-keyframe with JavaScript?

5条回答
  •  不思量自难忘°
    2020-12-05 14:39

    A solution of sorts:

    var cssAnimation = document.createElement('style');
    cssAnimation.type = 'text/css';
    var rules = document.createTextNode('@-webkit-keyframes slider {'+
    'from { left:100px; }'+
    '80% { left:150px; }'+
    '90% { left:160px; }'+
    'to { left:150px; }'+
    '}');
    cssAnimation.appendChild(rules);
    document.getElementsByTagName("head")[0].appendChild(cssAnimation);
    

    Just adds a style definition to the header. Would be much cleaner/better to define it though the DOM if possible.

    Edit: Error in Chrome with old method

提交回复
热议问题