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


猜你喜欢
- 推荐阅读:go语言最新版激活教程可以点下这个链接查看。goland永久安装教程,点击此处查看。Go 这几年很火,小哈也蹭业余时间悄 * 学习一
- 如何在php中判断一个网页请求是ajax请求还是普通请求?你可以通过传递参数的方法来实现,例如使用如下网址请求:/path/to/pkphp
- 本人第一次使用vue awesome。踩到的坑确实不少。官网上面的用法写的很简单,按照上面做法基本会遇到如下这个问题轮播第二次之后,首屏会自
- 最近公司准备扩张海外业务,所以要给 Django 系统添加 国际化与本土化 支持。国际化一般简称 i18n ,代表 Internationa
- python random库简单使用demo当我们需要生成随机数或者从一个序列中随机选择元素时,可以使用 Python 内置的 random
- MySQL支持单向、异步复制,复制过程中一个服务器充当主服务器,而一个或多个其它服务器充当从服务器。主服务器将更新写入二进制日志文件,并维
- 一、写在开头哈喽兄弟们之前经常编写Python脚本来进行数据处理、数据传输和模型训练。随着数据量和数据复杂性的增加,运行脚本可能需要一些时间
- Linux中进程的通信方式有信号,管道,共享内存,消息队列socket等。其中管道是*nix系统进程间通信的最古老形式,所有*nix都提供这
- 本文实例讲述了Python迭代器与生成器用法。分享给大家供大家参考,具体如下:迭代器,迭代的工具什么是迭代器?指的是一个重复的过程,每一次重
- 在mysql中limit可以实现快速分页,但是如果数据到了几百万时我们的limit必须优化才能有效的合理的实现分页了,否则可能卡死你的服务器
- 前言使用 Pandas 的between 、cut、qcut 和 value_count离散化数值变量。分箱是一种常见的数据预处理技术有时也
- 本文实例讲述了Python使用中文正则表达式匹配指定中文字符串的方法。分享给大家供大家参考,具体如下:业务场景:从中文字句中匹配出指定的中文
- aspjpeg版本:v1.801 将pic.jpg打上logo.png,可根据图片大小对水印图做适当调整 &
- 我们需要做的第⼀件事情是获取 MNIST 数据。如果你是⼀个 git ⽤⼾,那么你能够通过克隆这本书的代码仓库获得数据,实现我们的⽹络来分类
- 前言:在使用DDT数据驱动+HTMLTestRunner输出测试报告时遇到过2个问题:1、生成的测试报告中,用例名称后有dict() -&g
- 一般与页面有关的系统都会有大量的静态文件,包括js、css以及图标图片等,这些文件一般是项目的相对路径,在加载的时候会从本地读取再转发出去。
- 本文实例讲述了JavaScript实现的伸展收缩型菜单代码。分享给大家供大家参考。具体如下:运行效果截图如下:具体代码如下:<html
- 本文实例为大家分享了element跨分页操作选择的具体代码,供大家参考,具体内容如下业务需求:在批量导出或者批量删除的时候会涉及到跨分页导出
- 骨骼识别的应用场景如今,当前疫情大环境之下。很多人,因为居家办公或者其他原因闷在家里不能外出健身。那么,借助骨骼识别和卷积神经网络模型,计算
- 只需要在fckeditor\editor\filemanager\connectors\asp文件夹下的commands.asp修改一下即可