【问题标题】:Animating Color with jQuery Step Function and CSS rgb()使用 jQuery Step Function 和 CSS rgb() 动画颜色
【发布时间】:2011-12-02 23:00:32
【问题描述】:

我正在尝试用 jQuery 做一个简单的宽度和背景颜色动画(类似于 apple.com 的搜索框)。

基本上,您聚焦一个表单域,其父级更改背景颜色并变宽,然后在模糊时,父级的宽度和背景颜色变回。

我不想为这么简单的事情加载 jQuery Color 插件,所以我找到了一个解决方法。问题是,背景颜色在焦点上完美动画,但在模糊上没有任何变化。任何帮助将不胜感激。

这是我的代码:(如果您想知道该数字来自何处,则起始背景颜色为 rgb(243, 243, 243)

$('#s').focus(function() {
    $('#header-search-form').animate(
        {'width': '175px'}, { 
        duration  : 150,
        easing    : 'swing',
        step      : function( now, fx ) {
            var completion = ( now - fx.start ) / ( fx.end - fx.start );
            $('#header-search-form').css( 'backgroundColor', 'rgb(' + 243+(255-243)*completion + ',' + 243+(255-243)*completion + ',' + 243+(255-243)*completion + ')' );
        }           
    });
});
$('#s').blur(function() {
    $('#header-search-form').animate(
        {'width': '125px'}, {
        duration  : 150,
        easing    : 'swing',
        step      : function( now, fx ) {
            var completion = ( now - fx.start ) / ( fx.end - fx.start );                    
            $('#header-search-form').css( 'backgroundColor', 'rgb(' + 255-(255-243)*completion + ',' + 255-(255-243)*completion + ',' + 255-(255-243)*completion + ')' );               
        }                   
    });
});

【问题讨论】:

  • 如果有人感兴趣,我应用我在这里学到的知识来创建一个简单的 jQuery 插件,用于动画颜色。你可以在这里看到它:gist.github.com/dominic-p/4569265

标签: jquery css


【解决方案1】:

这些数字是红色绿色和蓝色的值 rgb(red, green, blue) 这个范围在 0 到 255 之间 所以rgb(255,255,255) 是白色,rgb(0,0,0) 是黑色。

        $('#s').focus(function() {
        $('#header-search-form').animate(
    { 'width': '175px' }, {
        duration: 150,
        easing: 'swing',
        step: function(now, fx) {
            var completion = (now - fx.start) / (fx.end - fx.start);
            $('#header-search-form').css('backgroundColor', 'rgb(' + (243 + (255 - 243) * completion) + ',' + (243 + (255 - 243) * completion) + ',' + (243 + (255 - 243) * completion) + ')');
        }
    });
    });
    $('#s').blur(function() {
        $('#header-search-form').animate(
    { 'width': '125px' }, {
        duration: 150,
        easing: 'swing',
        step: function(now, fx) {
            var completion = (now - fx.start) / (fx.end - fx.start);
            $('#header-search-form').css('backgroundColor', 'rgb(' + (255 - (255 - 243) * completion) + ',' + (255 - (255 - 243) * completion) + ',' + (255 - (255 - 243) * completion) + ')');
        }
    });
    });

【讨论】:

  • 感谢您的回复。是的,我知道 rgb 颜色声明在 CSS 中是如何工作的。我的问题是模糊阶跃函数(显示的第二个)由于某种原因没有为颜色设置动画。
  • 试试这个代码焦点.css('backgroundColor', 'rgb(' + ((243 + (255 - 243) * completion)) + ',' + ((243 + (255 - 243) * completion)) + ',' + (243 + (255 - 243) * completion) + ')');和这个模糊.css('backgroundColor', 'rgb(' + (255 - (255 - 243) * parseInt(completion)) + ',' + (255 - (255 - 243) * parseInt(completion)) + ',' + (255 - (255 - 243) * parseInt(completion)) + ')');
  • 你需要括号来计算像(255 - (255 - 243) * completion)这样的数字
  • 感谢您的解释。我没有意识到需要括号。它现在按预期工作。
【解决方案2】:

你需要做的实际上是创建一个变量来保存颜色的有效数字,然后它就会起作用。

看到这个小提琴:http://jsfiddle.net/2X6QL/

它将向您显示在您的 step 函数中返回的数字,如果它们看起来像 0 到 255 之间的 rgb 数字,那么您的黄金,对我来说,这些数字看起来不会很快改变颜色,并且彩色动画对我不起作用,它会变成白色,因为数字已关闭。

我不确定如何为您准确计算数字,因为我不知道您要匹配什么颜色,但我看到您的计算中的某些数字接近于一些数字,可能是 math.round 或 parseInt会解决的,我再看看四舍五入是否可行?

它确实做到了,这是一个有效的小提琴:http://jsfiddle.net/2X6QL/3/

代码:

var completion, color;
$('#s').focus(function() {
    $('#header-search-form').animate({
        width: 175
      }, { 
        duration  : 1500,
        easing    : 'swing',
        step      : function(now, fx) {
            completion = ( now - fx.start ) / ( fx.end - fx.start );
            color = Math.round(243+(255-243)*completion) +', '+ Math.round(243+(255-243)*completion) +', '+ Math.round(243+(255-243)*completion);
            $('#header-search-form').css( 'backgroundColor', 'rgb('+color+')' );
        }           
    });
}).blur(function() {
    $('#header-search-form').animate({
        width: 125
       }, {
        duration  : 1500,
        easing    : 'swing',
        step      : function(now, fx) {
            completion = ( now - fx.start ) / ( fx.end - fx.start );                    
            color = Math.round(255-(255-243)*completion) + ', ' + Math.round(255-(255-243)*completion) + ', ' + Math.round(255-(255-243)*completion);
            $('#header-search-form').css( 'backgroundColor', 'rgb('+color+')' );
        }                   
    });
});

【讨论】:

  • 感谢您提供有用的解决方案。这看起来会很好用。我接受了另一种解决方案,因为它不需要我喜欢的其他变量。但是,我也对此表示赞成。再次感谢。
  • 谢谢,请注意,其他解决方案仍然没有将 rgb 值设置为整数,而是设置有很多小数的数字,希望您的浏览器将其转换为整数,您应该四舍五入应用它们之前的数字。
  • 好的,感谢您指出这一点。为了安全起见,我会添加一些Math.rounds。
猜你喜欢
  • 2011-03-04
  • 2012-11-18
  • 2011-05-14
  • 1970-01-01
  • 2015-06-09
  • 1970-01-01
  • 2023-02-11
  • 2017-10-28
相关资源
最近更新 更多