gpt4 book ai didi

javascript - 随机背景 gif 不起作用

转载 作者:太空宇宙 更新时间:2023-11-04 09:59:11 25 4
gpt4 key购买 nike

我有这样的代码,当正文加载时,它会从文件夹中随机选择一个 gif 图像并将其用作背景。当加载新页面或同一页面时,它会选择另一个随机 gif 作为背景。我在网上看过,即使代码相对相同,也不会加载新的 gif 作为背景。这是 HTML 代码。

<html lang="en">
<head>

<!-- Random Background Image -->
<script>
function newBackground()
{
// Set up the image files to be used.
var theBackgrounds = new Array() // do not change this

// To add more image files, continue with the pattern below, adding to the array.
theBackgrounds[0] = 'images/EXTRA/Backgrounds/equalizer.gif'
theBackgrounds[1] = 'images/EXTRA/Backgrounds/equalizer2.gif'
theBackgrounds[2] = 'images/EXTRA/Backgrounds/recordSpinning.gif'

var p = theBackgrounds.length;

var whichBackground = Math.round(Math.random()*(p-1));


document.body.style.background = theBackgrounds[whichBackground];
}
</script>

</head>
<body onLoad="newBackground();">
*some code*
</body>
</html>

这是 styles.css 中的正文 CSS 代码。

body{
background-size:cover;
background-repeat: repeat-y;
background-attachment: fixed;
background-color:#000000;
color:#FFF;
font-size:13px;
font-family: Helvetica, Arial, sans-serif;
text-align:left;
}

为什么代码不起作用?

最佳答案

因为这不是您使用 CSS 设置背景图片的方式。您缺少 url() 部分。

应该是

document.body.style.backgroundImage = "url(" + theBackgrounds[whichBackground] + ")";

关于javascript - 随机背景 gif 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38465168/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com