gpt4 book ai didi

javascript - 如何通过 Javascript 函数更改不同 div 的背景图像?

转载 作者:行者123 更新时间:2023-11-28 02:57:01 25 4
gpt4 key购买 nike

这是我的 HTML:

<link href="styles.css" rel=stylesheet>

<title>
Gallery
</title>
</head>
<body>
<div class="master">
<div class="bg1"></div>
<div class="bg2"></div>
<div class="bg3"></div>
...

CSS:

.bg1{
height: 100%;
background-image: url("assets/pi/file-1.jpg");
background-size: contain;
margin: auto;
background-attachment: fixed;
background-repeat: no-repeat;
background-position: center;
max-height:100%;
max-width: 100%;
}
.bg2{
height: 100%;
background-image: url("assets/pi/file-2.jpg");
background-size: contain;
margin: auto;
background-attachment: fixed;
background-repeat: no-repeat;
background-position: center;
max-height:100%;
max-width: 100%;
}
...

Javascript(在/body 之前的 html 末尾):

<script type="text/javascript">
function genBg() {

var randomNum;
var imagePath = new Array(359); //array to hold the image filenames


for (int i=1; i<=359; i++){
imagePath[i]= '\"assets/pi/file-'+i+'.jpg\"';// files are named as
//'file-1, file-2..so
//this loop stores the
//names in array




randomNum = Math.floor(Math.random() * 359);

var background = imagePath[randomNum]; // takes a random filename and
//stores it in var background

document.getElementsByClassName("bg1").style.backgroundImage ='url(\"'+background+'\")'; //changes filename


}
</script>

根据此代码,div class = "bg1"的背景应该是随机的,但它不起作用,它采用 css 文件中指定的默认背景。我希望函数用随机文件名覆盖它

最佳答案

替换这个:

document.getElementsByClassName("bg1")

 document.querySelector(".bg1")

调用函数

完整代码(我还纠正了其他错误):

function genBg() {
var randomNum;
var imagePath = new Array(359); //array to hold the image filenames

for (var i = 1; i < 359; i++) {
imagePath[i] = '"assets/pi/file-' + i + '.jpg"'; // files are named as
}
//'file-1, file-2..so
//this loop stores the
//names in array
randomNum = Math.floor(Math.random() * 359);

var background = imagePath[randomNum]; // takes a random filename and
//stores it in var background
document.querySelector(".bg1").style.backgroundImage = 'url(' + background + ')'; //changes filename
//this line is for debug
document.querySelector(".bg1").innerHTML = background;
}
genBg();
.bg1 {
height: 100%;
background-image: url("assets/pi/file-1.jpg");
background-size: contain;
margin: auto;
background-attachment: fixed;
background-repeat: no-repeat;
background-position: center;
max-height: 100%;
max-width: 100%;
width:200px;
height:100px;
border:1px solid #000;
}

.bg2 {
height: 100%;
background-image: url("assets/pi/file-2.jpg");
background-size: contain;
margin: auto;
background-attachment: fixed;
background-repeat: no-repeat;
background-position: center;
max-height: 100%;
max-width: 100%;
}
<div class="master">
<div class="bg1"></div>
<div class="bg2"></div>
<div class="bg3"></div>
</div>

关于javascript - 如何通过 Javascript 函数更改不同 div 的背景图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46393114/

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