想让改变背景像褪色一样顺利
我花了很多天试图弄清楚如何为背景图像过渡设置动画,但我找不到解决方案。我是网络开发新手,所以请不要苛刻;)
这是我从其他人那里得到的一些示例代码:
var images = ['bg1.jpg', 'bg2.jpg', 'bg3.jpg'];
var curImage = 0;
function switchImage()
{
curImage = (curImage + 1) % images.length
document.body.style.backgroundImage = 'url(images/' + images[curImage] + ')'
}
window.setInterval(switchImage, 5000);
此代码立即更改背景图像,但我希望新的背景图像淡入。
这是该页面的链接我正在创建:
http://preferredmerchantservices.net/
我希望有人可以帮助我,并提前致谢。
I've spent many days trying to figure out how to animate background image transitions, but I can't find a solution. I am new to web development so please dont be harsh ;)
Here is some example code I got from someone else:
var images = ['bg1.jpg', 'bg2.jpg', 'bg3.jpg'];
var curImage = 0;
function switchImage()
{
curImage = (curImage + 1) % images.length
document.body.style.backgroundImage = 'url(images/' + images[curImage] + ')'
}
window.setInterval(switchImage, 5000);
This code changes the background image instantly, but I want the new background image to fade in.
Here is a link to the page I am creating:
http://preferredmerchantservices.net/
I hope someone can help me, and thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看看这个问题:
背景图像淡入淡出
这是一个解释的链接如何使用 jQuery 进行淡入淡出过渡:
http://jqueryfordesigners.com/image-loading/
如果您需要 jQuery 库,可以将此代码粘贴到您的页面中:
Take a look at this question:
Fading in a background image
Here is a link that explains how to do a fade transition with jQuery:
http://jqueryfordesigners.com/image-loading/
If you need the jQuery library, you can paste this code into your page:
如果您不想浪费太多时间来弄清楚自己,您应该使用 jQuery 等库。这样就只剩下
fadeIn()
和fadeOut()
的问题了。由于您是 Web 开发的新手,您必须知道 jQuery 是一个非常流行的库,现在几乎每个人都在使用。它可以让您编写更少的代码并执行更多操作。
但最重要的是,它确保了跨浏览器兼容性。
If you don't want to loose too much time trying to figure out yourself, you should use a library such as jQuery. With that it's just a matter of
fadeIn()
andfadeOut()
.Since you are a novice in web development, you have to know jQuery is a really popular library almost everyone is using nowadays. It lets you write less code and do more.
But most importantly, it ensures cross browser compatibility.