Javascript 使用jQuery.animate()而不是.css()
这是我的app.jsJavascript 使用jQuery.animate()而不是.css(),javascript,jquery,Javascript,Jquery,这是我的app.js $(document).ready(function() { var cnt=0, bg; var $body = $('body'); var arr = ['http://www.writerscentre.com.au/wp-content/uploads/2013/12/Writing-Picture-Books-grid.jpg','bg2.jpg','bg3.jpg','bg4.jpg','bg5.jpg','bg6.jpg']; var anim; var b
$(document).ready(function() {
var cnt=0, bg;
var $body = $('body');
var arr = ['http://www.writerscentre.com.au/wp-content/uploads/2013/12/Writing-Picture-Books-grid.jpg','bg2.jpg','bg3.jpg','bg4.jpg','bg5.jpg','bg6.jpg'];
var anim;
var bgrotater = setInterval(function() {
if (cnt==5) cnt=0;
bg = 'url("' + arr[cnt] + '")';
cnt++;
$body.css('background-image', bg);
/* $body.animate({
background-image = bg;
},200)
*/
}, 1000);
});
$body.css可以正常工作,但是我可以使用它吗。animate()
animate
接受一个对象,但是您定义属性就像定义变量一样
此外,由于破折号,您需要在属性键周围加引号
背景图像=bg代码>
应该是
“背景图像”:背景
不过,背景图像不会淡入淡出,这需要更多的时间,而且动画不透明度将淡入淡出元素而不是背景
有一个插件可能会帮助你做你想看到的事情:你希望背景图像动画能做什么?我想让它淡入淡出out@user,您需要找到(或编写)一个特定的插件来实现这一点。无论是jQuery还是jQuery UI都不支持这种开箱即用的特性。。。他们还有其他方法来实现这一点吗?使用$body.animate({opacity:0},'slow',function(){$(this).animate({opacity:1})})
来实现淡入淡出效果