<!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>百度换肤</title> | |
<style> | |
* { | |
margin: 0; | |
padding: 0; | |
list-style: none; | |
} | |
body { | |
box-sizing: border-box; | |
padding-top: 80px; | |
width: 100vw; | |
min-height: 100vh; | |
background-color: gold; | |
background-repeat: no-repeat; | |
background-size: auto 100vh; | |
background-position: center center; | |
/* 背景图不滚动 */ | |
background-attachment: fixed; | |
} | |
content { | |
width: 50px; | |
height: 2000px; | |
margin: 0 auto; | |
background-color: pink; | |
} | |
nav { | |
position: fixed; | |
top: 0; | |
width: 100vw; | |
height: 60px; | |
background-color: rgba(0, 0, 0, .35); | |
color: white; | |
} | |
nav p { | |
float: right; | |
margin-right: 50px; | |
height: 60px; | |
line-height: 60px; | |
font-size: 14px; | |
} | |
nav p:hover { | |
cursor: pointer; | |
} | |
.alert { | |
display: none; | |
align-items: center; | |
justify-content: center; | |
position: fixed; | |
top: 0; | |
z-index: 1; | |
width: 100vw; | |
height: 100vh; | |
background-color: rgba(0, 0, 0, .1); | |
} | |
.card { | |
padding: 0 20px; | |
display: flex; | |
justify-content: space-around; | |
align-items: center; | |
position: relative; | |
width: 920px; | |
height: 320px; | |
background-color: #fff; | |
border-radius: 8px; | |
overflow: hidden; | |
box-shadow: 0 0 5px lightgray; | |
} | |
.card img:hover { | |
cursor: pointer; | |
transform: scale(1.2); | |
box-shadow: 0 0 3px #999; | |
} | |
.card img { | |
width: 180px; | |
height: 200px; | |
border-radius: 4px; | |
box-shadow: 0 0 3px #ccc; | |
transition: .3s; | |
} | |
.card .close { | |
position: absolute; | |
right: 30px; | |
top: 30px; | |
font-size: 20px; | |
color: lightgray; | |
} | |
.close:hover, | |
.no:hover { | |
cursor: pointer; | |
color: cornflowerblue; | |
} | |
.no { | |
position: absolute; | |
bottom: 0; | |
width: 100%; | |
height: 40px; | |
line-height: 40px; | |
font-size: 14px; | |
text-align: center; | |
color: lightgray; | |
} | |
.checked { | |
outline: 4px solid tomato; | |
} | |
</style> | |
</head> | |
<body> | |
<nav> | |
<p>更换皮肤</p> | |
</nav> | |
<div class="alert"> | |
<div class="card"> | |
<span class="close">X</span> | |
<span class="no">不使用皮肤</span> | |
<img src="./img/xiu1.jpg" alt=""> | |
<img src="./img/xiu5.jpg" alt=""> | |
<img src="./img/xiu7.jpg" alt=""> | |
<img src="./img/xiu9.jpg" alt=""> | |
<img src="./img/xiu4.jpg" alt=""> | |
</div> | |
</div> | |
<div class="content"></div> | |
</body> | |
<script> | |
var dAlert = document.querySelector('.alert'); | |
var p = document.querySelector('nav p'); | |
var spanClose = document.querySelector('.close'); | |
var imgs = document.querySelectorAll('img'); | |
var body = document.querySelector('body'); | |
var pNo = document.querySelector('.no'); | |
// 当前显示图片的下标,如果为-1,则代表没有被选中; | |
var index = -1; | |
p.onclick = function () { | |
dAlert.style.display = 'flex'; | |
} | |
spanClose.onclick = function () { | |
dAlert.style.display = 'none'; | |
} | |
for (i = 0; i < imgs.length; i++) { | |
imgs[i].xiabiao = i; | |
imgs[i].onclick = function () { | |
if (index === this.xiabiao) { | |
return; | |
} | |
// this 指向函数的拥有者 | |
body.style.backgroundImage = `url(${this.src})`; | |
// 1.暴力解除 | |
// for (var j = 0; j < imgs.length; j++) { | |
// imgs[j].className = ""; | |
// } | |
// 2.下标 | |
if (index != -1) { | |
imgs[index].className = ''; | |
} | |
this.className = 'checked'; | |
index = this.xiabiao; | |
} | |
} | |
pNo.onclick = function () { | |
body.style.backgroundImage = ''; | |
imgs[index].className = ''; | |
index = -1; | |
} | |
</script> | |
</html> |