移動端js手指滑動事件初體驗,js手指滑動初體驗

來源:互聯網
上載者:User

移動端js手指滑動事件初體驗,js手指滑動初體驗

今天在公司遇到做一個移動端手指滑動的效果,剛開始用了swiper.js外掛程式發現效果不好(文字存在模糊效果)。後來查了一些資料,自己手寫了一個使用原生js寫的滑動效果。

下面直接上代碼:

 <!doctype html> <html lang="en"> <head> <meta charset="UTF-8">     <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no, minimal-ui" />    <meta name="apple-mobile-web-app-capable" content="yes" />    <meta name="apple-mobile-web-app-status-bar-style" content="black" />    <meta name="format-detection"content="telephone=no, email=no" /> <title>Document</title> <style> #id { width: 1150px; height: 150px; background: red; position: absolute; left: 0px; } .id { width: 40px; height: 150px; background: green; border: solid 1px grey; float: left;  } </style> </head> <body> <div id="inp"></div> <div id="id" style="left:0px;"> <div class="id">1</div><div class="id">2</div><div class="id"></div><div class="id"></div><div class="id"></div><div class="id"></div> </div> </body> <script> function load (){ /*單指拖動*/var obj = document.getElementById('id');obj.addEventListener("touchstart", function(event) {var touch = event.targetTouches[0];var left = parseInt(obj.style.left);// alert(left);var x = touch.pageX - left;var y = touch.pageY - left;obj.addEventListener('touchmove', function(event) {// 如果這個元素的位置內只有一個手指的話if (event.targetTouches.length == 1) {    event.preventDefault(); // 阻止瀏覽器預設事件,重要 var touch = event.targetTouches[0];// 把元素放在手指所在的位置obj.style.left = touch.pageX - x + 'px';// obj.style.top = touch.pageY + 'px';}}, false);});obj.addEventListener("touchend",function(){obj.removeEventListener("touchstart");obj.removeEventListener("touchmove");});   }window.addEventListener('load',load, false); </script> </html>

還有一個關於

【html5構建觸屏網站】之touch事件的連結地址,有興趣的大家可以去看一下:http://www.cnblogs.com/shawn-xie/archive/2012/12/07/2805582.html

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.