JS 移动端触屏滑动
發(fā)布時(shí)間:2025/4/16
33
豆豆
生活随笔
收集整理的這篇文章主要介紹了
JS 移动端触屏滑动
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
首頁> 程序開發(fā) > web前端 > JavaScript > 正文
JS案例之5——移動(dòng)端觸屏滑動(dòng)
2014-08-15 ????0個(gè)評(píng)論?? ?? 收藏??我要投稿
移動(dòng)端觸屏滑動(dòng)的效果其實(shí)就是圖片輪播,在PC的頁面上很好實(shí)現(xiàn),綁定click和mouseover等事件來完成。但是在移動(dòng)設(shè)備上,要實(shí)現(xiàn)這種輪播的效果,就需要用到核心的touch事件。處理touch事件能跟蹤到屏幕滑動(dòng)的每根手指。 以下是四種touch事件 touchstart: ? ? //手指放到屏幕上時(shí)觸發(fā) touchmove: ? ? ?//手指在屏幕上滑動(dòng)式觸發(fā) touchend: ? ?//手指離開屏幕時(shí)觸發(fā) touchcancel: ? ? //系統(tǒng)取消touch事件的時(shí)候觸發(fā),這個(gè)好像比較少用 每個(gè)觸摸事件被觸發(fā)后,會(huì)生成一個(gè)event對(duì)象,event對(duì)象里額外包括以下三個(gè)觸摸列表 touches: ? ? //當(dāng)前屏幕上所有手指的列表 targetTouches: ? ? ?//當(dāng)前dom元素上手指的列表,盡量使用這個(gè)代替touches changedTouches: ? ? //涉及當(dāng)前事件的手指的列表,盡量使用這個(gè)代替touches 這些列表里的每次觸摸由touch對(duì)象組成,touch對(duì)象里包含著觸摸信息,主要屬性如下: clientX / clientY: ? ? ?//觸摸點(diǎn)相對(duì)瀏覽器窗口的位置 pageX / pageY: ? ? ? //觸摸點(diǎn)相對(duì)于頁面的位置 screenX ?/ ?screenY: ? ?//觸摸點(diǎn)相對(duì)于屏幕的位置 identifier: ? ? ? ?//touch對(duì)象的ID target: ? ? ? //當(dāng)前的DOM元素 注意: 手指在滑動(dòng)整個(gè)屏幕時(shí),會(huì)影響瀏覽器的行為,比如滾動(dòng)和縮放。所以在調(diào)用touch事件時(shí),要注意禁止縮放和滾動(dòng)。 1.禁止縮放 通過meta元標(biāo)簽來設(shè)置。 <meta name="viewport" content="target-densitydpi=320,width=640,user-scalable=no"> 2.禁止?jié)L動(dòng) preventDefault是阻止默認(rèn)行為,touch事件的默認(rèn)行為就是滾動(dòng)。 event.preventDefault(); 案例: 下面給出一個(gè)案例,需在移動(dòng)設(shè)備上才能看出效果。 1.定義touchstart的事件處理函數(shù),并綁定事件: if(!!self.touch) self.slider.addEventListener('touchstart',self.events,false);? //定義touchstart的事件處理函數(shù) start:function(event){ event.preventDefault(); //阻止觸摸事件的默認(rèn)動(dòng)作,即阻止?jié)L屏 var touch = event.touches[0]; //touches數(shù)組對(duì)象獲得屏幕上所有的touch,取第一個(gè)touch startPos = {x:touch.pageX,y:touch.pageY,time:+new Date}; //取第一個(gè)touch的坐標(biāo)值 //綁定事件 this.slider.addEventListener('touchmove',this,false); this.slider.addEventListener('touchend',this,false); }, 觸發(fā)touchstart事件后,會(huì)產(chǎn)生一個(gè)event對(duì)象,event對(duì)象里包括觸摸列表,獲得屏幕上的第一個(gè)touch,并記下其pageX,pageY的坐標(biāo)。此時(shí)綁定touchmove,touchend事件。 2.定義手指在屏幕上移動(dòng)的事件,定義touchmove函數(shù)。 //移動(dòng) move:function(event){ event.preventDefault(); //阻止觸摸事件的默認(rèn)行為,即阻止?jié)L屏 //當(dāng)屏幕有多個(gè)touch或者頁面被縮放過,就不執(zhí)行move操作 if(event.touches.length > 1 || event.scale && event.scale !== 1) return; var touch = event.touches[0]; endPos = {x:touch.pageX - startPos.x,y:touch.pageY - startPos.y}; //執(zhí)行操作,使元素移動(dòng) this.slider.className = 'cnt'; this.slider.style.left = -this.index*600 + endPos.x + 'px'; }, 同樣首先阻止頁面的滾屏行為,touchmove觸發(fā)后,會(huì)生成一個(gè)event對(duì)象,在event對(duì)象中獲取touches觸屏列表,取得第一個(gè)touch,并記下pageX,pageY的坐標(biāo),算出差值,得出手指滑動(dòng)的偏移量,使當(dāng)前DOM元素滑動(dòng)。 3.定義手指從屏幕上拿起的事件,定義touchend函數(shù)。 //滑動(dòng)釋放 end:function(event){ var duration = +new Date - startPos.time; //滑動(dòng)的持續(xù)時(shí)間 this.icon[this.index].className = ''; if(Number(duration) > 100){? //判斷是左移還是右移,當(dāng)偏移量大于50時(shí)執(zhí)行 if(endPos.x > 50){ if(this.index !== 0) this.index -= 1; }else if(endPos.x < -50){ if(this.index !== 4) this.index += 1; } } this.slider.className = 'cnt f-anim'; this.slider.style.left = -this.index*600 + 'px'; this.icon[this.index].className = 'curr'; //解綁事件 this.slider.removeEventListener('touchmove',this,false); this.slider.removeEventListener('touchend',this,false); }, 手指離開屏幕后,所執(zhí)行的函數(shù)。這里先判斷手指停留屏幕上的時(shí)間,如果時(shí)間太短,則不執(zhí)行該函數(shù)。再判斷手指是左滑動(dòng)還是右滑動(dòng),分別執(zhí)行不同的操作。最后很重要的一點(diǎn)是移除touchmove,touchend綁定事件。 源代碼: 復(fù)制代碼 1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta http-equiv="Content-Type" Content="text/html; charset=utf-8;"> 5 <title>移動(dòng)端觸摸滑動(dòng)</title> 6 <meta name="author" content="rainna" /> 7 <meta name="keywords" content="rainna's js lib" /> 8 <meta name="description" content="移動(dòng)端觸摸滑動(dòng)" /> 9 <meta name="viewport" content="target-densitydpi=320,width=640,user-scalable=no"> 10 <style> 11 *{margin:0;padding:0;} 12 li{list-style:none;} 13? 14 .m-slider{width:600px;margin:50px 20px;overflow:hidden;} 15 .m-slider .cnt{position:relative;left:0;width:3000px;} 16 .m-slider .cnt li{float:left;width:600px;} 17 .m-slider .cnt img{display:block;width:100%;height:450px;} 18 .m-slider .cnt p{margin:20px 0;} 19 .m-slider .icons{text-align:center;color:#000;} 20 .m-slider .icons span{margin:0 5px;} 21 .m-slider .icons .curr{color:red;} 22 .f-anim{-webkit-transition:left .2s linear;} 23 </style> 24 </head> 25? 26 <body> 27 <div class="m-slider"> 28 ? ? <ul class="cnt" id="slider"> 29 ? ? ? ? <li> 30 ? ? ? ? ? ? <img src=http://www.2cto.com/uploadfile/2014/0815/20140815091259973.jpg"> 31 ? ? ? ? ? ? <p>20140813鏡面的世界,終究只是倒影。看得到你的身影,卻觸摸不到你的未來</p> 32 ? ? ? ? </li> 33 ? ? ? ? <li> 34 ? ? ? ? ? ? <img src=http://www.2cto.com/uploadfile/2014/0815/20140815091259295.jpg"> 35 ? ? ? ? ? ? <p>20140812錫林浩特前往東烏旗S101必經(jīng)之處,一條極美的鐵路。鐵路下面是個(gè)小型的鹽沼,淡淡的有了一絲天空之境的感覺。可惜在此玩了一個(gè)小時(shí)也沒有看見一列火車經(jīng)過,只好繼續(xù)趕往東烏旗。</p> 36 ? ? ? ? </li> 37 ? ? ? ? <li> 38 ? ? ? ? ? ? <img src=http://www.2cto.com/uploadfile/2014/0815/20140815091259357.jpg"> 39 ? ? ? ? ? ? <p>20140811水的顏色為什么那么藍(lán),我也納悶,反正自然飽和度和對(duì)比度拉完就是這個(gè)顏色的</p> 40 ? ? ? ? </li> 41 ? ? ? ? <li> 42 ? ? ? ? ? ? <img src=http://www.2cto.com/uploadfile/2014/0815/20140815091259687.jpg"> 43 ? ? ? ? ? ? <p>海洋星球3重慶天氣熱得我想臥軌自殺</p> 44 ? ? ? ? </li> 45 ? ? ? ? <li> 46 ? ? ? ? ? ? <img src=http://www.2cto.com/uploadfile/2014/0815/20140815091259295.jpg"> 47 ? ? ? ? ? ? <p>以上這些作品分別來自兩位設(shè)計(jì)師作為觀者,您能否通過設(shè)計(jì)風(fēng)格進(jìn)行區(qū)分</p> 48 ? ? ? ? </li> 49 ? ? </ul> 50 ? ? <div class="icons" id="icons"> 51 ? ? ? ? <span class="curr">1</span> 52 ? ? ? ? <span>2</span> 53 ? ? ? ? <span>3</span> 54 ? ? ? ? <span>4</span> 55 ? ? ? ? <span>5</span> 56 ? ? </div> 57 </div> 58? 59 <script> 60 var slider = { 61 ? ? //判斷設(shè)備是否支持touch事件 62 ? ? touch:('ontouchstart' in window) || window.DocumentTouch && document instanceof DocumentTouch, 63 ? ? slider:document.getElementById('slider'), 64? 65 ? ? //事件 66 ? ? events:{ 67 ? ? ? ? index:0, ? ? //顯示元素的索引 68 ? ? ? ? slider:this.slider, ? ? //this為slider對(duì)象 69 ? ? ? ? icons:document.getElementById('icons'), 70 ? ? ? ? icon:this.icons.getElementsByTagName('span'), 71 ? ? ? ? handleEvent:function(event){ 72 ? ? ? ? ? ? var self = this; ? ? //this指events對(duì)象 73 ? ? ? ? ? ? if(event.type == 'touchstart'){ 74 ? ? ? ? ? ? ? ? self.start(event); 75 ? ? ? ? ? ? }else if(event.type == 'touchmove'){ 76 ? ? ? ? ? ? ? ? self.move(event); 77 ? ? ? ? ? ? }else if(event.type == 'touchend'){ 78 ? ? ? ? ? ? ? ? self.end(event); 79 ? ? ? ? ? ? } 80 ? ? ? ? }, 81 ? ? ? ? //滑動(dòng)開始 82 ? ? ? ? start:function(event){ 83 ? ? ? ? ? ? event.preventDefault(); ? ?//阻止觸摸事件的默認(rèn)動(dòng)作,即阻止?jié)L屏 84 ? ? ? ? ? ? var touch = event.touches[0]; ? ? //touches數(shù)組對(duì)象獲得屏幕上所有的touch,取第一個(gè)touch 85 ? ? ? ? ? ? startPos = {x:touch.pageX,y:touch.pageY,time:+new Date}; ? ?//取第一個(gè)touch的坐標(biāo)值 86 ? ? ? ? ? ? //綁定事件 87 ? ? ? ? ? ? this.slider.addEventListener('touchmove',this,false); 88 ? ? ? ? ? ? this.slider.addEventListener('touchend',this,false); 89 ? ? ? ? }, 90 ? ? ? ? //移動(dòng) 91 ? ? ? ? move:function(event){ 92 ? ? ? ? ? ? event.preventDefault(); ? ? ?//阻止觸摸事件的默認(rèn)行為,即阻止?jié)L屏 93 ? ? ? ? ? ? //當(dāng)屏幕有多個(gè)touch或者頁面被縮放過,就不執(zhí)行move操作 94 ? ? ? ? ? ? if(event.touches.length > 1 || event.scale && event.scale !== 1) return; 95 ? ? ? ? ? ? var touch = event.touches[0]; 96 ? ? ? ? ? ? endPos = {x:touch.pageX - startPos.x,y:touch.pageY - startPos.y}; 97 ? ? ? ? ? ? //執(zhí)行操作,使元素移動(dòng) 98 ? ? ? ? ? ? this.slider.className = 'cnt'; 99 ? ? ? ? ? ? this.slider.style.left = -this.index*600 + endPos.x + 'px'; 100 ? ? ? ? }, 101 ? ? ? ? //滑動(dòng)釋放 102 ? ? ? ? end:function(event){ 103 ? ? ? ? ? ? var duration = +new Date - startPos.time; ? ?//滑動(dòng)的持續(xù)時(shí)間 104 ? ? ? ? ? ? this.icon[this.index].className = ''; 105 ? ? ? ? ? ? if(Number(duration) > 100){ ? ?? 106 ? ? ? ? ? ? ? ? //判斷是左移還是右移,當(dāng)偏移量大于50時(shí)執(zhí)行 107 ? ? ? ? ? ? ? ? if(endPos.x > 50){ 108 ? ? ? ? ? ? ? ? ? ? if(this.index !== 0) this.index -= 1; 109 ? ? ? ? ? ? ? ? }else if(endPos.x < -50){ 110 ? ? ? ? ? ? ? ? ? ? if(this.index !== 4) this.index += 1; 111 ? ? ? ? ? ? ? ? } 112 ? ? ? ? ? ? } 113 ? ? ? ? ? ? this.slider.className = 'cnt f-anim'; 114 ? ? ? ? ? ? this.slider.style.left = -this.index*600 + 'px'; 115 ? ? ? ? ? ? this.icon[this.index].className = 'curr'; 116 ? ? ? ? ? ? //解綁事件 117 ? ? ? ? ? ? this.slider.removeEventListener('touchmove',this,false); 118 ? ? ? ? ? ? this.slider.removeEventListener('touchend',this,false); 119 ? ? ? ? } 120 ? ? }, 121 ? ?? 122 ? ? //初始化 123 ? ? init:function(){ 124 ? ? ? ? var self = this; ? ? //this指slider對(duì)象 125 ? ? ? ? if(!!self.touch) self.slider.addEventListener('touchstart',self.events,false); ? ?//addEventListener第二個(gè)參數(shù)可以傳一個(gè)對(duì)象,會(huì)調(diào)用該對(duì)象的handleEvent屬性 126 ? ? } 127 }; 128? 129 slider.init(); 130 </script> 131 </body> 132 </html>
總結(jié)
以上是生活随笔為你收集整理的JS 移动端触屏滑动的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。
歡迎分享!
轉(zhuǎn)載請(qǐng)說明來源于"生活随笔",并保留原作者的名字。
本文地址:JS 移动端触屏滑动