html結(jié)構(gòu)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="./css/lunbo.css">
<title>輪播圖</title>
</head>
<body>
<div class="main" id="main">
<!-- 圖片輪播 -->
<div class="banner" id="banner">
<a href="#">
<div class="banner-slide slider1 slider-active"></div>
</a>
<a href="#">
<div class="banner-slide slider2"></div>
</a>
<a href="#">
<div class="banner-slide slider3"></div>
</a>
</div>
<!-- 上一張 下一張按鈕 -->
<a href="javascript:void(0)" class="button prev" id="prev"></a>
<a href="javascript:void(0)" class="button next" id="next"></a>
<!-- 圓點導(dǎo)航 -->
<div class="dots" id="dots">
<span class="active"></span>
<span></span>
<span></span>
</div>
</div>
<script src="./js/luobo.js" type="text/javascript"></script>
</body>
</html>
css樣式
*{
margin: 0;
padding: 0;
}
ul{
list-style: none;
}
body{
font-family: "微軟雅黑";
color: #14191e
}
.main{
width: 1200px;
height: 400px;
margin: 30px auto;
overflow: hidden;
position: relative;
}
.banner{
width: 1200px;
height: 460px;
position: relative;
}
.banner-slide {
width: 1200px;
height: 460px;
position: absolute;
background-repeat: no-repeat;
display: none;
}
/* 顯示當(dāng)前圖片 */
.slider-active{
display: block;
}
.slider1{
background-image: url('../img/banner1.jpg');
}
.slider2{
background-image: url('../img/banner2.jpg');
}
.slider3{
background-image: url('../img/banner3.jpg');
}
.button{
position: absolute;
width: 40px;
height: 80px;
left: 0px;
top: 50%;
margin-top: -40px;
background: url(../img/nexImg.png) no-repeat center center;
}
.button:hover{
background-color: #333;
opacity: 0.8;
filter: alpha(opacity=80);
}
.next{
left: auto;
right: 0;
}
.prev{
transform: rotate(180deg);
}
.dots{
position: absolute;
right: 20px;
bottom: 24px;
text-align: center;
}
.dots span{
display: inline-block;
width: 12px;
height: 12px;
border-radius: 50%;
background: rgba(17, 17, 27, 0.5);
margin-left: 8px;
line-height: 12px;
cursor: pointer;
box-shadow: 0 0 0 2px rgba(255, 255, 255, 0.8) inset; /* 圓點描邊*/
}
.dots span.active{
box-shadow: 0 0 0 2px rgba(17, 17, 27, 0.5);
background: #fff;
}
js代碼實現(xiàn)輪播
//封裝一個代替getElementById()的方法
function byId(id) {
return typeof (id) === "string" ? document.getElementById(id) : id;
}
//鼠標(biāo)輪播
var index = 0;
var timer = null;//定時器
var pics = byId("banner").getElementsByTagName("div"); //banner下面的div
var len = pics.length;//有多少個圖片
var dots = byId("dots").getElementsByTagName("span"); //小圓點控制
var prev = byId("prev"); //上一張
var next = byId("next");//下一張
function slideImg() {
var main = byId("main");
//滑過清除定時器 離開繼續(xù)執(zhí)行
main.onmouseover = function () {
//滑過清除定時器
if (timer) clearInterval(timer); //鼠標(biāo)移入清除定時器
}
main.onmouseout = function () {
timer = setInterval(function () {
index++;
if (index >= len) {
index = 0;
}
changeImg();//點擊切換圖片
}, 3000);
}
main.onmouseout(); //這里是方法執(zhí)行 自動在main上觸發(fā)事件
//點擊圓點切換圖片 遍歷所有點擊 且綁定事件
for (var d = 0; d < len; d++) {
//給所有span添加一個id的屬性 值為d 作為當(dāng)前的span的索引
dots[d].id = d;
dots[d].onclick = function () {
//改變index為當(dāng)前span的id值
index = this.id;
//調(diào)用changeImg實現(xiàn)切換圖片
changeImg();
}
}
//下一張
next.onclick = function () {
index++;
if (index > len) index = 0;
changeImg();
};
//上一張
prev.onclick = function () {
index--;
if (index < 0) {
index = len - 1;
}
changeImg();
};
}
function changeImg() {
for (var i = 0; i < len; i++) {
pics[i].style.display = "none";
dots[i].className = "";
}
pics[index].style.display = "block";
dots[index].className = "active";
}
slideImg();
實現(xiàn)效果如下:
image.png
image.png