<!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>Change Skin</title>
</head>
<body>
<style>
body {
background: url(./img/A\ \(1\).jpg) no-repeat center top;
background-size: 100% 100%;
}
li {
list-style: none;
float: left;
flex: 1;
}
ul {
flex: 1;
width: 1200px;
display: flex;
margin: 0 auto;
}
ul::after::before {
content: '';
clear: both;
display: block;
height: 0;
visibility: hidden;
}
img {
box-sizing: border-box;
display: flexbox;
width: 150px;
}
.container::before,
.container::after {
content: '';
height: 0;
display: block;
clear: both;
visibility: hidden;
}
.container {
display: flex;
margin: auto;
width: 80%;
border: 3px solid green;
padding: 10px;
margin-bottom: 20px;
}
img:hover {
border: 3px solid rgba(59, 69, 207, 0.842);
}
</style>
<center>
<h1>利用按钮更改背景的简单案例</h1>
<div class="container">
<ul>
<li><img src="./img/01.jpg" alt="" srcset="" /></li>
<li><img src="./img/02.jpg" alt="" srcset="" /></li>
<li><img src="./img/03.png" alt="" srcset="" /></li>
<li><img src="./img/04.jpg" alt="" srcset="" /></li>
<li><img src="./img/05.jpg" alt="" srcset="" /></li>
</ul>
</div>
</center>
<script>
var imgs = document.querySelector('.container').querySelectorAll('img');
for (var i = 0; i < imgs.length; i++) {
imgs[i].onclick = function () {
console.log(this.src);
document.body.style.backgroundImage = 'url(' + this.src + ')';
}
}
</script>
</body>
</html>
|