我有两个功能来做一些动画效果,如何缩短这个函数,并将其转换为简单的代码?
$('.box-1').hover(function() {
$('.box-1').stop().animate({bottom:'0'});
$('.dul-1').stop().animate({top:'0px'});
},
function() {
$('.box-1').stop().animate({bottom:'-100px'});
$('.dul-1').stop().animate({top:'-100px'});
});
$('.box-2').hover(function() {
$('.box-2').stop().animate({bottom:'0'});
$('.dul-2').stop().animate({top:'0px'});
},
function() {
$('.box-2').stop().animate({bottom:'-100px'});
$('.dul-2').stop().animate({top:'-100px'});
});琴杆
发布于 2015-03-18 07:08:43
检查我更新的小提琴 .add数据索引属性到.box div。因此,当悬停事件调用脚本将使用$(This).attr(‘data- index’)获得索引时。
HTML:
<div class="wrapper">
<div class="dul-1"></div>
<div class="dul-2"></div>
<div class="box box-1" data-index="1"></div>
<div class="box box-2" data-index="2"></div>
</div>Javascript:
$('.box').hover(function() {
index = $(this).attr('data-index');
$('.box-'+index).stop().animate({bottom:'0'});
$('.dul-'+index).stop().animate({top:'0px'});
},
function() {
$('.box-'+index).stop().animate({bottom:'-100px'});
$('.dul-'+index).stop().animate({top:'-100px'});
}
);https://stackoverflow.com/questions/29115891
复制相似问题