-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcarousel.html
163 lines (147 loc) · 4.78 KB
/
carousel.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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<script type="text/javascript" src='http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js'></script>
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
#container {
width: 300px;
height: 200px;
overflow: hidden;
border: 1px solid gray;
position: relative;
}
p {
width: 100%;
height: 100%;
top: 0;
position: absolute;
}
#p1 {
background: salmon;
}
#p2 {
background: yellowgreen;
left: 100%;
}
#p3 {
background: forestgreen;
left: 100%;
}
i {
width: 25px;
height: 50px;
background: firebrick;
position: absolute;
top: 50%;
transform: translateY(-50%);
}
i.left {
left: 0;
}
i.right {
right: 0;
}
#cursors {
position: absolute;
bottom: 10px;
right: 10px;
display: flex;
}
a {
padding: 4px;
margin-right: 10px;
background: #fff;
text-decoration: none;
}
a.active {
background: yellow;
color: fuchsia;
}
</style>
</head>
<body>
<div id='container'>
<p id='p1'>p1</p>
<p id='p2'>p2</p>
<p id='p3'>p3</p>
<div id='cursors'>
<a index='0' class='active' href="javascript:;">1</a>
<a index='1' href="javascript:;">2</a>
<a index='2' href="javascript:;">3</a>
</div>
<i class='left'></i>
<i class='right'></i>
</div>
<script>
$('a').click(debunce(jump, 500, true))/$
function jump() {
var index = this.getAttribute('index');
$('a').eq(index).addClass('active').siblings().removeClass('active')
if (inow != index) {
$('#container p').eq(inow).animate({ 'left': '-100%' }, 500);
$('#container p ').eq(index).css({ 'left': '100%' });
$('#container p').eq(index).animate({ 'left': '0px' }, 500);
inow = index;
}
}
$('#container').mouseover(() => {
clearInterval(time);
}).mouseout(() => {
time = setInterval(change, 1800);
})
$('.left').click(debunce(leftChange, 500, true))
$('.right').click(debunce(change, 500, true))
function debunce(fn, delay, immediate) {
var timer = null;
return function () {
var context = this;
console.log(context,'context')
if (timer) clearTimeout(timer)
if (immediate) {
//根据距离上次触发操作的时间是否到达delay来决定是否要现在执行函数
var doNow = !timer;
//每一次都重新设置timer,就是要保证每一次执行的至少delay秒后才可以执行
timer = setTimeout(function () {
timer = null;
}, delay);
//立即执行
if (doNow) {
fn.apply(context)
// fn()
}
} else {
timer = setTimeout(function () {
fn()
}, delay);
}
}
}
function leftChange() {
$('#container p').eq(inow).animate({ 'left': '100%' }, 500);
inow--;
inow = inow % 3;
$('a').eq(inow).attr('class','active').siblings().removeClass();
$('#container p ').eq(inow).css({ 'left': '-100%' });
$('#container p').eq(inow).animate({ 'left': '0px' }, 500);
}
var time = setInterval(change, 1800);
var inow = 0;
function change() {
$('#container p').eq(inow).animate({ 'left': '-100%' }, 500);
inow++;
inow = inow % 3;
$('a').eq(inow).attr('class','active').siblings().removeClass();
$('#container p ').eq(inow).css({ 'left': '100%' });
$('#container p').eq(inow).animate({ 'left': '0px' }, 500);
}
</script>
</body>
</html>