更改预定义的身体背景图像

发布于 2024-11-16 23:10:38 字数 780 浏览 1 评论 0原文

我发现这个脚本可以在您单击链接时更改正文的背景图像。它工作得很好,除了唯一的问题是我不能在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧竹 2024-11-23 23:10:38

将背景保留在 CSS 中,并改用此代码

var backImage = [
    "images/bg0.png",
    "images/bg1.png",
    "images/bg2.png",
    "images/bg3.png",
    "images/bg4.png",
    "images/bg5.png"
];

function changeBGImage(whichImage) {
    if (document.body){
        document.body.style.backgroundImage = 'url('+backImage[whichImage]+')';
    }
}

Keep the background in the CSS, and use this code instead

var backImage = [
    "images/bg0.png",
    "images/bg1.png",
    "images/bg2.png",
    "images/bg3.png",
    "images/bg4.png",
    "images/bg5.png"
];

function changeBGImage(whichImage) {
    if (document.body){
        document.body.style.backgroundImage = 'url('+backImage[whichImage]+')';
    }
}
反话 2024-11-23 23:10:38

要更改背景(如果您在 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文