我有以下代码
HTML
<ul id="test">
<li class="a">1</li>
<li class="b">2</li>
<li class="c">3</li>
</ul>
<ul id="test2">
<li data-prod="a" class="animated">
<div>1</div>
<div>2</div>
<div>3</div>
</li>
<li data-prod="b" class="animated hide">
<div>1</div>
<div>2</div>
<div>3</div>
</li>
<li data-prod="c" class="animated hide">
<div>1</div>
<div>2</div>
<div>3</div>
</li>
</ul>
CSS
ul#test > li {
display: inline-block;
padding: 10px;
cursor: pointer;
}
#test {
position: fixed;
top: 0;
left: 0;
}
#test2 {
position: fixed;
top: 0;
right: 0;
list-style: none;
}
#test2 > li {
background: yellow;
width: 350px;
height: 600px;
/* transition: height 0.3s ease, width 0.2s ease;*/
overflow: hidden;
margin-top: -30px;
padding-top: 30px;
margin-right: -50px;
}
.hide {
display: none;
}
这个 jquery
$(document).ready(function () {
$('#test li').on('click', function () {
var className = $(this).attr('class');
$('#test2 > li').each(function () {
var prodName = $(this).data('prod');
if (prodName == className) {
$('#test2 > li').removeClass('bounceInRight').addClass('bounceOutRight');
$this = $(this);
setTimeout(function () {
console.log(this);
$('#test2 > li').addClass('hide')
$this.removeClass('hide bounceOutRight').addClass('bounceInRight');
}, 400);
}
});
})
});
以下是 DEMO
我的问题很少
li
ul
且ID为#test
的动作,
标识为li
的{{1}}个ul
。如何为#test2
中的每个div设置动画为
li
(animate.css)一个接一个没有
fadeInTop
完成动画后,使用类和ID(仅使用标签选择器)。 答案 0 :(得分:1)
您可以为动画添加回调。在不删除和添加类的情况下,您无法重复动画。
$(document).ready(function () {
$('#test li').on('click', function () {
var className = $(this).attr('class');
$('#test2 > li').each(function () {
var prodName = $(this).data('prod');
if (prodName == className) {
$('#test2 > li').removeClass('bounceInRight').addClass('bounceOutRight');
$this = $(this);
$this.find('div').removeClass('fadeInDown');
setTimeout(function () {
console.log(this);
$('#test2 > li').addClass('hide')
$this.removeClass('hide bounceOutRight').addClass('bounceInRight');
}, 400);
}
});
})
$('#test2 li').on('oanimationend animationend webkitAnimationEnd', function() {
$(this).find('div').each(function(i){
var $this = $(this);
setTimeout(function(){
$this.addClass('animated fadeInDown')
},i*500);
})
});
});
ul#test > li {
display: inline-block;
padding: 10px;
cursor: pointer;
}
#test {
position: fixed;
top: 0;
left: 0;
}
#test2 {
position: fixed;
top: 0;
right: 0;
list-style: none;
}
#test2 > li {
background: yellow;
width: 350px;
height: 600px;
/* transition: height 0.3s ease, width 0.2s ease;*/
overflow: hidden;
margin-top: -30px;
padding-top: 30px;
margin-right: -50px;
}
.hide {
display: none;
}
ul#test2 > li div { padding: 5px; transform: translate3d(0,-2000px,0)}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="//cdnjs.cloudflare.com/ajax/libs/animate.css/3.2.6/animate.min.css" rel="stylesheet" />
<ul id="test">
<li class="a">1</li>
<li class="b">2</li>
<li class="c">3</li>
</ul>
<ul id="test2">
<li data-prod="a" class="animated hide">
<div>1</div>
<div>2</div>
<div>3</div>
</li>
<li data-prod="b" class="animated hide">
<div>1</div>
<div>2</div>
<div>3</div>
</li>
<li data-prod="c" class="animated hide">
<div>1</div>
<div>2</div>
<div>3</div>
</li>
</ul>