Perché non usare semplicemente, toggleClass al clic?
JS:
$(this).toggleClass("up");
css:
button.up {
-webkit-transform: rotate(180deg);
-moz-transform: rotate(180deg);
-ms-transform: rotate(180deg);
-o-transform: rotate(180deg);
transform: rotate(180deg);
/* IE6–IE9 */
filter: progid:DXImageTransform.Microsoft.Matrix(M11=0.9914448613738104, M12=-0.13052619222005157,M21=0.13052619222005157, M22=0.9914448613738104, sizingMethod='auto expand');
zoom: 1;
}
è possibile aggiungere anche questo al css:
button{
-webkit-transition: all 500ms ease-in-out;
-moz-transition: all 500ms ease-in-out;
-o-transition: all 500ms ease-in-out;
-ms-transition: all 500ms ease-in-out;
}
che aggiungerà l'animazione.
PS ...
per rispondere alla tua domanda iniziale:
hai detto che ruoti, ma non si ferma mai. Quando si utilizza il timeout impostato è necessario assicurarsi di avere una condizione che non chiamerà settimeout altrimenti funzionerà per sempre.Così, per il codice:
<script type="text/javascript">
$(function() {
var $elie = $("#bkgimg");
rotate(0);
function rotate(degree) {
// For webkit browsers: e.g. Chrome
$elie.css({ WebkitTransform: 'rotate(' + degree + 'deg)'});
// For Mozilla browser: e.g. Firefox
$elie.css({ '-moz-transform': 'rotate(' + degree + 'deg)'});
/* add a condition here for the extremity */
if(degree < 180){
// Animate rotation with a recursive call
setTimeout(function() { rotate(++degree); },65);
}
}
});
</script>
Questo solo ruota l'immagine. Lo voglio davvero animare/ruotare e fermarsi a un certo punto o grado. Scusa, avrei dovuto dirlo prima. Grazie. – Andrea
@Andrea, guardo la tua pagina in Safari e l'immagine viene ruotata di 45 gradi. Guardando il DOM nella console, è stato mostrato il corretto CSS di '-webkit-transform'. Forse il tuo browser non supporta CSS 3. – Sparky
@Andrea, cambia semplicemente '.css' in' .animate'. Vedi la mia modifica. – Sparky