JS封装移动端触摸滑动插件应用于导航banner【精装版】
自己封装了一个小插件,一个小尝试。 可用于类似于京东导航等效果,效果多样化,很方便。 欢迎大家提点意见。
mrChenSwiper( {parent, child, type, parentN, childN, goli,lisname} );
mrChenSwipe({
parent : 父盒子 [父盒子的高度会继承父级] , 必填
child : 子盒子 [ul,子盒子需要用ul>li>a], 必填
type : 滑动方向 [vertical || level], 必填
parentN : 垂直-父盒子宽度 ~ 默认值200 || 水平-父盒子高度 ~ 默认值200, 选填
childN : 垂直-li高度 ~ 默认值40 || 水平-li宽度 ~ 默认值80, 选填
goli : 是否开启点击导航 [true || false], 选填
lisname : 选中标签类名 ~ 默认值 'current' 选填
})
注意:请传入原生DOM对象。
0 | function mrChenSwipe(obj) { |
1 | /* |
2 | startD //开始的坐标 |
3 | moveD //滑动时的坐标 |
4 | goD //改变的距离 |
5 | lis //获取所有li |
6 | distance //吸附距离 |
7 | maxPosition //最小定位区间 |
8 | minSwipe //滑动最小定位区间 |
9 | maxSwipe //滑动最大定位区间 |
10 | currSwipe //当前定位 |
11 | xy //XY |
12 | */ |
13 | |
14 | var parent = obj.parent, |
15 | child = obj.child, |
16 | type = obj.type, |
17 | goli = obj.goli, |
18 | lisname = obj.lisname || 'current', |
19 | startD = 0, |
20 | moveD = 0, |
21 | goD = 0, |
22 | lis = child.querySelectorAll('li'), |
23 | lisLength = lis.length, |
24 | distance = 100, |
25 | maxPosition = 0, |
26 | minSwipe, maxSwipe, currSwipe = 0, |
27 | xy; |
28 | |
29 | |
30 | /*加过渡*/ |
31 | var addTransition = function(elm) { |
32 | |
33 | elm.style.webkitTransition = 'all 0.2s'; /*兼容*/ |
34 | |
35 | elm.style.transition = 'all 0.2s'; |
36 | |
37 | }; |
38 | |
39 | /*删除过渡*/ |
40 | var removeTransition = function(elm) { |
41 | |
42 | elm.style.webkitTransition = 'none'; /*兼容*/ |
43 | |
44 | elm.style.transition = 'none'; |
45 | |
46 | }; |
47 | |
48 | //设置定位 |
49 | var setTranslate = function(elm, direction, num) { |
50 | |
51 | if (direction == 'y') { |
52 | |
53 | elm.style.webkitTransform = 'translateY(' + num + 'px)'; /*兼容*/ |
54 | |
55 | elm.style.transform = 'translateY(' + num + 'px)'; |
56 | |
57 | } |
58 | |
59 | if (direction == 'x') { |
60 | |
61 | elm.style.webkitTransform = 'translateX(' + num + 'px)'; /*兼容*/ |
62 | |
63 | elm.style.transform = 'translateX(' + num + 'px)'; |
64 | |
65 | } |
66 | |
67 | }; |
68 | |
69 | |
70 | |
71 | //判断如果为上下滑动,获取宽高 |
72 | //垂直 |
73 | if (type == 'vertical') { |
74 | |
75 | //默认宽度 |
76 | parentN = obj.parentN || 200; |
77 | |
78 | //li默认高度 |
79 | childN = obj.childN || 40; |
80 | |
81 | parent.style.width = parentN + 'px'; |
82 | |
83 | parent.style.height = '100%'; |
84 | |
85 | var parentWH = parent.offsetHeight; |
86 | |
87 | var childWH = childN * lisLength; |
88 | |
89 | //获取定位最小区间 |
90 | minPosition = parentWH - childWH; |
91 | |
92 | minSwipe = minPosition - distance; |
93 | |
94 | maxSwipe = maxPosition + distance; |
95 | |
96 | setTranslate(child, 'y', 0); |
97 | |
98 | goV(); |
99 | |
100 | } |
101 | |
102 | //水平 |
103 | if (type == 'level') { |
104 | |
105 | //默认高度 |
106 | parentN = obj.parentN || 200; |
107 | |
108 | //li默认宽度 |
109 | childN = obj.childN || 80; |
110 | |
111 | //父盒子默认100%宽度 |
112 | parent.style.width = document.documentElement.clientWidth + 'px'; |
113 | |
114 | parent.style.height = parentN + 'px'; |
115 | |
116 | //子盒子宽度 |
117 | child.style.width = lisLength * childN + 'px'; |
118 | |
119 | child.style.height = parentN + 'px'; |
120 | |
121 | var parentWH = parent.offsetWidth; |
122 | |
123 | var childWH = childN * lisLength; |
124 | |
125 | //获取定位最小区间 |
126 | minPosition = parentWH - childWH; |
127 | |
128 | minSwipe = minPosition - distance; |
129 | |
130 | maxSwipe = maxPosition + distance; |
131 | |
132 | setTranslate(child, 'x', 0); |
133 | |
134 | goL(); |
135 | |
136 | } |
137 | |
138 | //水平初始化 |
139 | |
140 | function goL() { |
141 | |
142 | var i = 0; |
143 | |
144 | for (i; i < lisLength; i += 1) { |
145 | |
146 | lis[i].style.width = childN + 'px'; |
147 | |
148 | lis[i].style.height = '100%'; |
149 | |
150 | lis[i].style.textAlign = 'center'; |
151 | |
152 | lis[i].style.lineHeight = parentN + 'px'; |
153 | |
154 | lis[i].style.float = 'left'; |
155 | |
156 | aStyle(lis[i]); |
157 | |
158 | } |
159 | |
160 | } |
161 | |
162 | //垂直初始化 |
163 | |
164 | function goV() { |
165 | |
166 | var i = 0; |
167 | |
168 | for (i; i < lisLength; i += 1) { |
169 | |
170 | lis[i].style.width = '100%'; |
171 | |
172 | lis[i].style.height = childN + 'px'; |
173 | |
174 | lis[i].style.textAlign = 'center'; |
175 | |
176 | lis[i].style.lineHeight = childN + 'px'; |
177 | |
178 | aStyle(lis[i]); |
179 | |
180 | } |
181 | } |
182 | |
183 | //给a设置style |
184 | |
185 | function aStyle(dom) { |
186 | |
187 | dom.querySelector('a').style.display = 'block'; |
188 | |
189 | dom.querySelector('a').style.height = '100%'; |
190 | |
191 | dom.querySelector('a').style.color = '#333'; |
192 | |
193 | dom.querySelector('a').style.textDecoration = 'none'; |
194 | |
195 | } |
196 | |
197 | //子盒子绑定滑动事件 |
198 | child.addEventListener('touchstart', function(e) { |
199 | |
200 | if (type == 'vertical') { |
201 | |
202 | startD = e.touches[0].clientY; |
203 | |
204 | } |
205 | |
206 | if (type == 'level') { |
207 | |
208 | startD = e.touches[0].clientX; |
209 | |
210 | } |
211 | |
212 | }) |
213 | |
214 | |
215 | //滑动中 |
216 | child.addEventListener('touchmove', function(e) { |
217 | |
218 | if (type == 'vertical') { |
219 | |
220 | moveD = e.touches[0].clientY; |
221 | |
222 | xy = 'y'; |
223 | |
224 | } |
225 | |
226 | if (type == 'level') { |
227 | |
228 | moveD = e.touches[0].clientX; |
229 | |
230 | xy = 'x'; |
231 | } |
232 | |
233 | goD = moveD - startD + currSwipe; |
234 | |
235 | removeTransition(child); |
236 | |
237 | if (goD > minSwipe && goD < maxSwipe) { |
238 | |
239 | setTranslate(child, xy, goD); |
240 | |
241 | } |
242 | |
243 | }) |
244 | |
245 | //滑动结束 |
246 | window.addEventListener('touchend', function() { |
247 | |
248 | //最终定位 |
249 | if (goD > maxPosition) { |
250 | |
251 | currSwipe = maxPosition; |
252 | |
253 | addTransition(child); |
254 | |
255 | setTranslate(child, xy, currSwipe); |
256 | |
257 | } else if (goD < minPosition) { |
258 | |
259 | currSwipe = minPosition; |
260 | |
261 | console.log(minPosition); |
262 | |
263 | addTransition(child); |
264 | |
265 | setTranslate(child, xy, currSwipe); |
266 | } else { |
267 | |
268 | currSwipe = goD; |
269 | |
270 | } |
271 | |
272 | startD = 0; |
273 | moveD = 0; |
274 | goD = 0; |
275 | |
276 | }) |
277 | |
278 | |
279 | //如果开启单机索引 |
280 | if (goli) { |
281 | |
282 | //循环绑定点击事件 |
283 | for (var j = 0; j < lisLength; j += 1) { |
284 | |
285 | lis[j].index = j; |
286 | |
287 | tap(lis[j], function(e) { |
288 | |
289 | if (type == 'level') { |
290 | xy = 'x'; |
291 | } |
292 | |
293 | if (type == 'vertical') { |
294 | xy = 'y'; |
295 | } |
296 | |
297 | //找到父元素 |
298 | var li = e.target.parentNode; |
299 | |
300 | for (var n = 0; n < lisLength; n += 1) { |
301 | |
302 | lis[n].className = ''; |
303 | |
304 | } |
305 | |
306 | li.className = lisname; |
307 | |
308 | //计算位置 |
309 | var tapGoD = -li.index * childN; |
310 | |
311 | if (tapGoD > minPosition) { |
312 | |
313 | currSwipe = tapGoD; |
314 | |
315 | addTransition(child); |
316 | |
317 | setTranslate(child, xy, currSwipe); |
318 | |
319 | } else { |
320 | |
321 | currSwipe = minPosition; |
322 | |
323 | setTranslate(child, xy, currSwipe); |
324 | |
325 | } |
326 | |
327 | }) |
328 | |
329 | } |
330 | |
331 | } |
332 | |
333 | //封装单机事件 |
334 | |
335 | function tap(dom, callback) { |
336 | |
337 | /*判断dom是不是一个对象 如果是才给他绑定事件*/ |
338 | if (typeof dom == 'object') { |
339 | |
340 | /*判断是否滑动过*/ |
341 | var isMove = false; |
342 | |
343 | /*记录刚刚触摸到屏幕的时候的时间*/ |
344 | var time = 0; |
345 | |
346 | dom.addEventListener('touchstart', function(e) { |
347 | |
348 | /*刚刚触摸到屏幕的时候的时间*/ |
349 | time = Date.now(); |
350 | |
351 | }); |
352 | |
353 | dom.addEventListener('touchmove', function(e) { |
354 | |
355 | /*设置为true*/ |
356 | isMove = true; |
357 | |
358 | }); |
359 | |
360 | window.addEventListener('touchend', function(e) { |
361 | |
362 | /* |
363 | * tap事件的要求 |
364 | *1.没有滑动过 |
365 | *2.响应时间在150ms以内 |
366 | * */ |
367 | if (!isMove && (Date.now() - time) < 150) { |
368 | |
369 | /*为了提高响应的速度*/ |
370 | callback && callback(e); |
371 | |
372 | } |
373 | |
374 | isMove = false; |
375 | time = 0; |
376 | |
377 | }); |
378 | |
379 | } |
380 | }; |
381 | } |
插件下载地址:百度网盘 欢迎共同探讨!
JS封装移动端触摸滑动插件应用于导航banner【精装版】的更多相关文章
- 移动端触摸滑动插件Swiper
移动端触摸滑动插件Swiper 04/02/2015 一.了解Swiper 目前移动端项目一般都需要具有触屏焦点图的效果,如果你也需要实现这一功能的话,Swiper是一个不错的选择. 1.他不需要加载 ...
- swiper嵌套小demo(移动端触摸滑动插件)
swiper(移动端触摸滑动插件) tip:自己敲得Swiper 的小demo,可以复制粘贴看看效果哦. swiper的js包css包下链接地址 : https://github.com/Clear ...
- Swipe-移动端触摸滑动插件swipe.js
原文链接:http://caibaojian.com/swipe.html 插件特色 viaswipe.JS是一个比较有名的触摸滑动插件,它能够处理内容滑动,支持自定义选项,你可以让它自动滚动,控制滚 ...
- 移动端触摸滑动插件Swiper使用指南
Swiper是一款开源.免费.功能十分强大的移动端内容触摸滑动插件,目前的最新版本是Swiper4.Swiper主要面向的是手机.平板电脑等移动设备,帮助开发者轻松实现触屏焦点图.触屏Tab切换.触屏 ...
- swiper.js 移动端触摸滑动插件
API链接: http://www.swiper.com.cn/api/start/2014/1218/140.html <body> <div class="swiper ...
- Swiper --移动端触摸滑动插件
Swiper使用方法 1.首先加载插件,需要用到的文件有swiper.min.js和swiper.min.css文件. <!DOCTYPE html> <html> <h ...
- 仿移动端触摸滑动插件swiper,的简单实现
/** * @author lyj * @Date 2016-02-04 * @Method 滑动方法 针对一个大容器内部的容器做滑动封装 * @param * args args.swipeDo ...
- 自己用原生JS写的轮播图,支持移动端触摸滑动,分页器圆点可以支持mouseover鼠标移入和click点击,高手看了勿喷哈
自己用原生JavaScript写的轮播图,分页器圆点按钮可支持click点击,也可支持mouseover鼠标悬浮触发,同时支持移动端触摸滑动,有兴趣的友友可以试试哈,菜鸟一枚,高手看了勿喷,请多多指正 ...
- 最好的移动触摸滑动插件:Swiper
最近在使用的一个移动触摸滑动插件Swiper,功能很强大,跟之前的那个swipe.JS相比,同时支持在PC端滑动,支持上下方向滑动,支持多张图片滑动,支持多屏滑动,凡是你想得到的几乎都可以实现.最近作 ...
随机推荐
- 减小Delphi的Exe文件大小(11种方法)
一般来说,由Delphi生成的EXE文件,要比其由它编程语言生成的体积大一些.这主要是由于使用VCL的原因(当然,VCL是有许多优点的!) 以下是减小EXE文件大小的几种途径: 01) 使用加壳工具( ...
- git ssh认证
一般新手用git时,使用HTTPS都需要输入用户名和密码,这是一个很低效的开发过程.(虽然有时可以让开发人员减少push的次数).github提供了几种连接方式,其中以https:开头的代表https ...
- Java学习之InputStream中read()与read(byte[] b)
Java学习之InputStream中read()与read(byte[] b) 这两个方法在抽象类InputStream中都是作为抽象方法存在的, JDK API中是这样描述两者的: read() ...
- hdu 1210_(逻辑训练)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1210 #include<stdio.h> int main() { int n,t,sum ...
- OA项目之打印
打印 若此页有一个打印按钮: <input type="button" id="btnPrint" class="button_sm7" ...
- Android重启应用程序代码
Intent i = getBaseContext().getPackageManager() .getLaunchIntentForPackage(getBaseContext().getPacka ...
- HTML5中video的使用一
<!DOCTYPE html> <html> <head> <title>HTML5 </title> <meta http-equi ...
- JS学习笔记(一)基本数据类型和对象类型
js是一种弱类型的语言,所有的变量都用var进行声明,字符串用双引号或单引号括起来,常见基本数据类型为number,string,boolean等.如 var num = 123;或var num = ...
- [学习笔记]viewport定义,弹性布局,响应式布局
一,移动端宽度设置viewport视图窗口,<meta name="viewport" content="width=device-width,initial-sc ...
- 2014.8.30.ref,out,params,enum,递归
(一)ref 函数形参变量的输入有两种方式:传值,传址.而ref则为传址.eg: static int Add(ref int n) { Console.WriteLine("Add---- ...