动画颜色变化的优雅方式
Elegant way to animate color change
>我有 3 个选项卡,应该有不同的背景和文本颜色。我用这种样式在 CSS 中创建了 3 个类(collor-pallet-1、2、3)。我目前正在做这件丑陋的事情,它也不能顺利地工作。肯定有更好的方法吗?谢谢
$("#tab1").click(function() {
$(".resp-tab-content").addClass("color-pallet-1");
if ($(".resp-tab-content").hasClass("color-pallet-2")) {
$(".resp-tab-content").removeClass("color-pallet-2", 500);
}
if ($(".resp-tab-content").hasClass("color-pallet-3")) {
$(".resp-tab-content").removeClass("color-pallet-3", 500)
}
$(".tab-background").css("background-color", function() {
return $(".resp-tab-content").css("background-color");
console.log($(".resp-tab-content").css("background-color"));
});
});
$("#tab2").click(function() {
$(".resp-tab-content").addClass("color-pallet-2");
if ($(".resp-tab-content").hasClass("color-pallet-1")) {
$(".resp-tab-content").removeClass("color-pallet-1", 500);
}
if ($(".resp-tab-content").hasClass("color-pallet-3")) {
$(".resp-tab-content").removeClass("color-pallet-3", 500);
}
$(".tab-background").css("background-color", function() {
return $(".resp-tab-content").css("background-color");
console.log($(".resp-tab-content").css("background-color"));
});
});
$("#tab3").click(function() {
$(".resp-tab-content").addClass("color-pallet-3");
if ($(".resp-tab-content").hasClass("color-pallet-2")) {
$(".resp-tab-content").removeClass("color-pallet-2", 500);
}
if ($(".resp-tab-content").hasClass("color-pallet-1")) {
$(".resp-tab-content").removeClass("color-pallet-1", 500);
}
$(".tab-background").css("background-color", function() {
return $(".resp-tab-content").css("background-color");
console.log($(".resp-tab-content").css("background-color"));
});
});
看起来您正在尝试为.removeClass()
提供一个时间参数,但是当您考虑它时,元素要么有类,要么没有类,没有转换。幸运的是,使用 CSS3,您不需要 JavaScript 来制作颜色动画!若要完成平滑过渡,请为基类(类似于 .tab
)提供类似 transition: background-color 0.5s ease;
的规则(带有适当的浏览器前缀)和起始颜色。为调色板类指定要过渡到的颜色。然后,您可以使用一些简单的javascript来切换调色板类名。 CSS Tricks(像往常一样)有一篇关于CSS3转换的有用文章:http://css-tricks.com/almanac/properties/t/transition/。
试试这个插件
https://github.com/jquery/jquery-color
彩色动画示例代码
jQuery("#go").click(function(){
jQuery("#block").animate({
backgroundColor: "#abcdef"
}, 1500 );
});
相关文章:
- 使用JS如何动态更改显示的html文件中的文本背景颜色
- 为什么这在IE中的工作方式与在Firefox中不同
- 在AngularJS应用程序中使用封装指令和路由的推荐方式是什么
- Windows8应用程序(html&Javascript):从图片库(除了文件选择器)显示图像的另一种方式
- 高亮显示时编辑文本大小和颜色
- 用与线条相同的颜色填充多折线图上的点
- 更改使用Chart.js创建的图表中的轴线颜色
- 在localhost Dev Box上测试JSONP请求的最佳方式
- 使用javascript存储变量的最安全方式
- 如何使用jquery更改html中的背景颜色
- 引导 NAV 方式在移动设备中折叠或展开时设置颜色样式
- 动画颜色变化的优雅方式
- HTML5画布-在屏幕上显示像素颜色阵列的最快方式
- 如何以编程方式计算两种颜色之间的对比度
- JavaScript-如何以编程方式计算颜色
- 以平滑的方式设置背景颜色
- 在extendscript中创建颜色选择器gui的最佳方式
- 当以手风琴方式打开外部菜单项时,无法向第一个子菜单项添加新的颜色类
- 以编程方式更改引导程序popover颜色
- 在Famo.us中转换颜色(或非转换/不透明度CSS属性)的正确方式