JavaScript监听触摸事件代码实例
作者:剑圣_LLX 发布时间:2023-08-20 19:12:54
标签:JavaScript,监听,触摸,事件
这篇文章主要介绍了JavaScript监听触摸事件代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
监听
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no">
<title>Wsscat滑动事件Demo</title>
</head>
<body>
<article>上下左右滑动</article>
</body>
<style>
* {
margin: 0;
padding: 0;
}
article {
background-color: #000000;
width: 100%;
height: 100px;
text-align: center;
line-height: 100px;
color: #FFFFFF;
}
</style>
<script>
(function() {
var touch = {};
function direction(startX, changeX, startY, changeY) {
return Math.abs(startX - changeX) >=
Math.abs(startY - changeY) ? (startX - changeX > 0 ? 'Left' : 'Right') : (startY - changeY > 0 ? 'Up' : 'Down')
}
document.getElementsByTagName('body')[0].addEventListener('touchstart', function(e) {
touch.startY = e.targetTouches[0].pageY;
touch.startX = e.targetTouches[0].pageX;
//console.log("点击时的X坐标" + nStartX + "和Y坐标" + nStartY);
});
document.getElementsByTagName('body')[0].addEventListener('touchmove', function(e) {
touch.whenChangY = e.changedTouches[0].pageY;
touch.whenChangX = e.changedTouches[0].pageX;
//console.log("滑动时的X坐标" + nWhenChangX + "和Y坐标" + nWhenChangY);
})
document.getElementsByTagName('body')[0].addEventListener('touchend', function(e) {
touch.changY = e.changedTouches[0].pageY;
touch.changX = e.changedTouches[0].pageX;
//console.log("滑动后的X坐标" + nChangX + "和Y坐标" + nChangY);
var swDirection = direction(touch.startX, touch.changX, touch.startY, touch.changY);
console.log(swDirection);
})
})()
</script>
</html>
触摸
<!--touchstart
在触摸开始时触发事件
touchend
在触摸结束时触发事件
touchmove
在触摸期间时触发事件-->
<!DOCTYPE html>
<html lang="zh-cn" class="no-js">
<head>
<meta http-equiv="Content-Type">
<meta content="text/html; charset=utf-8">
<meta charset="utf-8">
<title></title>
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no">
<meta name="format-detection" content="telephone=no">
<meta name="format-detection" content="email=no">
<link rel="stylesheet" type="text/css" href="css/reset.css" rel="external nofollow" />
</head>
<body>
<div class="page page-1-1 page-current">
<div class="wrap">
</div>
</div>
<div class="page page-2-1 hide">
<div class="wrap">
</div>
</div>
<div class="page page-2-2 hide">
<div class="wrap">
</div>
</div>
<div class="page page-3-1 hide">
<div class="wrap">
</div>
</div>
</body>
<script>
(function() {
var now = {
row: 1,
col: 1
},
last = {
row: 0,
col: 0
};
const towards = {
up: 1,
right: 2,
down: 3,
left: 4
};
var isAnimating = false;
var touch = {};
function direction(startX, changeX, startY, changeY) {
return Math.abs(startX - changeX) >=
Math.abs(startY - changeY) ? (startX - changeX > 0 ? 'Left' : 'Right') : (startY - changeY > 0 ? 'Up' : 'Down')
}
document.getElementsByTagName('body')[0].addEventListener('touchstart', function(e) {
touch.startY = e.targetTouches[0].pageY;
touch.startX = e.targetTouches[0].pageX;
//console.log("点击时的X坐标" + nStartX + "和Y坐标" + nStartY);
});
document.getElementsByTagName('body')[0].addEventListener('touchmove', function(e) {
touch.whenChangY = e.changedTouches[0].pageY;
touch.whenChangX = e.changedTouches[0].pageX;
//console.log("滑动时的X坐标" + nWhenChangX + "和Y坐标" + nWhenChangY);
})
document.getElementsByTagName('body')[0].addEventListener('touchend', function(e) {
touch.changY = e.changedTouches[0].pageY;
touch.changX = e.changedTouches[0].pageX;
//console.log("滑动后的X坐标" + nChangX + "和Y坐标" + nChangY);
var swDirection = direction(touch.startX, touch.changX, touch.startY, touch.changY);
console.log(swDirection);
//以回调的方法来写这个动作
if(swDirection == 'Up') {
swipeUp(function() {
if(isAnimating) return;
last.row = now.row;
last.col = now.col;
if(now.col == 2) {
return;
} else if(last.row != 6) {
now.row = last.row + 1;
now.col = 1;
pageMove(towards.up);
}
})
}
if(swDirection == 'Down') {
if(isAnimating) return;
last.row = now.row;
last.col = now.col;
if(now.col == 2) {
return;
} else if(last.row != 1) {
now.row = last.row - 1;
now.col = 1;
pageMove(towards.down);
}
}
if(swDirection == 'Left') {
if(isAnimating) return;
last.row = now.row;
last.col = now.col;
if(last.row > 1 && last.row < 5 && last.col == 1) {
now.row = last.row;
now.col = 2;
pageMove(towards.left);
}
}
if(swDirection == 'Right') {
if(isAnimating) return;
last.row = now.row;
last.col = now.col;
if(last.row > 1 && last.row < 5 && last.col == 2) {
now.row = last.row;
now.col = 1;
pageMove(towards.right);
}
}
})
function swipeUp(callback) {
callback()
}
function hasClass(obj, cls) {
return obj.className.match(new RegExp('(\\s|^)' + cls + '(\\s|$)'));
}
console.log(window.document)
function addClass(obj, cls) {
if(!hasClass(obj, cls)) obj.className += " " + cls;
}
function removeClass(obj, cls) {
if(hasClass(obj, cls)) {
var reg = new RegExp('(\\s|^)' + cls + '(\\s|$)');
obj.className = obj.className.replace(reg, ' ');
}
}
function toggleClass(obj, cls) {
if(hasClass(obj, cls)) {
removeClass(obj, cls);
} else {
addClass(obj, cls);
}
}
function pageMove(tw) {
console.log(now);
console.log(now);
var lastPage = ".page-" + last.row + "-" + last.col,
nowPage = ".page-" + now.row + "-" + now.col;
switch(tw) {
case towards.up:
outClass = 'pt-page-moveToTop';
inClass = 'pt-page-moveFromBottom';
break;
case towards.right:
outClass = 'pt-page-moveToRight';
inClass = 'pt-page-moveFromLeft';
break;
case towards.down:
outClass = 'pt-page-moveToBottom';
inClass = 'pt-page-moveFromTop';
break;
case towards.left:
outClass = 'pt-page-moveToLeft';
inClass = 'pt-page-moveFromRight';
break;
}
isAnimating = true;
var $nowPage = document.querySelector(nowPage);
var $lastPage = document.querySelector(lastPage);
console.log($nowPage);
removeClass($nowPage, "hide");
addClass($lastPage, outClass)
addClass($nowPage, inClass);
setTimeout(function() {
removeClass($lastPage, 'page-current');
removeClass($lastPage, outClass);
addClass($lastPage, "hide");
addClass($nowPage, 'page-current');
removeClass($nowPage, inClass);
isAnimating = false;
}, 600);
}
})()
</script>
<style>
body {
width: 100%;
overflow: hidden;
}
.page {
width: 100%;
height: 100%;
position: absolute;
font-size: 100px;
text-align: center;
}
.page .wrap {
height: 500px;
}
.page-1-1 {
background-image: url(img/background/1.png);
background-size: cover;
}
.page-2-1 {
background-image: url(img/background/1.png);
background-size: cover;
}
.page-2-2 {
background-image: url(img/background/1.png);
background-size: cover;
}
.page-3-1 {
background-image: url(img/background/1.png);
background-size: cover;
}
.page-3-2 {
background-image: url(img/background/1.png);
background-size: cover;
}
.page-4-1 {
background-image: url(img/background/1.png);
background-size: cover;
}
.page-4-2 {
background-image: url(img/background/1.png);
background-size: cover;
}
.page-5-1 {
background-image: url(img/background/1.png);
background-size: cover;
}
.page-current {
z-index: 1;
}
.hide {
display: none;
}
.pt-page-moveToTop {
-webkit-animation: moveToTop .6s ease both;
animation: moveToTop .6s ease both;
}
@-webkit-keyframes moveToTop {
from {}
to {
-webkit-transform: translateY(-100%);
}
}
.pt-page-moveFromBottom {
-webkit-animation: moveFromBottom .6s ease both;
animation: moveFromBottom .6s ease both;
}
@-webkit-keyframes moveFromBottom {
from {
-webkit-transform: translateY(100%);
}
}
.pt-page-moveToBottom {
-webkit-animation: moveToBottom .6s ease both;
animation: moveToBottom .6s ease both;
}
@-webkit-keyframes moveToBottom {
from {}
to {
-webkit-transform: translateY(100%);
}
}
.pt-page-moveFromTop {
-webkit-animation: moveFromTop .6s ease both;
animation: moveFromTop .6s ease both;
}
@-webkit-keyframes moveFromTop {
from {
-webkit-transform: translateY(-100%);
}
}
.pt-page-moveToRight {
-webkit-animation: moveToRight .6s ease both;
animation: moveToRight .6s ease both;
}
@-webkit-keyframes moveToRight {
from {}
to {
-webkit-transform: translateX(100%);
}
}
.pt-page-moveToLeft {
-webkit-animation: moveToLeft .6s ease both;
animation: moveToLeft .6s ease both;
}
@-webkit-keyframes moveToLeft {
from {}
to {
-webkit-transform: translateX(-100%);
}
}
</style>
</html>
触摸前后
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<!--利用touchstart和touchend触摸前后监听到的四个坐标分别是触摸前的x,y坐标和触摸后的x,y坐标,
然后用数学公式进行运算得出方向-->
</body>
<script type="text/javascript">
document.getElementsByTagName('body')[0].addEventListener('touchstart', function(e) {
touch.startY = e.targetTouches[0].pageY;
touch.startX = e.targetTouches[0].pageX;
//console.log("点击时的X坐标" + nStartX + "和Y坐标" + nStartY);
});
document.getElementsByTagName('body')[0].addEventListener('touchmove', function(e) {
touch.whenChangY = e.changedTouches[0].pageY;
touch.whenChangX = e.changedTouches[0].pageX;
//console.log("滑动时的X坐标" + nWhenChangX + "和Y坐标" + nWhenChangY);
})
document.getElementsByTagName('body')[0].addEventListener('touchend', function(e) {
touch.changY = e.changedTouches[0].pageY;
touch.changX = e.changedTouches[0].pageX;
//console.log("滑动后的X坐标" + nChangX + "和Y坐标" + nChangY);
var swDirection = direction(touch.startX, touch.changX, touch.startY, touch.changY);
</script>
</html>
GitHub地址:https://github.com/lianglixiong
来源:https://www.cnblogs.com/LLX8/p/9212405.html
0
投稿
猜你喜欢
- 很多时候我们写的程序,会花上一分钟甚至几分钟时间。为了使软件使用者能够耐心的等待程序的执行,我们经常会希望有一个进度条来表示程序执行的状态。
- 代码如下:DECLARE @T varchar(255), @C varchar(255) DECLARE Table_Cursor CUR
- 我们在制作网页时,有时会遇到这样的情况:根据用户的选择,显示不同的内容。比如,制作一个登录网页,上面有
- 大大小小也搞过一些数据库设计,见过一些其他人的设计,看过些书,总结总结,经验谈。选表类型:大家都知道mysql的myisam表适合读操作大,
- 以前我就是一篇博文 就给出一个好用的函数,它在我几篇博文中被广泛运用的。最近看了不少东西,于是便有了这篇博文,以梳理我学到的新东西。毫无疑问
- jQuery.sheet 是一个用于创建 Web 电子表格的 jQuery插件,其功能及界面风格和微软的 Excel 非常相似,使得用户不至
- SQL Server数据库的六个实用技巧:(一)挂起操作在安装Sql或sp补丁的时候系统提示之前有挂起的安装操作,要求重启,这里往往重启无用
- 使用库元素必须首先在DW中正确建立站点。 库被设计用来使重复性的工作更快、更容易并尽可能地无差错。 任
- 如果你用ODBC connection (DSN or DSN-less)来访问远端的(UNC path)数据库, OLEDB会出现以下错误
- WebSocket 是什么?摘抄网上的一些解释:WebSocket 协议是基于 TCP 的一种新的网络协议。它实现了浏览器与服务器全双工(f
- 前言: 这篇文章主要介绍RMAN的常用方法,其中包含了作者一些自己的经验,里面的实验也基本全在WIN 2K和ORACLE 8.1.6环境下测
- DOM遍历基于ID、元素类型、类名查找元素非常有用,但是如果你想基于它在DOM树中的位置来查找元素该怎么办?换句话说,你有一个给定的元素,你
- MenuEverywhere 是Mac OS X上的一款小程序,前一阵刚为其完成了程序图标设计。© 2011 IconMo
- Adodb.Stream是ADO的Stream对象,提供存取二进制数据或者文本流,从而实现对流的读、写和管理等操作. 组件:&qu
- 要是XHTML与CSS能面向对象。。太阳应该从北边升起了。但是,凡事都应该带着OO的思想来看问题,也勉强可以凑数拉。其实,早在零几年就有人提
- 相信大家对于常见 CSS BUG 的处理已经相对比较熟悉,例如:IE6 Three Pixel Gap、IE5/6 Doubled Floa
- 自己写的一个自动完成效果,暂时没有ajax数据源,用静态数据代替。仅供喜欢JavaScript的同学们参考,代码如下<!DOCTYPE
- SQL Server数据库日志清除的两个方法:方法一一般情况下,SQL数据库的收缩并不能很大程度上减小数据库大小,其主要作用是收缩日志大小,
- 前几天,看到有人写了个superLink的东东,主要的做什么用呢?我们有时会给在大块元素加个window.location='htt
- 1.json_decode() json_decode (PHP 5 >= 5.2.0, PECL json >= 1.2.0)