$("#team").css("background-color","blue");我想把id:team的背景颜色转成蓝色,但是我只想把这个颜色转4秒。
我该怎么做?
我在谷歌上搜索过,但是我找不到任何关于在给定的时间框架内改变css的信息。
此外,从以前的设置中淡出/进入也是一种很好的触摸。
发布于 2012-12-31 22:15:42
如果您希望它只显示为蓝色4秒钟,您可以这样做:
var element = $( "#team" );
var oldColor = element.css( "background-color" );
element.animate( { "background-color": "blue" } )
.delay( 4000 )
.animate( { "background-color": oldColor } );您需要jQuery UI到.animate(),否则您可以只使用.css()。
发布于 2012-12-31 22:11:27
您必须使用计时器功能:
setTimeout(function() {
$('#team').css('background-color', 'whatever');
}, 4000);第二个参数是要在调用第一个参数(函数)之前等待多久的计数,以毫秒为单位。
没有内置的能力可以说“回到以前的状态”;您必须在自己的代码中记住旧的值。
发布于 2012-12-31 23:22:10
如果您不想使用jQuery UI插件(可能是由于它的大小),那么您可以手动进行动画操作。
function animblue(selector, from, to, step) {
var
target = $('#target'),
color = from,
next;
next = function () {
var hex = (Math.floor(color) < 16 ? '0' : '') + Math.floor(color).toString(16);
target.css('background-color', '#' + hex + hex + 'ff');
color += step;
if (!((color < from && color < to) || (color > from && color > to))) {
setTimeout(next, 10);
}
};
next();
}
$('#action').on('click', function () {
animblue('#target', 255, 0, -255 / 16);
window.setTimeout(function () {
animblue('#target', 0, 255, 255 / 16);
}, 4000);
});https://stackoverflow.com/questions/14106437
复制相似问题