Tôi tìm thấy câu trả lời dựa trên mã nguồn và các ví dụ tại CSS3 transition tests github page.
Về cơ bản, hoạt ảnh CSS có sự kiện animationEnd
được kích hoạt khi hoạt ảnh hoàn tất.
Đối với trình duyệt webkit, sự kiện này được đặt tên là “webkitAnimationEnd
”. Vì vậy, để thiết lập lại một hình ảnh động sau khi nó được gọi là bạn cần phải thêm một event-listener vào phần tử cho sự kiện animationEnd
.
Trong plain vanilla javascript:
var element = document.getElementById('box');
element.addEventListener('webkitAnimationEnd', function(){
this.style.webkitAnimationName = '';
}, false);
document.getElementById('button').onclick = function(){
element.style.webkitAnimationName = 'shake';
// you'll probably want to preventDefault here.
};
và với jQuery:
var $element = $('#box').bind('webkitAnimationEnd', function(){
this.style.webkitAnimationName = '';
});
$('#button').click(function(){
$element.css('webkitAnimationName', 'shake');
// you'll probably want to preventDefault here.
});
Các mã nguồn cho CSS3 transition tests (nêu trên) có support
đối tượng sau đây có thể hữu ích cho chéo chuyển đổi CSS, chuyển đổi và hoạt ảnh CSS.
Đây là mã hỗ trợ (định dạng lại):
var css3AnimationSupport = (function(){
var div = document.createElement('div'),
divStyle = div.style,
// you'll probably be better off using a `switch` instead of theses ternary ops
support = {
transition:
divStyle.MozTransition === ''? {name: 'MozTransition' , end: 'transitionend'} :
// Will ms add a prefix to the transitionend event?
(divStyle.MsTransition === ''? {name: 'MsTransition' , end: 'msTransitionend'} :
(divStyle.WebkitTransition === ''? {name: 'WebkitTransition', end: 'webkitTransitionEnd'} :
(divStyle.OTransition === ''? {name: 'OTransition' , end: 'oTransitionEnd'} :
(divStyle.transition === ''? {name: 'transition' , end: 'transitionend'} :
false)))),
transform:
divStyle.MozTransform === '' ? 'MozTransform' :
(divStyle.MsTransform === '' ? 'MsTransform' :
(divStyle.WebkitTransform === '' ? 'WebkitTransform' :
(divStyle.OTransform === '' ? 'OTransform' :
(divStyle.transform === '' ? 'transform' :
false))))
//, animation: ...
};
support.transformProp = support.transform.name.replace(/([A-Z])/g, '-$1').toLowerCase();
return support;
}());
Tôi chưa thêm mã để phát hiện “hoạt hình” thuộc tính cho mỗi trình duyệt. Tôi đã đưa ra câu trả lời “wiki cộng đồng” và để lại câu trả lời đó cho bạn. :-)
là thư viện jQuery hoặc một thư viện tương tự? Bạn có thể thấy hỗ trợ trình duyệt tốt hơn. –
jQuery có sẵn nhưng không thực sự áp dụng trong trường hợp của tôi. Tôi không cần hỗ trợ trình duyệt khác vì điều này sẽ chỉ chạy trong Adobe Air. Tôi đang cố gắng để gắn bó với hình ảnh động css thay vì javascript. –
Có liên quan: [Firmin - Thư viện hoạt ảnh JavaScript sử dụng chuyển đổi CSS và chuyển tiếp] (http://extralogical.net/projects/firmin/) –