1、鼠标进入banner后左右箭头出现。
.banner:hover #leftbutton {
visibility: visible;
}
2、页面加载初始定义一个小按钮为红色初始状态。
"button">
- "buttonindex1" style="background-color: red">
- "buttonindex2">
- "buttonindex3">
- "buttonindex4">
一、自动轮播
1、核心是计时器
setInterval(banner, 3000);
2、每次执行函数banner(),改变图片src,小按钮backgroundColor,这里就需要定义一个计数器来更改图片路径与小按钮伪数组地址,也是将图片地址与按钮绑定起来。
var index = 1 //加载banner()函数外面,不然每次执行函数index都会重置
setInterval(function banner() {
index++; //每次执行index加1,当大于4是,重置为1
if (index > 4) {
index = 1;
}
//获取元素,更换src
var img = document.querySelector(".img").querySelector("img");
var button = document.querySelectorAll("li");
img.src = "./img/back" + index + ".jpg";
for (i = 0; i < button.length; i++) { //遍历数组全部重置成白色
button[i].style.backgroundColor = "white"
}
button[index-1].style.backgroundColor = "red";
}, 3000); //3s自动轮播
二、左右箭头
1、点击箭头更换图片,改变按钮颜色。
左箭头index自减
leftbutton.onclick = function() {
index--;
if (index < 1) {
index = 4;
}
img.src = "./img/back" + index + ".jpg";
for (i = 0; i < button.length; i++) {
button[i].style.backgroundColor = "white"
}
button[index-1].style.backgroundColor = "red";
}
右箭头index自增
rightbutton.onclick = function() {
index++;
if (index > 4) {
index = 1;
}
img.src = "./img/back" + index + ".jpg";
for (i = 0; i < button.length; i++) {
button[i].style.backgroundColor = "white"
}
button[index-1].style.backgroundColor = "red";
}
三、小按钮
1、鼠标悬浮,更换成对应图片路径,按钮颜色。这里应该可以按照数组遍历来写,但是没写。
//小按钮1 这样事件有四个,复制后修改对应对象就可以
buttonindex1.onmouseover = function() {
for (i = 0; i < button.length; i++) {
button[i].style.backgroundColor = "white"
}
buttonindex1.style.backgroundColor = "red"
index = 1;
img.src = "./img/back" + index + ".jpg";
}
附录:
<!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">
<link rel="stylesheet" href="./index.css">
<script src="./index.js"></script>
<link rel="stylesheet" href="../表单/img/font-awesome-4.7.0/css/font-awesome.min.css">
<title>主页</title>
</head>
<body>
<div class="clock">
<h1></h1>
</div>
<div class="banner">
<div class="bannerimg">
<ul>
<li class="img">
<a href=""><img src="./img/back1.jpg" alt=""></a>
</li>
</ul>
</div>
<div class="button">
<ul>
<li class="buttonindex1" style="background-color: red"></li>
<li class="buttonindex2"></li>
<li class="buttonindex3"></li>
<li class="buttonindex4"></li>
</ul>
</div>
<div id="leftbutton">
<i class="fa fa-chevron-left" aria-hidden="true"></i>
</div>
<div id="rightbutton">
<i class="fa fa-chevron-right" aria-hidden="true"></i>
</div>
</div>
</body>
</html>
* {
margin: 0;
padding: 0;
}
ul li {
list-style: none;
float: left;
cursor: pointer;
margin: 0 12px;
}
a {
text-decoration: none;
}
body {
background-color: #f4f4f4;
}
.bannerimg .img img {
width: 1000px;
height: 650px;
z-index: -100;
margin-left: -12px;
}
.clock {
text-align: center;
}
.banner {
width: 1000px;
height: 650px;
margin: 0 auto;
/* border: 1px solid red; */
position: relative;
/* top: 100px;
left: 450px; */
overflow: hidden;
}
.button {
position: absolute;
right: 420px;
bottom: 40px;
z-index: 100;
width: 156px;
height: 25px;
border-radius: 20px;
background-color: rgba(255, 255, 255, .3);
}
.button ul {
margin-top: 5px;
}
.button li {
width: 14px;
height: 14px;
border-radius: 50%;
/* border: 1px solid white; */
background-color: white;
line-height: 40px;
}
#leftbutton {
position: absolute;
left: -16px;
top: 290px;
width: 50px;
height: 50px;
border-radius: 50%;
background-color: rgba(0, 0, 0, .15);
color: rgba(255, 255, 255, .8);
font-size: 26px;
cursor: pointer;
visibility: hidden;
}
#leftbutton i {
margin: 14px 18px;
}
.banner:hover #leftbutton {
visibility: visible;
}
#leftbutton:hover {
background-color: rgba(0, 0, 0, .20);
}
#rightbutton {
position: absolute;
right: -16px;
top: 290px;
width: 50px;
height: 50px;
border-radius: 50%;
background-color: rgba(0, 0, 0, .15);
color: rgba(255, 255, 255, .8);
font-size: 26px;
cursor: pointer;
visibility: hidden;
}
#rightbutton i {
margin: 14px 14px;
}
.banner:hover #rightbutton {
visibility: visible;
}
#rightbutton:hover {
background-color: rgba(0, 0, 0, .20);
}
//页面加载
window.onload = function() {
//定义index计数增加
var index = 1
var leftbutton = document.querySelector("#leftbutton");
var rightbutton = document.querySelector("#rightbutton");
var img = document.querySelector(".img").querySelector("img");
var button = document.querySelector(".button").querySelectorAll("li");
var buttonindex1 = document.querySelector(".buttonindex1");
var buttonindex2 = document.querySelector(".buttonindex2");
var buttonindex3 = document.querySelector(".buttonindex3");
var buttonindex4 = document.querySelector(".buttonindex4");
setInterval(function banner() {
index++;
if (index > 4) {
index = 1;
}
//获取元素,更换src
img.src = "./img/back" + index + ".jpg";
for (i = 0; i < button.length; i++) {
button[i].style.backgroundColor = "white"
}
button[index - 1].style.backgroundColor = "red";
}, 3000); //3s自动轮播
//左箭头
leftbutton.onclick = function() {
index--;
if (index < 1) {
index = 4;
}
img.src = "./img/back" + index + ".jpg";
for (i = 0; i < button.length; i++) {
button[i].style.backgroundColor = "white"
}
button[index - 1].style.backgroundColor = "red";
}
//右箭头
rightbutton.onclick = function() {
index++;
if (index > 4) {
index = 1;
}
img.src = "./img/back" + index + ".jpg";
for (i = 0; i < button.length; i++) {
button[i].style.backgroundColor = "white"
}
button[index - 1].style.backgroundColor = "red";
}
//小按钮1
buttonindex1.onmouseover = function() {
for (i = 0; i < button.length; i++) {
button[i].style.backgroundColor = "white"
}
buttonindex1.style.backgroundColor = "red"
index = 1;
img.src = "./img/back" + index + ".jpg";
}
//小按钮2
buttonindex2.onmouseover = function() {
for (i = 0; i < button.length; i++) {
button[i].style.backgroundColor = "white"
}
buttonindex2.style.backgroundColor = "red"
index = 2;
img.src = "./img/back" + index + ".jpg";
}
//小按钮3
buttonindex3.onmouseover = function() {
for (i = 0; i < button.length; i++) {
button[i].style.backgroundColor = "white"
}
buttonindex3.style.backgroundColor = "red"
index = 3;
img.src = "./img/back" + index + ".jpg";
}
//小按钮4
buttonindex4.onmouseover = function() {
for (i = 0; i < button.length; i++) {
button[i].style.backgroundColor = "white"
}
buttonindex4.style.backgroundColor = "red"
index = 4;
img.src = "./img/back" + index + ".jpg";
}
}