-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSwiper.html
More file actions
272 lines (234 loc) · 8.39 KB
/
Swiper.html
File metadata and controls
272 lines (234 loc) · 8.39 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
<!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">
<title>Document</title>
<style>
html,
body {
position: relative;
height: 100%;
}
body {
background: #eee;
font-family: Helvetica Neue, Helvetica, Arial, sans-serif;
font-size: 14px;
color: #000;
margin: 0;
padding: 0;
overflow: hidden;
}
#viewer {
height: 100%;
width: 100%;
background: #ddd;
}
#header {
height: 50px;
background: #000;
}
.center {
position: absolute;
/* left: 50%; */
width: 100%;
height: 800px;
/* margin: 0 auto;
transform: translate(-500px, 0); */
}
.swiper-container {
margin: 0 auto;
position: relative;
overflow: hidden;
list-style: none;
z-index: 1;
height: 100%;
width: 100%;
}
.swiper-wrapper {
position: relative;
width: 100%;
height: 100%;
z-index: 1;
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
-webkit-transition-property: -webkit-transform;
transition-property: -webkit-transform;
-o-transition-property: transform;
transition-property: transform;
transition-property: transform, -webkit-transform;
-webkit-box-sizing: border-box;
box-sizing: border-box;
}
.swiper-slide {
-webkit-flex-shrink: 0;
-ms-flex-negative: 0;
flex-shrink: 0;
/* 属性指定了 flex 元素的收缩规则。flex 元素仅在默认宽度
之和大于容器的时候才会发生收缩,其收缩的大小是依据 flex-shrink 的值。默认是1*/
width: 100%;
height: 100%;
position: relative;
-webkit-transition-property: -webkit-transform;
transition-property: -webkit-transform;
-o-transition-property: transform;
transition-property: transform;
transition-property: transform, -webkit-transform;
}
</style>
<script src="jquery1.11.1.min.js"></script>
</head>
<body>
<div id="viewer">
<div id="header">
</div>
<div class="center">
<div class="swiper-container">
<div class="swiper-wrapper">
<div class="swiper-slide" style="background: darkcyan"></div>
<div class="swiper-slide" style="background: darkgreen"></div>
<div class="swiper-slide" style="background: darkkhaki"></div>
</div>
</div>
</div>
</div>
</body>
</html>
<script>
function IsPC() {
var userAgentInfo = navigator.userAgent;
var Agents = ["Android", "iPhone",
"SymbianOS", "Windows Phone",
"iPad", "iPod"];
var flag = true;
for (var v = 0; v < Agents.length; v++) {
if (userAgentInfo.indexOf(Agents[v]) > 0) {
flag = false;
break;
}
}
return flag;
}
var Swiper = (function () {
var Swiper;
function swiper() {
this.width = 0;
this.wrapper = $(".swiper-wrapper");
this.touchFlag = false;
this.activePointX = 0;
this.direction = "right";
this.isEdge = true;
this.currentSlideIndex = 0;
this.minTransformWidth=0;
this.maxTransformWidth=0;
this.slideCount = 0;
this.currentTransform = 0;
this.init();
}
Swiper = swiper;
swiper.prototype.init = function () {
this.width = $(".swiper-wrapper").width();
this.setEverySlide(this.width);
this.setEvent();
}
swiper.prototype.setEverySlide = function (width) {
var _this = this;
$(".swiper-slide").each(function (i, v) {
$(v).width(width);
_this.slideCount++;
});
this.maxTransformWidth=(this.slideCount-1)*width;
}
swiper.prototype.setEvent = function () {
var isPc = IsPC();
if (isPc) {
this.setPcEvent();
}
else {
this.setTouchEvent();
}
}
swiper.prototype.changeEdge=function(direction){
if(direction=="--"&&this.currentSlideIndex==0){
this.isEdge=true;
}
else if(direction=="++"&&this.currentSlideIndex==this.slideCount-1){
this.isEdge=true;
}
else{
this.isEdge=false;
}
}
swiper.prototype.activeMove = function (direction) {
if (direction == "--"&&this.isEdge==false) {
this.currentSlideIndex--;
this.moveTo(-this.currentSlideIndex);
}
if (direction == "++"&&this.isEdge==false) {
this.currentSlideIndex++;
this.moveTo(-this.currentSlideIndex);
}
}
swiper.prototype.setPcEvent = function () {
var _this = this;
this.wrapper.on("mousedown", function (e) {
_this.touchFlag = true;
_this.activePointX = e.clientX;
_this.wrapper.css("transition", "");
});
this.wrapper.on("mouseup", function (e) {
_this.touchFlag = false;
var moveWidth = e.clientX - _this.activePointX;
if (moveWidth > 0) { _this.direction = "--" }
else { _this.direction = "++" }
_this.changeEdge(_this.direction);
_this.activeMove(_this.direction);
_this.wrapper.css("transition", "all .5s");
});
this.wrapper.on("mousemove", function (e) {
if (_this.touchFlag) {
var moveWidth = _this.currentTransform + e.clientX - _this.activePointX;
if(moveWidth>=_this.minTransformWidth||moveWidth<=-_this.maxTransformWidth){_this.isEdge=true;return;}
_this.isEdge=false;
$(this).css("transform", "translate3d(" + moveWidth + "px, 0px, 0px)");
}
});
}
swiper.prototype.setTouchEvent = function () {
var _this = this;
this.wrapper.on("touchstart", function (e) {
var touch = e.originalEvent.targetTouches[0];
_this.touchFlag = true;
_this.activePointX = touch.pageX;
_this.wrapper.css("transition", "");
});
this.wrapper.on("touchend", function (e) {
var touch = e.originalEvent.changedTouches[0];
var moveWidth = touch.pageX - _this.activePointX;
if (moveWidth > 0) { _this.direction = "--" }
else { _this.direction = "++" }
_this.changeEdge(_this.direction);
_this.activeMove(_this.direction);
_this.wrapper.css("transition", "all .5s");
});
this.wrapper.on("touchmove", function (e) {
var touch = e.originalEvent.targetTouches[0];
if (_this.touchFlag) {
var moveWidth = _this.currentTransform + touch.pageX - _this.activePointX;
if(moveWidth>=_this.minTransformWidth||moveWidth<=-_this.maxTransformWidth){_this.isEdge=true;return;}
_this.isEdge=false;
$(this).css("transform", "translate3d(" + moveWidth + "px, 0px, 0px)");
}
});
}
swiper.prototype.moveTo = function (index) {
this.wrapper.css("transform", "translate3d(" + index * this.width + "px, 0px, 0px)");
this.currentTransform = index * this.width;
}
return Swiper;
})()
new Swiper();
</script>