【发布时间】: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