包含兩個(gè)輪播圖版本
1.0 2.0
1.0特點(diǎn):
????1.簡(jiǎn)單,易理解
????2.可維護(hù)性差(添加圖片后,要手動(dòng)添加按鈕span的數(shù)量,還要修改越界的代碼
????3.可以在瀏覽界面進(jìn)行切換速度快慢設(shè)置
????4.0可以選擇按鈕進(jìn)行圖片切換

autoplay1.0.png
2.0特點(diǎn)
????1.在1.0的基礎(chǔ)上進(jìn)行了部分代碼調(diào)整
????2.可維護(hù)性提高,用戶只需要添加圖片,系統(tǒng)會(huì)自動(dòng)生成按鈕代碼,修改越界代
????3.去除在界面瀏覽界面的切換速度設(shè)置
????4.添加了圖片切換的動(dòng)畫效果
系統(tǒng)的可維護(hù)性是衡量一個(gè)系統(tǒng)的可修復(fù)(恢復(fù))性和可改進(jìn)性的難易程度。

autoplay2.0.png
文檔組織結(jié)構(gòu)
├─css
├─index.css
├─img
├─js
├─autoplay.js
├─index.html
輪播圖1.0
index.html
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<link rel="stylesheet" href="css/index.css">
</head>
<body>
<div class="container">
<div class="banner" style="left: -600px"> <!--圖片-->
<img src="./img/5.jpg" alt="">
<img src="./img/1.jpg" alt="">
<img src="./img/2.jpg" alt="">
<img src="./img/3.jpg" alt="">
<img src="./img/4.jpg" alt="">
<img src="./img/5.jpg" alt="">
<img src="./img/1.jpg" alt="">
</div>
<div class="Buttons"> <!-- 按鈕-->
<span class="on">1</span>
<span>2</span>
<span>3</span>
<span>4</span>
<span>5</span>
</div>
<a href="javascript:;" class="l"><</a> <!--左按鈕-->
<a href="javascript:;" class="r">></a> <!--右按鈕-->
</div>
<div class="control">
<p>速度選擇:
<input type="text" maxlength="6" id="num">
<input type="button" value="確定" onclick="speed()" class="mark">
</p>
</div>
</body>
<script type="text/javascript" src="js/autoplay.js"></script>
</html>
index.css
* { /*通用屬性*/
padding: 0px;
margin: 0 auto;
text-align: center;
font-family: 宋體;
}
a {
text-decoration: none; /*消除a標(biāo)簽的默認(rèn)屬性*/
}
.container { /*相對(duì)布局,寬高為一張圖片的大小*/
margin-top: 150px;
position: relative;
height: 300px;
width: 600px;
border: 2px solid blue;
overflow: hidden /*只顯示一張圖片*/
}
.container .banner { /*相對(duì)于.container絕對(duì)定位*/
position: absolute;
height: 300px;
width: 4200px;
z-index: 1; /*設(shè)置為1好讓.Buttons顯示在圖片上*/
}
.container .banner img {
float: left; /*圖片左浮動(dòng),使其變?yōu)樾蓄惪祛愋?/
height: 300px;
width: 600px;
}
.container .Buttons {
position: absolute;
bottom: 10px;
left: 400px;
width: 200px;
height: 40px;
z-index: 2;
cursor: pointer; /*更改鼠標(biāo)樣式*/
}
.container .Buttons span {
display: inline-block;
height: 25px;
width: 25px;
font-size: 20px;
line-height: 25px;
border-radius: 50%;
background: darkred;
}
.container .l {
position: absolute;
display: block;
width: 40px;
height: 60px;
background: yellow;
line-height: 60px; /*將整個(gè)高度作為一行的高度*/
font-size: 30px; /*更改字體大小*/
left: 0px;
top: 120px;
}
.container .r {
position: absolute;
display: block;
width: 40px;
height: 60px;
background: yellow;
line-height: 60px;
font-size: 30px;
right: 0px;
top: 120px;
}
.container:hover .l{
z-index: 2;
}
.container:hover .r{
z-index: 2;
}
.container .Buttons span.on {
background: green;
}
.control {
margin-top: 30px;
height: 300px;
width: 600px;
}
.control p {
display: inline-block;
float: left;
line-height: 50px;
text-align: left;
}
#num:hover {
border-color: #cccccc;
}
.mark {
border-color: #cccccc;
}
autoplay.js
var index = 0;
var container = document.querySelector(".container");
var banner = document.querySelector(".banner");
var prev = document.querySelector(".l");
var next = document.querySelector(".r");
//速度調(diào)整
function speed() {
var num = document.getElementById('num');
settimes = num.value;
if (settimes <= 0)
{
alert("輸入不合法!請(qǐng)保證數(shù)入的數(shù)值為正值!");
settimes = 1000;
}
else
{
clearInterval(times);
autoPlay();
}
}
//上一張
prev.onclick = function () {
prev_pic();
}
//下一張
next.onclick = function () {
next_pic();
}
//上一張的函數(shù)
var prev_pic = function () {
index --;
if (index == -1)
index = 4;
var imgLeft = parseInt(banner.style.left) + 600;
console.log(imgLeft);
if (imgLeft == 0)
imgLeft = -3000;
banner.style.left = imgLeft + "px";
BTtons();
}
//下一張的函數(shù)
var next_pic = function () {
index ++;
if (index == 5)
index = 0;
var imgLeft = parseInt(banner.style.left) - 600;
if (imgLeft == -3600)
imgLeft = -600;
banner.style.left = imgLeft + "px";
BTtons();
}
var times = null; //自動(dòng)切換事件
var settimes = 1000; //自動(dòng)切換的時(shí)間間隔
var autoPlay = function () {
times = setInterval(function () {
next_pic();
},settimes);
}
autoPlay();
//鼠標(biāo)移入
container.onmouseenter=function () {
clearInterval(times);
}
//鼠標(biāo)移出
container.onmouseleave = function () {
autoPlay();
}
//按鈕隨圖片切換
var Span = document.getElementsByTagName("span");
function BTtons() {
for (let i = 0;i < Span.length;i ++)
{
Span[i].className = "";
}
Span[index].className = "on";
}
//對(duì)按鈕進(jìn)行監(jiān)聽
for (let i = 0;i < Span.length;i ++)
{
Span[i].onclick = function () {
index = i;
var imgLeft = index * -600 - 600;
banner.style.left = imgLeft + "px";
BTtons();
}
}
輪播圖2.0
index.html代碼
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<link rel="stylesheet" href="css/index.css">
</head>
<body>
<div class="container">
<div class="wrap" style="left: -600px">
<img src="./img/5.jpg" alt="" class="imgs">
<img src="./img/1.jpg" alt="" class="imgs">
<img src="./img/2.jpg" alt="" class="imgs">
<img src="./img/3.jpg" alt="" class="imgs">
<img src="./img/4.jpg" alt="" class="imgs">
<img src="./img/5.jpg" alt="" class="imgs">
<img src="./img/1.jpg" alt="" class="imgs">
</div>
<div id="buttons">
</div>
<a href="javascript:;" class="lbutton"><</a>
<a href="javascript:;" class="rbutton">></a>
</div>
</body>
<script type="text/javascript" src="js/autoplay.js"></script>
</html>
index.css
* {
padding: 0px;
font-family: 宋體;
margin: 0 auto;
text-align: center;
}
a {
text-decoration: none;
}
.container {
position: relative;
width: 600px;
height: 300px;
top: 100px;
overflow: hidden;
}
.wrap {
position: absolute;
height: 300px;
z-index: 1;
}
.wrap img {
float: left;
width: 600px;
height: 300px;
}
#buttons {
position: absolute;
height: 30px;
right: 10px;
bottom: 10px;
z-index: 2;
cursor: pointer;
}
#buttons span {
display: inline-block;
width: 30px;
height: 30px;
border-radius: 50%;
background: red;
line-height: 30px;
margin-left: 10px;
}
#buttons span.on {
background: green;
}
#buttons span:hover {
background: green;
}
.container .lbutton {
position: absolute;
display: block;
height: 30px;
width: 30px;
line-height: 30px;
top: 135px;
left: 30px;
background: rgba(40,41,41,0.75);
color: red;
}
.container:hover .lbutton{
z-index: 2;
}
.container .lbutton:hover {
background: rgba(40,41,41,1);
}
.container .rbutton {
position: absolute;
display: block;
height: 30px;
width: 30px;
line-height: 30px;
top: 135px;
right: 30px;
background: rgba(40,41,41,0.75);
color: red;
}
.container:hover .rbutton{
z-index: 2;
}
.container .rbutton:hover {
background: rgba(40,41,41,1);
}
autoplay.js
var container = document.querySelector(".container");
var wrap = document.querySelector(".wrap");
var buttons = document.getElementById("buttons");
var spans = document.getElementById("buttons").getElementsByTagName("span");
var prev = document.querySelector(".lbutton");
var next = document.querySelector(".rbutton");
var imgs = document.getElementsByClassName("imgs");
var index = 0;
var pic_time = 1000; //一張圖片切換的時(shí)間
function create_Buttons() {
let cssStr = `.wrap {width: ${imgs.length * 600 + "px"};}`+ `#buttons {width: ${(imgs.length - 1) * 40 + "px"};}`; //動(dòng)態(tài)添加.wrap #buttons 的寬度
let styleNode = document.createElement('style');
styleNode.innerText = cssStr;
document.head.appendChild(styleNode);
buttons.innerHTML += `<span class="on">`+1+`</span>`;
for (let i = 2;i < imgs.length - 1;i ++)
{
buttons.innerHTML += `<span>`+i+`</span>`;
}
}
create_Buttons();
function var_pic(offset) {
var timer = 300; //總位移時(shí)間
var nums = 10; //位移間隔時(shí)間
var var_px = offset / (timer / nums); //每次位移量
var newleft = parseInt(wrap.style.left) + offset;
function to_pic() {
//動(dòng)畫效果開始
if ((var_px < 0 && parseInt(wrap.style.left) > newleft)|| (var_px > 0 && parseInt(wrap.style.left) < newleft )){
wrap.style.left = parseInt(wrap.style.left) + var_px + "px";
setTimeout(to_pic,nums); //調(diào)用自己nums次
} else {
//在第一張和最后一張完成到最后一張和第一張的跳轉(zhuǎn)
if (newleft > -600) {
newleft = ((imgs.length-2) * -600);
}else if (newleft < ((imgs.length-2) * -600)) {
newleft = -600;
}
if (index === spans.length) {
index = 0;
}else if (index === -1) {
index = spans.length - 1;
}
showbutton(index);
wrap.style.left = newleft + "px";
}
}
to_pic();
}
//下一張
next.onclick = function () {
index ++;
var_pic(-600);
}
//上一張
prev.onclick =function () {
index --;
var_pic(600);
}
//按鈕顏色變化
function showbutton(index) {
for (let i = 0;i < spans.length;i ++)
{
spans[i].className = "";
}
spans[index].className = "on";
}
//一直保持監(jiān)聽狀態(tài),監(jiān)聽按鈕是被點(diǎn)擊
for (let i = 0;i< spans.length;i ++)
{
spans[i].onclick= function () {
index = i;
wrap.style.left = i * -600 - 600 + "px";
showbutton(i);
}
}
//圖片自動(dòng)播放事件
var auto_pic = null;
function autoPlay() {
auto_pic = setInterval(function () {
index ++;
var_pic(-600);
},pic_time);
}
autoPlay();
//鼠標(biāo)移入
container.onmouseenter = function () {
clearInterval(auto_pic);
}
//鼠標(biāo)移出
container.onmouseleave = function () {
autoPlay();
}
有些不足之處,還請(qǐng)不吝賜教。