更改预定义的身体背景图像
我发现这个脚本可以在您单击链接时更改正文的背景图像。它工作得很好,除了唯一的问题是我不能在 CSS 中预定义背景图像,否则它不会改变。
因此,我删除了背景图像的 CSS,它可以很好地切换图像,但我希望以默认图像开始。
脚本:
<script language="JavaScript">
<!--
var backImage = new Array(); // don't change this
backImage[0] = "images/bg0.png";
backImage[1] = "images/bg1.png";
backImage[2] = "images/bg2.png";
backImage[3] = "images/bg3.png";
backImage[4] = "images/bg4.png";
backImage[5] = "images/bg5.png";
function changeBGImage(whichImage){
if (document.body){
document.body.background = backImage[whichImage];
}
}
//-->
</script>
链接:
<a href="javascript:changeBGImage(1)">Change2</a>
例如,我希望在页面加载时默认将 bg0.png 作为背景。
如果可以在背景图像之间淡入淡出,那就太好了。
提前致谢。
I found this script to change the body's background image when you click a link. It works, great except the only problem is is that I cannot have a pre-defined bg image in the css or else it won't change.
So I remove the css for the background image and it switches the images fine, but I want an image to be default to start with.
Script:
<script language="JavaScript">
<!--
var backImage = new Array(); // don't change this
backImage[0] = "images/bg0.png";
backImage[1] = "images/bg1.png";
backImage[2] = "images/bg2.png";
backImage[3] = "images/bg3.png";
backImage[4] = "images/bg4.png";
backImage[5] = "images/bg5.png";
function changeBGImage(whichImage){
if (document.body){
document.body.background = backImage[whichImage];
}
}
//-->
</script>
Link:
<a href="javascript:changeBGImage(1)">Change2</a>
And, for example, I want bg0.png to be the background by default when the page loads.
It would also be great if it was possible to fade between the bg images.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将背景保留在 CSS 中,并改用此代码
Keep the background in the CSS, and use this code instead
要更改背景(如果您在 css 中定义了背景),您需要更改: document.body.style.backgroundImage 属性
To change the background if you have one defined in the css you need to alter: document.body.style.backgroundImage attribute