无限滚动轮播图

html这部分没有什么好说的,大家都会写,我直接把代码贴在下面,就不多解释了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Page Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" media="screen" href="main.css" />
<script src="main.js"></script>
</head>

<body>
<div id="container">
<div id="list" style="left: -467px;">
<img src="images/5.jpg">
<img src="images/1.jpg">
<img src="images/2.jpg">
<img src="images/3.jpg">
<img src="images/4.jpg">
<img src="images/5.jpg">
<img src="images/1.jpg"> //实现无限滚动的关键
</div>
<div id="buttons">
<span index="1" class="on"></span>
<span index="2"></span>
<span index="3"></span>
<span index="4"></span>
<span index="5"></span>
</div>
<a href="javascript:;" id="prev" class="arrow">&lt;</a>
<a href="javascript:;" id="next" class="arrow">&gt;</a>
</div>

</body>

</html>

CSS

CSS算是我耗费时间比较长的一块,也可能是因为太长时间没有敲过CSS的原因。
代码里有我的备注,大家有不太懂的可以直接看备注。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
* {
margin: 0;
padding: 0;
text-decoration: none;
} //初始化css

#container {
width: 467px;
height: 671px;
border: 3px solid #333;
position: relative;
margin: 0 auto; //水平居中
text-align: center;
overflow: hidden;
} //设置容器

#list {
width: 3269px; //我这里有7张图,所以长度是7*每张图片的大小
height: 671px;
position: absolute;
z-index: 1;
}

#list img {
float: left;
}

#buttons {
position: absolute;
height: 10px;
width: 100px;
z-index: 2;
bottom: 20px;
left: 42%;
}

#buttons span {
cursor: pointer;
float: left;
border: 1px solid #fff;
width: 10px;
height: 10px;
border-radius: 50%;
background: #333;
margin-right: 5px;
}

#buttons .on {
background: orangered;
}

.arrow {
cursor: pointer;
display: none;
line-height: 39px;
text-align: center;
font-size: 36px;
font-weight: bold;
width: 40px;
height: 40px;
position: absolute;
z-index: 2;
top: 50%;
background-color: RGBA(0, 0, 0, .3);
color: #fff;
}

.arrow:hover {
background-color: RGBA(0, 0, 0, .7);
}

#container:hover .arrow {
display: block; //这里实现将鼠标放到图片上按钮出现
}

#prev {
left: 20px;
}

#next {
right: 20px;< 大专栏  无限滚动轮播图/span>
}

img {
width: 467px;
height: 671px;
}

JavaScript DOM

轮播图这里,DOM是最重要的部分了,这是轮播图实现的核心。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
window.onload = function() {
var container = document.getElementById("container");
var prev = document.getElementById("prev");
var next = document.getElementById("next");
var list = document.getElementById("list");
var buttons = document.getElementsByTagName("span");
var index = 1;
var animated = false;
//做一个优化
var timer;

function showButton() {
if (index == 6)
//如果不判断,index会一直加。
index = 1;
else if (index == 0)
index = 5;
for (var i = 0; i < buttons.length; i++) {
if (buttons[i].className === 'on')
buttons[i].className = '';
}
buttons[index - 1].className = 'on';
};

function animate(offset) {
animated = true;
var newLeft = parseInt(list.style.left) + offset;
var time = 500;
//动画总时间;
var interval = 10;
//位移间隔时间;
var speed = offset / (time / interval);
//每次移动量;


function go() {
if (speed < 0 && parseInt(list.style.left) > newLeft || (speed > 0 && parseInt(list.style.left) < newLeft)) {
list.style.left = parseInt(list.style.left) + speed + 'px';
setTimeout(go, interval);
//再次调用,看是否满足条件;递归;实现动画效果
} else {
animated = false;
list.style.left = newLeft + 'px';
//更新坐标
if (newLeft > -467)
list.style.left = -467 * 5 + 'px';
//无限滚动,如果我们的Left值大于第一张图的值时,即需要转换到最后一张图。
if (newLeft < -467 * 5)
list.style.left = -467 + 'px';
//与上面一样,实现的是最后一张到第一张的的转变
}
}
go();
}

next.onclick = function() {
index += 1;
showButton();
if (!animated) {
animate(-467);
}
};
//

prev.onclick = function() {
index -= 1;
showButton();
if (!animated) {
animate(467);
}
};

for (var i = 0; i < buttons.length; i++) {
buttons[i].onmouseover = function() {
if (this.className == 'on')
return;
//优化处理,当鼠标移动到同一个元素上,不会执行函数;
var myIndex = parseInt(this.getAttribute('index'));
var offset = (-467 * (myIndex - index));
index = myIndex;
showButton();
if (!animated) {
animate(offset);
}
}
};

function change() {
timer = setInterval(function() {
next.onclick();
}, 3000);
};
//可以一直执行;

function stop() {
clearInterval(timer);
};
//清除自动滚动

container.onmouseover = stop;
//鼠标放到图片上时,停止播放
container.onmouseout = change;
//鼠标不在图片上时,自动播放

change();
}

总结

  • 通过写一个轮播图这样的小demo能让我将学到的只是串联起来,为自己以后做项目打下的基础。

学习视频

慕课网:焦点轮播图特效,很感谢这位讲师。给了我很大的帮助。老师讲的也比较清晰明了。

猜你喜欢

转载自www.cnblogs.com/lijianming180/p/12032688.html