单指右滑动事件,实现左右换页(View.aspx)
var curpageX =-1; var curpageXmove =-1; function loadtouch (){ //单指拖动 //var obj = document.getElementById('newspapercontent'); var obj=document.querySelector("#newspapercontent"); obj.addEventListener("touchstart", function(event) { var touch = event.targetTouches[0]; //var left = parseInt(obj.style.left); // alert(left); //var x = touch.pageX - left; curpageX = touch.pageX; //var y = touch.pageY - left; if (event.targetTouches.length != 1) { curpageX=-1 return; } if (curpageX>50) { curpageX=-1 return; } //console.log("curpageX"); // event.preventDefault(); 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'; curpageXmove = touch.pageX; // console.log(curpageXmove); } else { curpageX=-1 return; } }, false); }); obj.addEventListener("touchend",function(event){ if(curpageX==-1) return; if (curpageX>0 && (curpageXmove-curpageX)>5) { //console.log("touchend"); window.location.href=returnurl; } //event.preventDefault(); //obj.removeEventListener("touchstart"); //obj.removeEventListener("touchmove"); }); } //if (!(navigator.userAgent.indexOf("iPhone")>-1) && navigator.userAgent.indexOf("MicroMessenger")>-1){ if (!(navigator.userAgent.indexOf("iPhone")>-1) ){ //touch window.addEventListener('load',loadtouch, false); }
相关文章
单指滑动