原生JavaScript实现可自定义缩放轮播图效果
- 轮播效果的应用
 - 轮播原理
 - 效果
 - 轮播的实现
 
- HTML 部分
 - CSS 部分
 - JS 部分
 
轮播效果的应用
网站中使用轮播效果,在如今的各大企业门户网站中可谓是重头戏啊,尤其是电子产品厂商。良好的轮播效果,给消费者带来视觉上的冲击,是非常有效的营销手段。这里最具代表性的要属华为、小米的网站,他们对于轮播的使用可以说是刻进基因里的。
轮播原理
轮播效果的原理其实并不神秘,就是替换,后者替换前者
效果

录制效果,改动了切换时间
轮播的实现
这里的实现方式便是 HTML+CSS+JS 也就是我们俗称的 Web三剑客 😃
   HTML 很简单就是用来搭建基础的骨架
   CSS 则是实现我们想要的基础样式效果
   JS 可以称之为灵魂,为什么这么说,是因为JS决定了我们的轮播能否动起来,如何动,以及点击效果的实现。
HTML 部分
<!DOCTYPE html>
<html>
<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">
<title>轮播</title>
<link rel="stylesheet" type="text/css" href="css/lunbo.css" />
</head>
<body>
<div class="wrap">
<ul class="list">
<li class="item active"><img src="img\图片 (14).jpg"></li>
<li class="item"><img src="img\图片 (13).jpg"></li>
<li class="item"><img src="img\图片 (21).jpg"></li>
<li class="item"><img src="img\图片 (23).jpg"></li>
<li class="item"><img src="img\图片(37).jpg"></li>
</ul>
<ul class="pointList">
<li class="point active" data-index="0"></li>
<li class="point" data-index="1"></li>
<li class="point" data-index="2"></li>
<li class="point" data-index="3"></li>
<li class="point" data-index="4"></li>
</ul>
<button type="button" class="btn" id="goPre"><</button>
<button type="button" class="btn" id="goNext">></button>
</div>
<script type="text/javascript" src="js/lunbo.js"></script>
</body>
</html>
CSS 部分
定义整个轮播框架的显示效果
.wrap {
  margin: 0 auto;
  width: 800px;
  height: 450px;
  position: relative;
}
.list {
  width: 800px;
  height: 450px;
  list-style: none;
  position: relative;
  padding-left: 0px;
}
.item {
  width: 100%;
  height: 100%;
  color: white;
  font-size: 50px;
  position: absolute;
  opacity: 0;
  transition: all 0.5s;
}
.btn {
  width: 50px;
  height: 100px;
  position: absolute;
  z-index: 99;
  top: 150px;
  background-color: transparent;
  border: 0px;
  color: whitesmoke;
  font-size: 50px;
}
.btn:hover {
  background-color: #666666;
  opacity: 0.7;
  border: 0px;
}
#goPre {
  left: 0px;
}
#goNext {
  right: 0px;
}
.item.active {
  z-index: 10;
  opacity: 1;
}
.pointList {
  padding-left: 0px;
  list-style: none;
  position: absolute;
  right: 20px;
  bottom: 10px;
  z-index: 1000;
}
.point {
  width: 8px;
  height: 8px;
  background-color: rgba(0, 0, 0, 0.4);
  border-radius: 100%;
  float: left;
  margin-right: 14px;
  border-style: solid;
  border-width: 2px;
  border-color: rgba(255, 255, 255, 0.6);
  cursor: pointer;
}
.point.active {
  background-color: rgba(255, 255, 255, 0.5);
}
img {
  width: 100%;
  height: 100%;
}JS 部分
因为是原生JS所以看起来会有些,不那么好理解
var items = document.getElementsByClassName("item"); // 图片
var points = document.getElementsByClassName("point"); // 点
var goPreBtn = document.getElementById("goPre"); // 按钮-->上一张图片
var goNextBtn = document.getElementById("goNext"); // 按钮-->下一张图片
var time = 0; // 定时器图片跳转参数
var index = 0; // 第几章图片在展示 --> 第index张图片有active这个类名
var clearActive = function() {
  for (let i = 0; i < items.length; i++) {
    items[i].className = "item";
  }
  for (let j = 0; j < points.length; j++) {
    points[j].className = "point";
  }
}
var goIndex = function() {
  clearActive();
  items[index].className = "item active";
  points[index].className = "point active";
  time = 0;
}
var goPre = function() {
  if (index == 0) {
    index = 4;
  } else {
    index--;
  }
  goIndex();
}
var goNext = function() {
  if (index < 4) {
    index++;
  } else {
    index = 0;
  }
  goIndex();
}
goPreBtn.addEventListener("click", function() {
  goPre();
});
goNextBtn.addEventListener("click", function() {
  goNext();
});
setInterval(function() {
  time++;
  if (time == 40) {
    goNext();
  }
}, 50);
/* 小圆圈点击 */
for (var i = 0; i < points.length; i++) {
  points[i].addEventListener('click', function() {
    index = this.getAttribute('data-index');
    goIndex();
  });
}                










