我试图使用jQuery在鼠标悬停时通过动画改变背景颜色。
我查看了一些示例,似乎做得很对,它可以用于其他属性(如fontSize),但是当涉及到backgroundColor时,我会收到一个"无效属性"的js错误。 我正在处理的元素是一个div。
$(".usercontent").mouseover(function() {
$(this).animate({ backgroundColor: "olive" }, "slow");
});
有什么想法吗?
我试图使用jQuery在鼠标悬停时通过动画改变背景颜色。
我查看了一些示例,似乎做得很对,它可以用于其他属性(如fontSize),但是当涉及到backgroundColor时,我会收到一个"无效属性"的js错误。 我正在处理的元素是一个div。
$(".usercontent").mouseover(function() {
$(this).animate({ backgroundColor: "olive" }, "slow");
});
有什么想法吗?
我喜欢使用delay()来完成这个任务,以下是一个例子:
jQuery(element).animate({ backgroundColor: "#FCFCD8" },1).delay(1000).animate({ backgroundColor: "#EFEAEA" }, 1500);
这可以通过函数调用,"element" 代表元素的类名/名称等。该元素将立即出现带有 #FCFCD8 背景,保持一秒钟,然后淡入 #EFEAEA。
试试这个:
(function($) {
var i = 0;
var someBackground = $(".someBackground");
var someColors = [ "yellow", "red", "blue", "pink" ];
someBackground.css('backgroundColor', someColors[0]);
window.setInterval(function() {
i = i == someColors.length ? 0 : i;
someBackground.animate({backgroundColor: someColors[i]}, 3000);
i++;
}, 30);
})(jQuery);
element
.css('color','#FF0000')
;
$('<div />')
.css('width',0)
.animate(
{'width':100},
{
duration: 3000,
step:function(now){
var v = (255 - 255/100 * now).toString(16);
v = (v.length < 2 ? '0' : '') + v.substr(0,2);
element.css('color','#'+v+'0000');
}
}
)
;
width
,可以表示百分比(0 到 100)。然后,使用 step 函数,将此数字动画转换为文本颜色,并进行简单的十六进制计算。.stop()
- 并且可以使用缓动和持续时间。使用动画效果更改背景颜色,无需使用jQueryUI:
selector.css({
backgroundColor: "#555",
transition: "background-color 1.8s"
});
ColorBlend插件正好可以满足您的需求
http://plugins.jquery.com/project/colorBlend
这是我的高亮代码
$("#container").colorBlend([{ colorList:["white", "yellow"],
param:"background-color",
cycles: 1,
duration: 500
}]);
jQuery(".usercontent").mouseover(function() {
jQuery(".usercontent").animate({backgroundColor:'red'}, 'fast', 'linear', function() {
jQuery(this).animate({
backgroundColor: 'white'
}, 'normal', 'linear', function() {
jQuery(this).css({'background':'none', backgroundColor : ''});
});
});
试试这个:
jQuery(".usercontent").hover(function() {
jQuery(this).animate({backgroundColor:"pink"}, "slow");
},function(){
jQuery(this).animate({backgroundColor:"white"}, "slow");
});
带有效果的修订方式:
jQuery(".usercontent").hover(function() {
jQuery(this).fadeout("slow",function(){
jQuery(this).animate({"color","yellow"}, "slow");
});
});
试着使用它
-moz-transition: background .2s linear;
-webkit-transition: background .2s linear;
-o-transition: background .2s linear;
transition: background .2s linear;
rgba(0, 0, 0, 0)
而不是预期的空/ null值。为了“修复”这个问题,元素必须具有初始背景颜色。 - Shadow The Spring Wizard