<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
body {
background-image: url(./images/desktop_1.jpg);
}
</style>
</head>
<body>
<script>
// 1. 随机获取图片序号
function getRandom(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
let random = getRandom(1, 10);
// 2. 更换背景图片
document.body.style.backgroundImage = `url(./images/desktop_${random}.jpg)`;
</script>
</body>
</html>
JavaScript随机背景案例
最新推荐文章于 2023-11-15 17:05:48 发布