HTML部分
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>轮播图测试</title>
<link rel="stylesheet" href="css/main.css">
<script src="js/main.js"></script>
</head>
<body>
<div class="container"> <!--最大的容器-->
<!--让舞台默认显示第一张图片 (第一张左边还有一张 最后一张)-->
<div class="wrap" style="left:-600px"> <!--存放图片的容器-->
<img src="image/5.jpg"> <!--多加的图片用于无缝连接-->
<img src="image/1.jpg">
<img src="image/2.jpg">
<img src="image/3.jpg">
<img src="image/4.jpg">
<img src="image/5.jpg">
<img src="image/1.jpg">
</div>
<div class="buttons"><!--按钮容器-->
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ul>
</div>
<a href="javascript:;" class="arrow arrow_left"><</a> <!--左箭头-->
<a href="javascript:;" class="arrow arrow_right">></a> <!--右箭头-->
</div>
</body>
</html>
CSS部分
*{
margin: 0;
padding: 0;
}
.container{
width: 600px;
height: 400px;
position: relative;
margin: 100px auto 0 auto;
overflow: hidden;
box-shadow: 0 0 5px black;
}
.container .wrap{
width: 4200px;
height: 400px;
position: absolute;
transition: 1s;
}
.container .wrap img{
float: left;
width: 600px;
height: 400px;
}
.buttons{
z-index: 1;
position: relative;
bottom: -370px;
margin: auto;
width: 160px;
height: 20px;
}
.buttons ul {
list-style: none;
}
.buttons ul li{
float: left;
width: 20px;
height: 20px;
border-radius: 50%;
margin-left: 10px;
background-color: #ffffff;
text-align: center;
}
.buttons ul li:hover{
cursor: pointer;
}
.buttons ul li.on{
background-color: darkgray;
}
.container .arrow{
position: absolute;
width: 70px;
height: 70px;
border-radius: 50%;
font-size: 60px;
line-height: 60px;
text-align: center;
top: 150px;
color: white;
display: none;
}
.container:hover .arrow{
display: block;
}
.container a{
text-decoration: none;
}
.container .arrow:hover{
background-color:rgba(169,169,169,0.5);
}
.arrow_left{
left: 10px;
}
.arrow_right{
right: 10px;
}
JS部分
window.onload=function () {
var wrap=document.querySelector(".wrap");
var arrow_left=document.querySelector(".arrow_left");
var arrow_right=document.querySelector(".arrow_right");
var DotsPos=0;
arrow_left.onclick=function () {
pre_Pic();
}
arrow_right.onclick=function () {
next_Pic();
}
function next_Pic(){
var newLeft;
if(wrap.style.left==="-3600px"){
newLeft= -1200;
}else {
newLeft=parseInt(wrap.style.left)-600;
}
wrap.style.left=newLeft+"px";
DotsPos++;
if (DotsPos>4){
DotsPos=0;
}
ShowCurrentDotsPos();
}
function pre_Pic(){
var newLeft;
if(wrap.style.left==="0px"){
newLeft= -2400;
}else {
newLeft=parseInt(wrap.style.left)+600;
}
wrap.style.left=newLeft+"px";
DotsPos--;
if (DotsPos<0){
DotsPos=4;
}
ShowCurrentDotsPos();
}
var Dots=document.getElementsByTagName("li");
function ShowCurrentDotsPos() {
for(var i=0;i<Dots.length;i++){
Dots[i].className="";
}
Dots[DotsPos].className="on";
}
ShowCurrentDotsPos();
var timer=null;
function autoplay() {
timer=setInterval(next_Pic,5000);
}
autoplay();
var container=document.querySelector(".container");
container.onmouseover=function () {
clearInterval(timer);
}
container.onmouseout=function () {
autoplay();
}
for (var i=0;i<Dots.length;i++){
(function (i) {
Dots[i].onclick=function () {
if (DotsPos!=i){
var changePos;
changePos=DotsPos-i;
if(DotsPos==0&&wrap.style.left==="-3600px"){
changePos=changePos+5;
}
if(DotsPos==4&&wrap.style.left==="0px"){
changePos=changePos-5;
}
wrap.style.left=(parseInt(wrap.style.left)+changePos*600)+"px";
DotsPos=i;
ShowCurrentDotsPos();
}
}
})(i);
}
}