3

I am trying to animaiate a circle in a square motion animation using jQuery and css transition, for some reason only the last css jquery animtion works. i know i can accomplish a series of animations with jQuery animate function but i want to now if its possible to do it with css transition and jquery ,any suggestions how to fix it

     $(document).ready(function(){

           $('h1').click(function(){
               if($('.container').hasClass('isMoved')){
                   $('.container').css({
                       '-webkit-transform': 'translatex(0px)',
                       'opacity' : '1'
                   });
                   $('.container').removeClass('isMoved')
               }else{
                   $('.container').css({
                       '-webkit-transform': 'translatex(350px) ',
                       'opacity' : '0.6'
                   });
                   $('.container').css({
                       '-webkit-transform': 'translatey(350px) ',
                       'opacity' : '0.6'
                   });
                   $('.container').css({
                       '-webkit-transform': 'translatex(0px) ',
                       'opacity' : '0.6'
                   });
                   $('.container').css({
                       '-webkit-transform': 'translatey(0px) ',
                       'opacity' : '0.6'
                   });
                   $('.container').addClass('isMoved');
               }

           });

       });

    <style>
    body{
        background-color:darkcyan;
    }   

    .container{
        width:100px;
        height:100px;
        border-radius: 100px;
        background-color:aquamarine;
        position:absolute;    
        -webkit-transition: all 2s ease-in-out;      
    }
</style>

</head>
 <body>
  <h1>Click</h1>
  <div class='container'>

</div>
</body>

1 Answer 1

2

You are applying all of the css commands at the same time.

You could set them to happen in a timeline, for example...

Apply the first CSS command

$('.container').css({
  '-webkit-transform': 'translatex(350px) ',
  'opacity' : '0.6'
});

Then set your next CSS command to run a set period of time afterwards...say 1 second (1000 milliseconds)

setTimeout(function() {

  $('.container').css({
   '-webkit-transform': 'translatey(350px) ',
   'opacity' : '0.6'
  });

}, 1000);

Then perhaps wait another second for the next command (now 2 seconds)

 setTimeout(function() {

  $('.container').css({
   '-webkit-transform': 'translatex(0px) ',
   'opacity' : '0.6'
  });

}, 2000);

And you could carry this on, remember to keep adding to the time.

Does this make sense?

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

Comments

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.