CSS dönüş tarayıcı jquery ile çapraz.()animasyon
Çapraz tarayıcı uyumlu bir dönüş (9 ) yaratmaya çalışıyorum ve jsfiddle aşağıdaki kodu var
$(document).ready(function () {
DoRotate(30);
AnimateRotate(30);
});
function DoRotate(d) {
$("#MyDiv1").css({
'-moz-transform':'rotate(' d 'deg)',
'-webkit-transform':'rotate(' d 'deg)',
'-o-transform':'rotate(' d 'deg)',
'-ms-transform':'rotate(' d 'deg)',
'transform': 'rotate(' d 'deg)'
});
}
function AnimateRotate(d) {
$("#MyDiv2").animate({
'-moz-transform':'rotate(' d 'deg)',
'-webkit-transform':'rotate(' d 'deg)',
'-o-transform':'rotate(' d 'deg)',
'-ms-transform':'rotate(' d 'deg)',
'transform':'rotate(' d 'deg)'
}, 1000);
}
CSS ve HTML çok basit ve sadece demo
.SomeDiv{
width:50px;
height:50px;
margin:50px 50px;
background-color: red;}
<div id="MyDiv1" class="SomeDiv">test</div>
<div id="MyDiv2" class="SomeDiv">test</div>
Dönüş .animate()
. neden Peki bunu düzeltmenin bir yolu var mı? kullanırken .css()
ama kullanırken çalışır
Teşekkürler.
CEVAP
CSS-Dönüşümler jQuery ile animasyon yapmak mümkün, henüz değil. Böyle bir şey yapabilirsiniz:
function AnimateRotate(angle) {
// caching the object for performance reasons
var $elem = $('#MyDiv2');
// we use a pseudo object for the animation
// (starts from `0` to `angle`), you can name it as you want
$({deg: 0}).animate({deg: angle}, {
duration: 2000,
step: function(now) {
// in the step-callback (that is fired each step of the animation),
// you can use the `now` paramter which contains the current
// animation-position (`0` up to `angle`)
$elem.css({
transform: 'rotate(' now 'deg)'
});
}
});
}
Daha fazla adım geri çağırma buradan okuyabilirsiniz: http://api.jquery.com/animate/#step
Ve, btw: css3 jQuery 1.7 ile dönüşümler önek gerek yok
Güncelleme
JQuery-plugin bu hayatınızı biraz daha kolaylaştırmak için sarın
$.fn.animateRotate = function(angle, duration, easing, complete) {
return this.each(function() {
var $elem = $(this);
$({deg: 0}).animate({deg: angle}, {
duration: duration,
easing: easing,
step: function(now) {
$elem.css({
transform: 'rotate(' now 'deg)'
});
},
complete: complete || $.noop
});
});
};
$('#MyDiv2').animateRotate(90);
http://jsbin.com/ofagog/2/edit
Update2
Biraz easing
, duration
complete
sırası önemsiz yapmak için optimize edilmiş.
$.fn.animateRotate = function(angle, duration, easing, complete) {
var args = $.speed(duration, easing, complete);
var step = args.step;
return this.each(function(i, e) {
args.complete = $.proxy(args.complete, e);
args.step = function(now) {
$.style(e, 'transform', 'rotate(' now 'deg)');
if (step) return step.apply(e, arguments);
};
$({deg: 0}).animate({deg: angle}, args);
});
};
Güncelleme 2.1
Tam-callback
this
-içerik ile ilgili bir sorun olduğunu fark eden matteo için teşekkürler. Eğer tamir olursabağlamaher düğümde jQuery.proxy
ile geri.
Daha önce kod edition ekledimGüncelleme 2.
Kullanımı...oldukça basit!
Çoğunlukla istenilen sonuca ulaşmak için iki yol var. Ama ilk başta, bağımsız bir göz atalım:
jQuery.fn.animateRotate(angle, duration, easing, complete)
"Açı" hepsi varsayılan jQuery.fn.animate
-özellikleri için isteğe bağlı ve geri dönüş: . hariç
duration: 400
easing: "swing"
complete: function () {}
1
Bu yol çok kısa olur, ama biraz belirsiz görünüyor daha bağımsız geçiyoruz.
$(node).animateRotate(90);
$(node).animateRotate(90, function () {});
$(node).animateRotate(90, 1337, 'linear', function () {});
2
Eğer üç veya daha fazla bağımsız değişken varsa orada nesneleri kullanmayı tercih eder, bu sözdizimi benim favori:
$(node).animateRotate(90, {
duration: 1337,
easing: 'linear',
complete: function () {},
step: function () {}
});
Tarayıcı JavaScript (jQuery...) en iyi...
En iyi çapraz tarayıcı yakalama yöntem...
Çapraz tarayıcı penceresi yeniden boyu...
Çapraz tarayıcı (hiçbir Prototip/jQuer...
JavaScript yapıştırın olay (Çapraz tar...