如何制作这个“闪光”?背景函数适用于具有透明背景颜色的元素吗?

发布于 2024-12-04 15:27:44 字数 1111 浏览 0 评论 0原文

我一直在玩一个简单的函数,它基本上使带有背景颜色的元素闪烁,然后淡出。它工作得很好:

$.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

So要识趣 2024-12-11 15:27:44

在动画后添加背景颜色的去除:

$.fn.animateHighlight = function(highlightColor, originalColor, duration) {
    var $this = $(this),
        highlightBg = highlightColor || "#FFFF9C",
        animateMs = duration || 1500,
        originalBg = originalColor || "#ffffff";

    this.stop().css("background-color", highlightBg).animate({backgroundColor: originalBg}, animateMs,

    function(){

        $this.css('background-color', '');

    });
};

Adding a removal of the background color after the animation:

$.fn.animateHighlight = function(highlightColor, originalColor, duration) {
    var $this = $(this),
        highlightBg = highlightColor || "#FFFF9C",
        animateMs = duration || 1500,
        originalBg = originalColor || "#ffffff";

    this.stop().css("background-color", highlightBg).animate({backgroundColor: originalBg}, animateMs,

    function(){

        $this.css('background-color', '');

    });
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文