移動端 slide拖拽

來源:互聯網
上載者:User

標籤:

<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,height=device-height,initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
<style type="text/Css">
body{background-color:#000000;}
.window{position:absolute;z-index:1;overflow:hidden;width:100%;height:400px;background-color:red;left: 0px;}
.dragme{position:relative;background-image:url(‘img/testbg.png‘);width:100%;height:400px;}
</style>

<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<script type="text/javascript">

var isdrag=false;
var tx,x;

$(function(){
document.getElementById("tap").addEventListener(‘touchend‘,function(){
isdrag = false;
});
document.getElementById("tap").addEventListener(‘touchstart‘,selectmouse);
document.getElementById("tap").addEventListener(‘touchmove‘,movemouse);
});

function movemouse(e){
if (isdrag){
var n = tx + e.touches[0].pageX - x;
$("#tap").css("left",n);
return false;
}
}

function selectmouse(e){
isdrag = true;
tx = parseInt(document.getElementById("tap").style.left+0);
x = e.touches[0].pageX;
return false;
}

// var isdrag=false;
// var tx,x,ty,y;
// $(function(){
// document.getElementById("tap").addEventListener(‘touchend‘,function(){
// isdrag = false;
// });
// document.getElementById("tap").addEventListener(‘touchstart‘,selectmouse);
// document.getElementById("tap").addEventListener(‘touchmove‘,movemouse);
// });
// function movemouse(e){
// if (isdrag){
// var n = tx + e.touches[0].pageX - x;
// m = ty + e.touches[0].pageY - y;
// $("#tap").css({"left":n,"top":m});
// return false;
// }
// }
// function selectmouse(e){
// isdrag = true;
// tx = parseInt(document.getElementById("tap").style.left+0);
// ty = parseInt(document.getElementById("tap").style.top+0);
// x = e.touches[0].pageX;
// y = e.touches[0].pageY;
// return false;
// }
</script>

</head>
<body>
<div align="left" class="window">
<div id="tap" class="dragme">
這是一個可以通過觸控螢幕拖動的demo<br>
這個demo花費了我半天時間,原因是以前從來沒有做過面向觸控螢幕的Web,按說mousedown,mouseup,mousemove和touchstart,touchend,touchmove
之間是可以互連的,也就是說一般面向pc開發的mouse時間對touch事件有效,據說是效率有差異。但是pc上測試沒有任何問題,在手機上就是無效。
然後……
然後百度了很久很久……

</div>
</div>
</html>

 

<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,height=device-height,initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
<style type="text/Css">body {height: 2000px;}#block {width:200px;height:200px;background-color: red;position: absolute;left: 0;top: 0;}</style>

<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<script type="text/javascript">

window.onload=function(){

 

// 擷取節點
  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>

</head>
<body>
<div>
   touchstart,touchmove,
   touchend,touchcancel
  </div>
  <div id="block"></div>
</html>

 

移動端 slide拖拽

聯繫我們

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