Javascript应用(2)
一、 自动播放选项卡
1.实现思路:
- 首先实现选项卡功能
- 加上定时器,和上一个,下一个按钮,实现自动功能(鼠标移动到上面就会暂停,鼠标移开自动播放)
2.实现选项卡中的注意事项和思路
- 获取中间五个button,并建立一个空数组,放在建好的空数组中
var aButton = document.getElementsByTagName('button');
var aBtn = [];
for (var i = 1; i < aButton.length - 1; i++) {
aBtn.push(aButton[i]);
}
var oBox = document.getElementById('box');
var aDiv = oBox.getElementsByTagName('div');
- 实现切换功能的算法为:首先在点击的时候使用匿名函数清空oBox和aDiv的class,然后给点击的对应的div添加class
aBtn[i].onclick = function () {
for (var j = 0; j < aBtn.length; j++) {
aBtn[j].className = '';
aDiv[j].className = '';
}
this.className = 'active';
aDiv[this.index].className = 'show';
3、 实现选项卡的完整代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>自动播放选项卡</title>
<style>
.box {
width: 1050px;
border: 1px solid red;
margin: 0 auto;
}
.box button {
width: 120px;
height: 50px;
margin-top: 20px;
font-size: 20px;
margin-left: 15px;
}
.box div {
margin-top: 20px;
width: 1050px;
height: 500px;
font-size: 40px;
text-align: center;
background-color: pink;
display: none;
}
.box .show {
display: block;
}
.box .active {
width: 140px;
background-color: tomato;
color: white;
}
</style>
</head>
<body>
<div class="box" id="box">
<button><-</button>
<button class="active">周杰伦</button>
<button>林俊杰</button>
<button>刘德华</button>
<button>张学友</button>
<button>陈奕迅</button>
<button>-></button>
<div class="show">双节棍、千里之外、七里香</div>
<div>曹操、豆浆油条、江南</div>
<div>爱你一万年、冰雨、男人哭吧不是罪</div>
<div>吻别、 情书、饿狼传说</div>
<div>十年、爱情呼叫转移、浮夸</div>
</div>
<script>
var aButton = document.getElementsByTagName('button');
var aBtn = [];
for (var i = 1; i < aButton.length - 1; i++) {
aBtn.push(aButton[i]);
}
var oBox = document.getElementById('box');
var aDiv = oBox.getElementsByTagName('div');
for (var i = 0; i < aBtn.length; i++) {
aBtn[i].index = i;
aBtn[i].onclick = function () {
for (var j = 0; j < aBtn.length; j++) {
aBtn[j].className = '';
aDiv[j].className = '';
}
this.className = 'active';
aDiv[this.index].className = 'show';
};
}
</script>
</body>
</html>
-
实现上一个下一个按钮功能
var oPre = aButton[0];
var oNext = aButton[aButton.length - 1];
- 定义一个全局变量number记录点击按钮和div的位置
var number = 0;
function addName() {
for (var i = 0; i < aBtn.length; i++) {
aBtn[i].className = '';
aDiv[i].className = '';
}
aBtn[number].className = 'active';
aDiv[number].className = 'show';
}
oPre.onclick = function () {
number--;
if (number === -1) {
number = aBtn.length - 1;
}
addName();
};
oNext.onclick = function () {
number++;
if (number === aBtn.length) {
number = 0;
}
addName();
};
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>自动播放选项卡</title>
<style>
.box {
width: 1050px;
border: 1px solid red;
margin: 0 auto;
}
.box button {
width: 120px;
height: 50px;
margin-top: 20px;
font-size: 20px;
margin-left: 15px;
}
.box div {
margin-top: 20px;
width: 1050px;
height: 500px;
font-size: 40px;
text-align: center;
background-color: pink;
display: none;
}
.box .show {
display: block;
}
.box .active {
width: 140px;
background-color: tomato;
color: white;
}
</style>
</head>
<body>
<div class="box" id="box">
<button><-</button>
<button class="active">周杰伦</button>
<button>林俊杰</button>
<button>刘德华</button>
<button>张学友</button>
<button>陈奕迅</button>
<button>-></button>
<div class="show">双节棍、千里之外、七里香</div>
<div>曹操、豆浆油条、江南</div>
<div>爱你一万年、冰雨、男人哭吧不是罪</div>
<div>吻别、 情书、饿狼传说</div>
<div>十年、爱情呼叫转移、浮夸</div>
</div>
<script>
var aButton = document.getElementsByTagName('button');
var number = 0;
var aBtn = [];
for (var i = 1; i < aButton.length - 1; i++) {
aBtn.push(aButton[i]);
}
var oBox = document.getElementById('box');
var aDiv = oBox.getElementsByTagName('div');
for (var i = 0; i < aBtn.length; i++) {
aBtn[i].index = i;
aBtn[i].onclick = function () {
for (var j = 0; j < aBtn.length; j++) {
aBtn[j].className = '';
aDiv[j].className = '';
}
this.className = 'active';
aDiv[this.index].className = 'show';
number = this.index;
};
}
var oPre = aButton[0];
var oNext = aButton[aButton.length - 1];
oPre.onclick = function () {
number--;
if (number === -1) {
number = aBtn.length - 1;
}
addName();
};
oNext.onclick = function () {
number++;
if (number === aBtn.length) {
number = 0;
}
addName();
};
function addName() {
for (var i = 0; i < aBtn.length; i++) {
aBtn[i].className = '';
aDiv[i].className = '';
}
aBtn[number].className = 'active';
aDiv[number].className = 'show';
}
</script>
</body>
</html>
5.添加定时器,实现自动播放
var timer = null;
timer = setInterval(next, 1000);
oBox.onmouseover = function () {
clearInterval(timer);
}
oBox.onmouseout = function () {
timer = setInterval(next, 1000);
}
6.demo的最终代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>自动播放选项卡</title>
<style>
.box {
width: 1050px;
border: 1px solid red;
margin: 0 auto;
}
.box button {
width: 120px;
height: 50px;
margin-top: 20px;
font-size: 20px;
margin-left: 15px;
}
.box div {
margin-top: 20px;
width: 1050px;
height: 500px;
font-size: 40px;
text-align: center;
background-color: pink;
display: none;
}
.box .show {
display: block;
}
.box .active {
width: 140px;
background-color: tomato;
color: white;
}
</style>
</head>
<body>
<div class="box" id="box">
<button><-</button>
<button class="active">周杰伦</button>
<button>林俊杰</button>
<button>刘德华</button>
<button>张学友</button>
<button>陈奕迅</button>
<button>-></button>
<div class="show">双节棍、千里之外、七里香</div>
<div>曹操、豆浆油条、江南</div>
<div>爱你一万年、冰雨、男人哭吧不是罪</div>
<div>吻别、 情书、饿狼传说</div>
<div>十年、爱情呼叫转移、浮夸</div>
</div>
<script>
var aButton = document.getElementsByTagName('button');
var number = 0;
var aBtn = [];
for (var i = 1; i < aButton.length - 1; i++) {
aBtn.push(aButton[i]);
}
var oBox = document.getElementById('box');
var aDiv = oBox.getElementsByTagName('div');
for (var i = 0; i < aBtn.length; i++) {
aBtn[i].index = i;
aBtn[i].onclick = function () {
for (var j = 0; j < aBtn.length; j++) {
aBtn[j].className = '';
aDiv[j].className = '';
}
this.className = 'active';
aDiv[this.index].className = 'show';
number = this.index;
};
}
var oPre = aButton[0];
var oNext = aButton[aButton.length - 1];
oPre.onclick = function () {
number--;
if (number === -1) {
number = aBtn.length - 1;
}
addName();
};
oNext.onclick = next;
function next() {
number++;
if (number === aBtn.length) {
number = 0;
}
addName();
};
function addName() {
for (var i = 0; i < aBtn.length; i++) {
aBtn[i].className = '';
aDiv[i].className = '';
}
aBtn[number].className = 'active';
aDiv[number].className = 'show';
}
var timer = null;
timer = setInterval(next, 1000);
oBox.onmouseover = function () {
clearInterval(timer);
}
oBox.onmouseout = function () {
timer = setInterval(next, 1000);
}
</script>
</body>
</html>