我有两个几乎相同的动画......它们之间的区别是“左右”定位。我想为 .forward 和 .backward 重用第一个代码块......我猜这可以通过使用 HTML 5 数据属性或变量来完成,但我不知道如何去吧。
.hover-area { position:relative; width:100%; height:50px; }
.backward, .forward { position:absolute; }
.backward{ left:0px; }
.forward { right:0px; }
<div class="hover-area">
Hover Area
<div class="backward" data-animate-on='{"left":"20"}' data-animate-off='{"left":"0"}'>Previous</div>
<div class="forward" data-animate-on='{"right":"20"}' data-animate-off='{"right":"0"}'>Next</div>
</div>
$('.forward').css({opacity:0, right:0});
$('.hover-area').hover(function() {
$(this).find('.forward').stop()
.animate({right:20}, {queue:false, duration:300, easing:'easeOutCubic'})
.animate({opacity:'0.95'}, {queue:false, duration:400, easing:'easeOutCubic'});
},function() {
$(this).find('.forward').stop()
.animate({right:0}, {queue:false, duration:550, easing:'easeOutSine'})
.animate({opacity:'0'}, {queue:false, duration:300, easing:'easeOutSine'});
});
$('.backward').css({opacity:0, left:0});
$('.hover-area').hover(function() {
$(this).find('.backward').stop()
.animate({left:20}, {queue:false, duration:300, easing:'easeOutCubic'})
.animate({opacity:'0.95'}, {queue:false, duration:400, easing:'easeOutCubic'});
},function() {
$(this).find('.backward').stop()
.animate({left:0}, {queue:false, duration:550, easing:'easeOutSine'})
.animate({opacity:'0'}, {queue:false, duration:300, easing:'easeOutSine'});
});