网易轮播图一款业内相比较而言比较好的轮播图
HTML结构
<!Doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>网易轮播图</title>
// 引入Css外链样式文件
<link rel="stylesheet" href="css/slider.css">
</head>
<body>
<div class="slider" id="slider">
<div class="slider-scroll" id="slider_scroll">
<div class="slider-main" id="slider_main">
<div class="slider-main-img">
<a href="">
![](images/banner1.png)
</a>
</div>
<div class="slider-main-img">
<a href="">
![](images/banner2.jpg)
</a>
</div>
<div class="slider-main-img">
<a href="">
![](images/banner3.jpg)
</a>
</div>
<div class="slider-main-img">
<a href="">
![](images/banner4.jpg)
</a>
</div>
<div class="slider-main-img">
<a href="">
![](images/ad2.jpg)
</a>
</div>
<div class="slider-main-img">
<a href="">
![](images/ad6.jpg)
</a>
</div>
</div>
</div>
<!-- 控制区域 -->
<div class="slider-ctl" id="slider_ctl">
<span class="slider-ctl-prev"></span>
<span class="slider-ctl-next"></span>
</div>
</div>
// 引入自己封装好函数的工具包
<script src="js/MyPack.js"></script>
// 引入Js外链文件
<script src="js/slider.js"></script>
</body>
</html>
Css样式
*{
margin: 0;
padding: 0;
border: 0;
}
img{
vertical-align: top;
}
.slider{
width: 310px;
height: 260px;
position: relative;
margin: 100px auto;
overflow: hidden;
}
.slider-scroll{
width: 310px;
height: 220px;
position: relative;
}
.slider-main{
width: 620px;
height: 220px;
}
.slider-main-img{
width: 310px;
height: 220px;
position: absolute;
}
.slider-main-img img{
height: 100%;
width: 100%;
}
.slider-ctl-prev, .slider-ctl-next{
background: url("../images/icon.png");
width: 20px;
height: 34px;
position: absolute;
top: 50%;
margin-top: -35px;
}
.slider-ctl-prev{
left:5px;
}
.slider-ctl-next{
background-position: -9px -45px;
right:5px;
}
.slider-ctl-icon{
width: 24px;
height: 5px;
background: url("../images/icon.png") no-repeat -24px -790px;
display: inline-block;
margin:5px;
text-indent:20em;
overflow: hidden;
}
.slider-ctl{
text-align: center;
cursor: pointer;
}
.current{
background-position: 0 -770px;
}
Js代码
window.onload = function () {
// 1. 获取标签
var slider = $('slider');
var sliderMain = $('slider_main');
var sliderMainImg = sliderMain.children;
var sliderCtl = $('slider_ctl');
// 记录全局索引
var key = 0;
// 2.动态创建span
for (var i = 0; i < sliderMainImg.length; i++) {
var span = document.createElement('span');
span.setAttribute('class', 'slider-ctl-icon');
span.innerHTML = sliderMainImg.length - i;
sliderCtl.insertBefore(span, sliderCtl.children[1]);
}
// 2.1 第一个被选中
sliderCtl.children[1].setAttribute('class', 'slider-ctl-icon current');
// 3. 原始定位
var scrollW = slider.offsetWidth;
for (var i = 1; i < sliderMainImg.length; i++) {
sliderMainImg[i].style.left = scrollW + 'px';
}
// 4. 监听鼠标点击
var sliderCtlChildren = sliderCtl.children;
for (var i = 0; i < sliderCtlChildren.length; i++) {
var single = sliderCtlChildren[i];
single.onmousedown = function () {
if (this.className == 'slider-ctl-prev') {
// 点击左边
/*
1.当前的图片做动画右移
2.下一张图片快速出现在可视区域的左边
3.做动画进入
*/
animate(sliderMainImg[key], {left: scrollW}, null);
key--;
if (key < 0) {
key = sliderMainImg.length - 1;
}
sliderMainImg[key].style.left = -scrollW + 'px';
animate(sliderMainImg[key], {left: 0}, null);
}
else if (this.className == 'slider-ctl-next') {
/*
* 点击右边
* 自动播放 就相当于一直点右边的按钮
* 直接调用
* */
autoPlay();
}
else {
/*
* 点击下边
* 用当前点击的索引和选中的序号做对比
* 点击的>选中的 右边
* 点击的<选中的 左边
* */
var index = this.innerHTML - 1;
alert(index);
// 对比 --- 序号和索引号
if (key > index) {
// 点击左边
/*
1.当前的图片做动画右移
2.下一张图片快速出现在可视区域的左边
3.做动画进入
*/
animate(sliderMainImg[key], {left: scrollW}, null);
sliderMainImg[index].style.left = -scrollW + 'px';
}
// 对比 --- 序号和索引号
else if (key < index) {
// 点击右边
/*
1.当前的图片做动画左移
2.下一张图片快速出现在可视区域的右边
3.做动画进入
*/
animate(sliderMainImg[key], {left: -scrollW}, null);
sliderMainImg[index].style.left = scrollW + 'px';
}
key = index;
animate(sliderMainImg[key], {left: 0}, null);
}
// 更新索引
changeIndex();
}
}
/*
* 5.切换索引
* */
function changeIndex() {
// 5.1 所有的都不被选中
for (var i = 1; i < sliderCtlChildren.length - 1; i++) {
sliderCtlChildren[i].className = 'slider-ctl-icon';
}
sliderCtlChildren[key + 1].className = 'slider-ctl-icon current';
}
// 6.自动播放
var timer = setInterval(autoPlay, 1000);
function autoPlay() {
// 点击右边
/*
1.当前的图片做动画左移
2.下一张图片快速出现在可视区域的右边
3.做动画进入
*/
animate(sliderMainImg[key], {left: -scrollW}, null);
key++;
if (key > sliderMainImg.length - 1) {
key = 0;
}
sliderMainImg[key].style.left = scrollW + 'px';
animate(sliderMainImg[key], {left: 0}, null);
// 更新索引
changeIndex();
}
// 7. 鼠标经过 清除定时器
slider.onmouseover = function () {
clearInterval(timer);
}
slider.onmouseout = function () {
timer = setInterval(autoPlay, 1000);
}
}
特效展示
案例所需精灵图