如何制作这个“闪光”?背景函数适用于具有透明背景颜色的元素吗?
我一直在玩一个简单的函数,它基本上使带有背景颜色的元素闪烁,然后淡出。它工作得很好:
$.fn.animateHighlight = function(highlightColor, duration) {
var highlightBg = highlightColor || "#FFFF9C";
var animateMs = duration || 1500;
var originalBg = this.css("backgroundColor");
this.stop().css("background-color", highlightBg).animate({backgroundColor: originalBg}, animateMs);
};
然后调用 via:
$('input.flasher').animateHighlight('#e9fff5',1000);
然而问题是当我尝试在没有 css 背景颜色的元素上使用它时 - 即它的 background-color: 透明
如何将背景颜色淡化为透明?或者至少创造一种幻觉,那就是所发生的事情?
我更改了函数以包含附加属性“originalColor”,
$.fn.animateHighlight = function(highlightColor, originalColor, duration) {
var highlightBg = highlightColor || "#FFFF9C";
var animateMs = duration || 1500;
var originalBg = originalColor || "#ffffff";
this.stop().css("background-color", highlightBg).animate({backgroundColor: originalBg}, animateMs);
};
这可以正常工作,但是当最终结果需要是具有透明背景色的输入时,我的字段将保留以其样式编写的originalColor。
我猜我可以添加一些额外的功能,在动画结束时删除背景颜色,但想知道如何实现这一点,或者更好的方法。
I have been playing with a simple function which basically flashes an element with a background color, then fades out. It works quite well:
$.fn.animateHighlight = function(highlightColor, duration) {
var highlightBg = highlightColor || "#FFFF9C";
var animateMs = duration || 1500;
var originalBg = this.css("backgroundColor");
this.stop().css("background-color", highlightBg).animate({backgroundColor: originalBg}, animateMs);
};
It is then called via:
$('input.flasher').animateHighlight('#e9fff5',1000);
The problem is however is when I've tried to use this on an element that doesn't have a css background-color - ie its background-color: transparent
How does one go about fading a backgroundColor to transparent? Or at least creating the illusion that is what happens?
I changed the function to include an additional attribute 'originalColor'
$.fn.animateHighlight = function(highlightColor, originalColor, duration) {
var highlightBg = highlightColor || "#FFFF9C";
var animateMs = duration || 1500;
var originalBg = originalColor || "#ffffff";
this.stop().css("background-color", highlightBg).animate({backgroundColor: originalBg}, animateMs);
};
This works ok, but then my field is left with the originalColor written in its style, when the end result needs to be an input that has a transparent background color.
I'm guessing I could add some extra functionality which removes backgroundColor at the end of the animation, but wondering how you would go about achieving this, or a better approach.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在动画后添加背景颜色的去除:
Adding a removal of the background color after the animation: