css - CSS3 Transform only plays first item -


i trying div rotate 360deg every time click it, using css3 transform rotate. however, i'm using css3 transform translate vertically align div.

on first click, applies required css doesn't rotate, rotate clicks after that. stays vertically aligned whole time.

unsure how solve , appreciated :)

my css:

    #my-div {         height: 300px;         width: 300px;         transition: 0.5s ease-in-out;         display: block;         margin: auto;          /*to vertically align*/         position: relative;         top: 50%;         -webkit-transform: translatey(-50%);         transform: translatey(-50%);     } 

my javascript

var angle = 360         $('#my-div').click(function() {             $(this).css({                 '-webkit-transform' : 'translatey(-50%) rotate('+angle+'deg) ',                 'transform' : 'translatey(-50%) rotate('+angle+'deg)'             })             angle += 360         }); 

in fact transition works properly when 2 ends explicitly set, here intially rotate transform not set explicitly, after first click, it's explicitly set rotate(360deg) , hence next clicks work. solve this, need apply rotate(0deg) div via css code:

#my-div {    /*...*/    -webkit-transform: translatey(-50%) rotate(0deg);     transform: translatey(-50%) rotate(0deg); } 

note emphasized on properly word, in fact if set initial angle angle equal or smaller 180deg, you'll see transitions ok. doubt if don't set initial rotate transform explicitly, behavior determined browser, 360deg won't make transition, otherwise rotating transition may clockwise (if angle % 360 less or equal 180deg) , counter-clockwise if angle % 360 greater 180deg (note modulo operation between angle , 360).

demo.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

django - CSRF verification failed. Request aborted. CSRF cookie not set -