<!DOCTYPE html>
<html lang="zh-CN">
<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: url(./images2/2.jpg);
}
</style>
</head>
<body>
<script>
// 1.获取属性
let body = document.body
// 2.随机数
// let random = Math.floor(Math.random()*(max-min+1))+min
let random = Math.floor(Math.random()*(6-2+1))+2
// 3.修改body的css属性
body.style.background=`url(./images2/${random}.jpg)`
</script>
</body>
</html>
案例-随机更换背景图片
最新推荐文章于 2021-08-04 01:48:21 发布