首先说一下思路叭,想了半天
想法:一、控制css依次执行样式,9个红包平均分成9份,占百分百多少,9个动画控制在一个动画里面(很好写的很快,但是还有一个要求,点击一个动画,跳过这个动画执行下一个)能实现,但是想想好大的代码量还是算了
二、js控制动画的执行与不执行,设置动画循环播放,设置时间戳8s执行一次,依次执行动画(想法很好,但是实现起来会出现动画错乱,原因第一次执行设置时间的问题)设置第一次动画规定执行时间,其他动画全部依赖第一次动画执行而执行,最后一个动画重置css样式重新去执行,点击红包直接跳过执行下一个,很好如果点击第一个怎么办???????????
三、同第二步,第一次动画做起点执行完暂停,执行第二个,如果第二个被点击执行第三个,依次类推,最后一个动画启动第一个动画无限循环。
第一想法代码(没有最后实现哈这这是依次执行而已)
.goods{
width:20%;
height:atuo;
position:relative;
animation:mymove 9s infinite ;
-webkit-animation:mymove 9s infinite ;
-webkit-animation-iteration-count:infinite;
}
.goods1{
width:20%;
height:atuo;
position:relative;
animation:mymove1 9s infinite ;
-webkit-animation:mymove1 9s infinite ;
-webkit-animation-iteration-count:infinite;
}
.goods2{
width:20%;
height:atuo;
position:relative;
animation:mymove2 9s infinite ;
-webkit-animation:mymove2 9s infinite ;
-webkit-animation-iteration-count:infinite;
}
.goods3{
width:20%;
height:atuo;
position:relative;
animation:mymove3 9s infinite ;
-webkit-animation:mymove3 9s infinite ;
-webkit-animation-iteration-count:infinite;
}
.goods4{
width:20%;
height:atuo;
position:relative;
animation:mymove4 9s infinite ;
-webkit-animation:mymove4 9s infinite ;
-webkit-animation-iteration-count:infinite;
}
.goods5{
width:20%;
height:atuo;
position:relative;
animation:mymove5 9s infinite ;
-webkit-animation:mymove5 9s infinite ;
-webkit-animation-iteration-count:infinite;
}
.goods6{
width:20%;
height:atuo;
position:relative;
animation:mymove6 9s infinite ;
-webkit-animation:mymove6 9s infinite ;
-webkit-animation-iteration-count:infinite;
}
.goods7{
width:20%;
height:atuo;
position:relative;
animation:mymove7 9s infinite ;
-webkit-animation:mymove7 9s infinite ;
-webkit-animation-iteration-count:infinite;
}
.goods8{
width:20%;
height:atuo;
position:relative;
animation:mymove8 9s infinite ;
-webkit-animation:mymove8 9s infinite ;
-webkit-animation-iteration-count:infinite;
}
@-webkit-keyframes mymove /* Safari and Chrome */
{
0%{top: 0px;left: 0px;}
2.75%{top: -0.5em;left: 0px;}
5.5%{top: -1em;left: 0px;}
8.25%{top: -0.5em;left: 0px;}
11%&{top: 0px;left: 0px;}
}
@-webkit-keyframes mymove1 /* Safari and Chrome */
{
11%{top: 0px;left: 0px;}
13.75%{top: -5px;left: 0px;}
16.5%{top: -10px;left: 0px;}
19.25%{top: -5px;left: 0px;}
22%&{top: 0px;left: 0px;}
}
@-webkit-keyframes mymove2 /* Safari and Chrome */
{
22%{top: 0px;left: 0px;}
24.75%{top: -0.5em;left: 0px;}
27.5%{top: -1em;left: 0px;}
30.25%{top: -0.5em;left: 0px;}
33%&{top: 0px;left: 0px;}
}
@-webkit-keyframes mymove3 /* Safari and Chrome */
{
33%{top: 0px;left: 0px;}
35.75%{top: -0.5em;left: 0px;}
38.5%{top: -1em;left: 0px;}
41.25%{top: -0.5em;left: 0px;}
44%&{top: 0px;left: 0px;}
}
@-webkit-keyframes mymove4 /* Safari and Chrome */
{
44%{top: 0px;left: 0px;}
46.75%{top: -0.5em;left: 0px;}
49.5%{top: -1em;left: 0px;}
52.25%{top: -0.5em;left: 0px;}
55%&{top: 0px;left: 0px;}
}
@-webkit-keyframes mymove5 /* Safari and Chrome */
{
55%{top: 0px;left: 0px;}
57.75%{top: -0.5em;left: 0px;}
60.5%{top: -1em;left: 0px;}
63.25%{top: -0.5em;left: 0px;}
66&{top: 0px;left: 0px;}
}
@-webkit-keyframes mymove6 /* Safari and Chrome */
{
66%{top: 0px;left: 0px;}
68.75%{top: -0.5em;left: 0px;}
71.5%{top: -1em;left: 0px;}
74.25%{top: -0.5em;left: 0px;}
77&{top: 0px;left: 0px;}
}
@-webkit-keyframes mymove7 /* Safari and Chrome */
{
77%{top: 0px;left: 0px;}
79.75%{top: -0.5em;left: 0px;}
82.5%{top: -1em;left: 0px;}
85.25%{top: -0.5em;left: 0px;}
88&{top: 0px;left: 0px;}
}
@-webkit-keyframes mymove8 /* Safari and Chrome */
{
88%{top: 0px;left: 0px;}
90.75%{top: -0.5em;left: 0px;}
93.5%{top: -1em;left: 0px;}
96.25%{top: -0.5em;left: 0px;}
100%&{top: 0px;left: 0px;}
}
很好我们把动画做出来了,是挺好看,但是在广告上不是光给人看的,还需要点击,那一部分隐藏,就有了时间差,所有我们略过第二想法直接看第三想法。
第三想法代码
首先我们先写css动画
.deep{
position: absolute;
width: 2.5rem;
height: 2.5rem;
float: left;
left: 2rem;
top: 10rem;
z-index: 100;
-webkit-animation: bounce-up 1s infinite;
animation: bounce-up 1s infinite;
animation-fill-mode: forwards;
animation-play-state: running;
}
@-webkit-keyframes bounce-up {
0% {-webkit-transform: translateY(0rem);}
50% {-webkit-transform: translateY(-1.5rem);}
100%{-webkit-transform: translateY(-0rem);}
}
只有开始的动画设置infinite属性。如果不设置的话只会播放一边。即便你js里设置了style.animationPlayState = ‘running’;
第一次动画监听循环结束
var $main = $('.fudai1');
//click1-9是全局变量,初始值0,被点击1
//重复播放
$main.on('animationiteration', function(e) {
console.log(click2+"-----click2--------");
if(click9==1){//第一个被点击
run2();
if(click2==1){//同时第二个被点击
run3();
if(click3==1){
run4();
if(click4==1){
run5();
if(click5==1){
run6();
if(click6==1){
run7();
if(click7==1){
run8();
if(click8==1){
run9();
}
}
}
}
}
}
}
}else{
if(click2==1){//第二个被点击
run3();
if(click3==1){//同时第三个被点击
run4();
if(click4==1){
run5();
if(click5==1){
run6();
if(click6==1){
run7();
if(click7==1){
run8();
if(click8==1){
run9();
if(click9==1){
run1();
}
}
}
}
}
}
}
}
run2();
}
a.style.animationName = "";//重置classanimationName属性,下次进入
});
第二次动画监听播放结束
var $main2 = $('.fudai2');
//监听动画的结束
$main2.on('animationend', function(e) {
if(click2==1){
run3();
if(click3==1){
run4();
if(click4==1){
run5();
if(click5==1){
run6();
if(click6==1){
run7();
if(click7==1){
run8();
if(click8==1){
run9();
if(click9==1){
run1();
}
}
}
}
}
}
}
}else{
if(click3==1){
run4();
if(click4==1){
run5();
if(click5==1){
run6();
if(click6==1){
run7();
if(click7==1){
run8();
if(click8==1){
run9();
if(click9==1){
run1();
}
}
}
}
}
}
}
console.log('三号红包没被点击');
run3();
}
a1.style.animationName = "";
});
依次类推到第九个动画
var $main9 = $('.fudai9');
$main9.on('animationiteration', function(e) {
});
//监听动画的结束
$main9.on('animationend', function(e) {
if (a.classList.contains('start')) {
console.log("我进的第一个");
a.className = 'starts deeps';
}else{
console.log("我进的第二个");
a.className = 'start deep';
}
console.log("我是第九个执行的");
if(click1==1){
console.log("福袋1被点击");
run2();
if(click2==1){
run3();
if(click3==1){
run4();
if(click4==1){
run5();
if(click5==1){
run6();
if(click6==1){
run7();
if(click7==1){
run8();
if(click8==1){
run9();
if(click9==1){
run1();
}
}
}
}
}
}
}
}
}else{
if(click1==1){
console.log("福袋1被点击");
run2();
if(click2==1){
run3();
if(click3==1){
run4();
if(click4==1){
run5();
if(click5==1){
run6();
if(click6==1){
run7();
if(click7==1){
run8();
if(click8==1){
run9();
if(click9==1){
run1();
}
}
}
}
}
}
}
}
}
a.style.animationPlayState='paused';
a.style.animationName = 'bounce-up';
a.style.animationPlayState = 'running';
a.style.animationDuration = "1s";
}
a8.style.animationName = "";
});
ok大工告成