css animation 語(yǔ)法:animation: name duration timing-function delay iteration-count direction fill-mode play-state;
參數(shù):

場(chǎng)景:點(diǎn)擊下面pagination彈出上方大圓球,要求動(dòng)畫起始點(diǎn)為點(diǎn)擊的pagination的位置。

解析:動(dòng)畫實(shí)現(xiàn)很簡(jiǎn)單,大概這樣:
@-webkit-keyframes fadeInContent4 {
from {
opacity: 0.3;
-webkit-transform: translate3d(665px, 8px, 0) scale3d(0.254, 0.114, 1);
transform: translate3d(665px, 8px, 0) scale3d(0.254, 0.114, 1);
}
to {
opacity: 1;
-webkit-transform: translate3d(0, 0, 0) scale3d(1, 1, 1);
transform: translate3d(0, 0, 0) scale3d(1, 1, 1);
}
}
@keyframes fadeInContent4 {
from {
opacity: 0.3;
-webkit-transform: translate3d(665px, 8px, 0) scale3d(0.254, 0.114, 1);
transform: translate3d(665px, 8px, 0) scale3d(0.254, 0.114, 1);
}
to {
opacity: 1;
-webkit-transform: translate3d(0, 0, 0) scale3d(1, 1, 1);
transform: translate3d(0, 0, 0) scale3d(1, 1, 1);
}
}
.fadeInContent4 {
-webkit-animation-name: fadeInContent4;
animation-name: fadeInContent4;
-webkit-animation-duration: 0.6s;
animation-duration: 0.6s;
}
執(zhí)行動(dòng)畫:$(".content-animation").addClass("fadeInContent4");
但是:
這里有4個(gè)pagination(動(dòng)態(tài)的話就是n個(gè)),意味著上面css的代碼要copy 4份,然后去修改translate參數(shù),所以推薦使用js循環(huán)insertRule去插入動(dòng)畫樣式。
function insertAnimationStyle() {
let styleEl = document.createElement('style');
document.head.appendChild(styleEl);
$(".tab-menu li").each(function (index, element) {
// element == this
var tabtopLoaction = -232 + 60 * (index);
var tabItemAnimation3 = " \
.fadeInContent" + index +" {\
-webkit-animation-name: fadeInContent" + index +";\
animation-name: fadeInContent" + index +";\
-webkit-animation-duration: 0.3s;\
animation-duration: 0.3s;\
}\
";
styleEl.sheet.insertRule(tabItemAnimation3, 0);
var tabItemAnimation2 = " \
@keyframes fadeInContent" + index +" {\
from {\
opacity: 0.3;\
-webkit-transform:translate3d(665px, " +tabtopLoaction +"px, 0) scale3d(0.254,0.114, 1);\
transform: translate3d(665px, " +tabtopLoaction + "px, 0) scale3d(0.254,0.114, 1);\
}\
to {\
opacity: 1;\
-webkit-transform:translate3d(0, 0, 0) scale3d(1,1,1);\
transform:translate3d(0, 0, 0) scale3d(1,1,1);\
}\
}";
styleEl.sheet.insertRule(tabItemAnimation2, 0);
var tabItemAnimation1 = " \
@-webkit-keyframes fadeInContent" + index +" {\
from {\
opacity: 0.3;\
-webkit-transform: translate3d(665px, " + tabtopLoaction +"px, 0) scale3d(0.254,0.114, 1);\
transform: translate3d(665px, " +tabtopLoaction +"px, 0) scale3d(0.254,0.114, 1);\
}\
to {\
opacity: 1;\
-webkit-transform: translate3d(0, 0, 0) scale3d(1,1,1);\
transform: translate3d(0, 0, 0) scale3d(1,1,1);\
}\
}";
styleEl.sheet.insertRule(tabItemAnimation1, 0);
});
};
執(zhí)行動(dòng)畫:
$tabMenu.on('click', function () {
var clickIndex = $(this).index();
$(".content-wapper").addClass("fadeInContent" + clickIndex);
var fankanv = "fadeInContent" + clickIndex;
$(".content-animation").css('animation', '' + fankanv + ' 0.6s ease 0s 1 normal none running');
});
Debug:
這里一定要注意每次
insertRule的參數(shù)只能有一條cssStyle
錯(cuò)誤日志:
火狐報(bào)錯(cuò)firefox: SyntaxError: An invalid or illegal string was specified
谷歌報(bào)錯(cuò)google: Uncaught DOMException: Failed to execute 'insertRule' on 'CSSStyleSheet': Failed to parse the rule
mozilla文檔解釋insertRule錯(cuò)誤(??如果你遇到上面的問題請(qǐng)點(diǎn)這里)
說的非常清楚,一次只能插入一條。

over,這篇主要記錄一下前端debug的過程,百度是不可能百度了,只有stackoverflow這樣子
補(bǔ)充:
這里安利一下:animation.css,animation.css已經(jīng)幫我們實(shí)現(xiàn)了很多常用的加載動(dòng)畫,真的很方便,star竟然比webpak還高??
添加動(dòng)畫:
$(element).addClass("animated fadeInUpBig");
動(dòng)畫完成監(jiān)聽:
$('#element').one('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', function () {
//todo
$(".tab-menu li").each(function (index, element) {
// element == this
var listarttime = index * 100;
setTimeout(function () { //循環(huán)添加動(dòng)畫
$(element).show();
$(element).addClass("animated fadeInUpBig");//這里注意this作用域
}, listarttime);
});
});