0

I'm trying to get CSS3 transitions to work in JQuery .css()

Something simple like

.someDiv { opacity: 1; background: #fff; transition: width 2s; }

formats into

$( ".box" ).one( "click", function() {
$(this).css({ "opacity": "1", "background": "#fff", "transition": "width 2s" });
});

I know that much.
But how do you format CSS3 transitions that have nested keyframes to work with JQuery?

.fadeInDown {
 -webkit-animation-name: fadeInDown;  
 animation-name: fadeInDown;  }

@keyframes fadeInDown {

0% {
    opacity: 0;
    -webkit-transform: translate3d(0, -100%, 0);
     transform: translate3d(0, -100%, 0);
    }

100% {
        opacity: 1;
        -webkit-transform: none;
        transform: none;
    }
} 
1
  • Why aren't you just using a CSS file for this? Commented Jun 19, 2017 at 16:56

2 Answers 2

2

Don't hold me up on any of this.

It's not so simple. Consider the following:

jQuery has a very nice built in $(selector).animate() function which allows for easy setup of these animations. However, jQuery's animate() does not support multiple keyframes

With that being said, you can include the jQuery.Keyframes library which would assit you in achieving what you want.

Just using the code you provided, you would be able to introduce the animation via jQuery -given that you add jQuery.Keyframes - with something like this:

$.keyframe.define([{
  name: 'animation-name',
  '0%': {
    'opacity': '0',
    'transform': 'translate3d(0, -100%, 0)'
  },
  '100%': {
    'opacity': '1',
    'transform': 'none'
  }
}]);

Sign up to request clarification or add additional context in comments.

Comments

1

Not sure why you would want to add keyframes using jQuery, but you can add CSS to the head like this:

$(function() {
  var keyframes = '.fadeInDown {' +
                     '-webkit-animation-name: fadeInDown;' +
                     'animation-name: fadeInDown; }' +

                    '@keyframes fadeInDown {' +

                    '0% {' +
                        'opacity: 0;' +
                        '-webkit-transform: translate3d(0, -100%, 0);' +
                        'transform: translate3d(0, -100%, 0);' +
                        '}' +

                    '100% {' +
                            'opacity: 1;' +
                            '-webkit-transform: none;' +
                            'transform: none;' +
                        '}' +
                    '}';
  $('<style type="text/css">' + keyframes + '</style>').appendTo($('head'));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

1 Comment

Much easier and 100% solution!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.