Set the webkit-keyframes from/to parameter with JavaScript

后端 未结 5 1801
无人共我
无人共我 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

    0 讨论(0)
  • 2020-12-05 14:39

    You can use the CSS DOM interface. For instance:

    <html>
        <body>
            <style>
            @keyframes fadeout {
                from { opacity:1; }
                to { opacity:0; }
            }
            </style>
            <script text="javascript">
                var stylesheet = document.styleSheets[0];
                var fadeOutRule = stylesheet.cssRules[0];
                alert( fadeOutRule.name ); // alerts "fadeout"
    
                var fadeOutRule_From = fadeOutRule.cssRules[0];
                var fadeOutRule_To = fadeOutRule.cssRules[1];
                alert( fadeOutRule_From.keyText ); // alerts "0%" ( and not "from" as you might expect)
                alert( fadeOutRule_To.keyText ); // alerts "100%"
    
                var fadeOutRule_To_Style = fadeOutRule_To.style;
    
                alert( fadeOutRule_To_Style.cssText ); // alerts "opacity:0;"
    
                fadeOutRule_To_Style.setProperty('color', 'red'); // add the style color:red
                fadeOutRule_To_Style.removeProperty('opacity'); // remove the style opacity
    
                alert( fadeOutRule_To_Style.cssText ); // alerts "color:red;"
            </script>
        </body>
    </html>
    
    0 讨论(0)
  • 2020-12-05 14:49

    This example covers several different browsers:

    var keyFramePrefixes = ["-webkit-", "-o-", "-moz-", ""];
    var keyFrames = [];
    var textNode = null;
    
    for (var i in keyFramePrefixes){
    
    keyFrames = '@'+keyFramePrefixes[i]+'keyframes cloudsMove {'+
    'from {'+keyFramePrefixes[i]+'transform: translate(0px,0px);}'+
    'to {'+keyFramePrefixes[i]+'transform: translate(1440px'
    'px,0px);}}';
    
    textNode = document.createTextNode(keyFrames);
    document.getElementsByTagName("style")[0].appendChild(textNode);
    }
    
    0 讨论(0)
  • 2020-12-05 14:57

    To solve this I added a 'webkit animation name' to my CSS selector and then created separate rules for my options, in my example red and yellow colouring:

    .spinner {
    -webkit-animation-name: spinnerColorRed;
    }
    
    @-webkit-keyframes spinnerColorRed {
      from {
        background-color: Black;
      }
      to {
        background-color: Red;
      }
    }
    
    @-webkit-keyframes spinnerColorYellow {
      from {
        background-color: Black;
      }
      to {
        background-color: Yellow;
      }
    }
    

    Then using jQuery:

    $("#link").click(function(event) { 
      event.preventDefault();
      $(".spinner").css("-webkit-animation-name", "spinnerColorYellow");
    });
    
    0 讨论(0)
  • 2020-12-05 15:01

    The way I handle this is to not set either the from or to of the element style I am manipulating in the css file and before triggering the animation I will set the element style that it should go to with javascript. This way you are free to dynamically manage what stuff should do until we can manage this directly in js. You only need to specify one of the two. The setTimeout allows the application of the css rule to the element before the animation is triggered otherwise you would have a race condition and it wouldn't animate.

    
    #someDiv.slideIn {
        -webkit-animation: slideIn 0.5s ease;
    }
    
    @-webkit-keyframes slideIn {
        0% {
            left:0px;
        }
    
        100% {}
    }
    
    
    var someDiv = document.getElementById('someDiv');
    someDiv.style.left = '-50px';
    setTimeout(function(){
        someDiv.addClass('slideIn');
    },0);
    
    0 讨论(0)
提交回复
热议问题