Infinite scrolling carousel map

html This part is not much to say, we all write, I posted the code directly below, not much to explain.

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 I suppose I spent a long time piece, it may be because there is no reason for too long struck the CSS.
Code have my notes, I have do not understand can directly see the notes.

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;
} // initialize css

{#container
width: 467px;
height: 671px;
border: 3px Solid # 333;
position: relative;
margin: Auto 0; // horizontally centered
text-align = left: Center;
overflow: hidden;
} // set the container

{#list
width: 3269px; here I // FIG 7, the length of each image size 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
Run the display: Block; // here to achieve the mouse over the picture button appears
}

#prev {
left: 20px;
}

{#next
right: 20px; < large column  infinite scroll FIG rotation / span>
}

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

JavaScript DOM

Carousel Figure here, DOM is the most important part, which is the core of an implementation of the carousel.

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);
};
//可以一直执行;

STOP function () {
the clearInterval (Timer);
};
// Clear the automatic scroll

container.onmouseover = stop;
when the // mouse into the picture, stop playing
container.onmouseout = change;
when the mouse is not // pictures, automatically play

change();
}

to sum up

  • By writing a carousel view of such a small demo will let me just learned together, for their project to lay a foundation of the future.

Learning Videos

Mu class Network: Focus Carousel drawing special effects , very grateful to the lecturers. He gave me a lot of help. The teacher is relatively clear.

Guess you like

Origin www.cnblogs.com/lijianming180/p/12032688.html