如何简化jquery动画函数代码
how to simplify jquery animation function code
我有两个函数来制作一些动画效果,如何简写这个函数并将其更改为简单的代码?
$('.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'});
});
小提琴链接
检查我更新的FIDDLE。将数据索引属性添加到.boxdiv。因此,当悬停事件调用脚本时,将使用$(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'});
}
);
相关文章:
- 如何将函数包装在函数中以避免代码重复
- 从var向代码隐藏函数传递值
- 为函数代码编写测试
- 如何简化jquery动画函数代码
- 用于删除数组中的零的Javascript函数代码
- 使用JavaScript中的高阶函数概念,用Python编写纯函数代码
- JQuery each() 函数代码即使在集合中没有元素时也在运行
- javascript.获取javascript函数位置或获取函数代码
- 部分函数代码是't已执行
- 什么'这是jQuery下面函数代码中的错误
- 为什么新函数(代码)比直接执行相同的代码更快
- 如何使用javascript调用ascx函数代码?DotNetNuke
- JQuery函数代码运行不正常
- Node.JS:MongoDB更新回调返回结果,返回函数代码
- 用nodejs在一定时间后运行函数/代码
- Jquery只处理第一个函数/代码块
- 如何确保我的dom操作函数/代码在dom被渲染后被调用
- 在jQuery中获取现成函数代码下面的元素
- Jquery函数返回的函数代码不是值
- 什么是'$(这个)'包含在下面的javascript函数代码中