javascript實現移動端上的觸屏拖拽功能,javascript拖拽

來源:互聯網
上載者:User

javascript實現移動端上的觸屏拖拽功能,javascript拖拽

本文是分享了javascript實現移動端上的觸屏拖拽功能,具體內容如下

實現代碼:

<!DOCTYPE html><html> <head>  <meta charset="utf-8">  <title></title>  <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0" />  <style>   body {    height: 2000px;   }   #block {    width:200px;    height:200px;    background-color: red;    position: absolute;    left: 0;    top: 0;   }  </style> </head> <body>  <div>   touchstart,touchmove,   touchend,touchcancel  </div>  <div id="block"></div><script>  // 擷取節點  var block = document.getElementById("block");  var oW,oH;  // 綁定touchstart事件  block.addEventListener("touchstart", function(e) {   console.log(e);   var touches = e.touches[0];   oW = touches.clientX - block.offsetLeft;   oH = touches.clientY - block.offsetTop;   //阻止頁面的滑動預設事件   document.addEventListener("touchmove",defaultEvent,false);  },false)  block.addEventListener("touchmove", function(e) {   var touches = e.touches[0];   var oLeft = touches.clientX - oW;   var oTop = touches.clientY - oH;   if(oLeft < 0) {    oLeft = 0;   }else if(oLeft > document.documentElement.clientWidth - block.offsetWidth) {    oLeft = (document.documentElement.clientWidth - block.offsetWidth);   }   block.style.left = oLeft + "px";   block.style.top = oTop + "px";  },false);    block.addEventListener("touchend",function() {   document.removeEventListener("touchmove",defaultEvent,false);  },false);  function defaultEvent(e) {   e.preventDefault();  }</script> </body></html>

以上就是本文的全部內容,希望對大家學習javascript程式設計有所協助。

您可能感興趣的文章:
  • js 表格排序(編輯+拖拽+縮放)
  • js最簡單的拖拽效果實現代碼
  • javascript拖拽上傳類庫DropzoneJS使用方法
  • js完美的div拖拽執行個體代碼
  • 觸屏中的JavaScript事件分析
  • js實現touch移動觸屏滑動事件
  • js實現滑動觸屏事件監聽的方法
  • JS+CSS實現仿觸屏手機撥號盤介面及功能類比完整執行個體
  • 基於JavaScript實現移動端TAB觸屏轉場效果

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.