添加/删除类和动画元素的不同方法
A different approach to add/remove classes and animate elements
我有 5 个列表项,单击其中一个时,我知道的唯一方法是对所有元素进行动画处理,然后将所选元素恢复到新宽度。谁能告诉我如何减少这个过程,以便只有当前活动项目被动画化并删除类,然后新项目被动画化并添加活动类?
.JS
var test = $('#test'),
test_li = test.children(),
small = 60,
large = 200;
test_li.on('click', function(e) {
if( !$(this).hasClass('active') ){
test_li.animate({ width: small }, 300).removeClass('active'); //animates every single li every time
$(this).animate({ width: large }, 300).addClass('active');
}
});
在这里小提琴:http://jsfiddle.net/TSNtu/1/
var test = $('#test'),
test_li = test.children(),
small = 60,
large = 200;
test_li.on('click', function(e) {
// if clicked element is active itself then do nothing
if ($(this).hasClass('active')) return;
// remove active from others
$('.active', test).animate({
width: small
}, 300).removeClass('active');
// animate the clicked one
$(this).animate({
width: large
}, 300).addClass('active');
});
演示
使用单链
var test = $('#test'),
test_li = test.children(),
small = 60,
large = 200;
test_li.on('click', function(e) {
// if clicked element is active itself then do nothing
if ($(this).hasClass('active')) return;
$(this) // clicked li
.animate({
width: large
}, 300)
.addClass('active')
.siblings('.active') // other li
.animate({
width: small
}, 300)
.removeClass('active');
});
演示
相关文章:
- 用于检查数组中是否存在元素的javascript自定义方法
- 从数组中删除元素的最佳方法是:javascript/jquery
- 有没有一种方法可以让内联事件处理程序在元素创建后立即执行
- 使用filter和map方法将数组中某些元素的第一个字母大写-JavaScript
- 任何一种简单的方法都可以将带有onload的元素作为目标
- 如何向Selectize元素添加渲染和创建方法
- 他们是如何使用angular/jqLite find()方法按属性名称和值选择元素的?ng conf 2015
- 在 JavaScript 数组中的所有元素之间穿插元素的简洁方法
- 列出没有 mysql 的元素的最佳方法是什么
- 使用Underscore.js修改json数组中所选元素的更有效方法
- 元素.appendChild()插入IE.变通方法?(与innerText与textContent相关)
- 选择具有值数组的所有元素的最有效方法
- 有什么方法可以在下一个元素上进行追加吗
- 使用javascript查找元素位置的最快方法
- 特定循环(html元素)方法的优点和缺点
- Javascript集:任何覆盖元素之间比较的方法
- 使用Ajax Get方法将数据显示到特定元素中
- 使用Javascript,获取元素的方法是什么,基于打开和关闭标记之间的文本
- javascript在html元素方法运行setTimeout后返回此消息
- 隐藏具有特定类$.each、for等的元素的有效方法