<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style>
* {
margin: 0;
padding: 0;
}
.box {
margin: 100px auto;
width: 690px;
height: 400px;
background: white;
text-align: center
}
button {
width: 60px;
height: 30px;
}
input {
width: 60px;
height: 30px;
color: green;
}
.box div {
width: 100%;
height: 400px;
background: yellow;
font-size: 30px;
display: none;
}
.box .show {
display: block;
}
.box .active {
background: orange;
width: 120px;
}
</style>
</head>
<body>
<div class='box'>
<button class="prev"><<</button>
<input type="button" value="干净" class="active" />
<input type="button" value="俊朗" />
<input type="button" value="阳光" />
<input type="button" value="忧郁" />
<button class="next">>></button>
<div class="show">
<img src="1.JPG" />
</div>
<div>
<img src="2.JPG" />
</div>
<div>
<img src="3.JPG" />
</div>
<div>
<img src="4.jpg" />
</div>
</div>
</body>
<script>
var box = document.getElementsByClassName('box')[0]
var prev = document.getElementsByClassName('prev')[0]
var next = document.getElementsByClassName('next')[0]
var aInput = document.getElementsByTagName('input')
var aDiv = box.getElementsByTagName('div')
var selectIndex = 0
var count = aInput.length
for(var i = 0; i < count; i++) {
aInput[i].index = i
aInput[i].onclick = function() {
refresh(this.index)
}
}
var timer = setInterval(nextOption, 2000)
box.onmouseover = function() {
clearInterval(timer)
timer = null
}
box.onmouseout = function() {
timer = setInterval(nextOption, 2000)
}
prev.onclick = prevOption
next.onclick = nextOption
function prevOption() {
var index = selectIndex - 1
if(index == -1) {
index = count - 1
}
refresh(index)
}
function nextOption() {
var index = selectIndex + 1
if(index == count) {
index = 0
}
refresh(index)
}
function refresh(index) {
if(index != selectIndex) {
// 刷新内容
// 清空所有显示
for(var i = 0; i < count; i++) {
aInput[i].className = ''
aDiv[i].className = ''
}
// 添加当前选中项
aInput[index].className = 'active'
aDiv[index].className = 'show'
// 记录当前位置
selectIndex = index
}
}
</script>
</html>
有空慢慢改o(╥﹏╥)o