js手机端判断滑动还是点击
网上的代码杂七杂八, 我搞个简单明了的!! 你们直接复制粘贴, 手机上 电脑上 可以直接测试!!!
上图:


上代码:
<!DOCTYPE html>
<html lang="en"> <head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport"
content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no">
<title>记录用户行为</title>
<style>
* {
padding: 0;
margin: 0;
color: #ccc;
}
</style>
</head> <body>
<div id="app">
<h1 v-for="item in 40">滑动我试试看{{item}}</h1>
</div>
<!-- 思路: 区分 点击和滑动, 关键在于 touchmove 事件; 只有滑动,touchmove中才能获取到 坐标值;
然后通过 touchend中 判断 touchmove是否获取到值, 从而来判断是否是滑动,
重点:touchend 后 把 touchmove获取到 坐标值 去除; -->
</body> </html>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
new Vue({
el: '#app',
data: {
x: '',//touchStart记录的坐标
y: '',//touchStart记录的坐标
touchMove_y: ''//touchMove_y != '' 是滑动 touchMove_y == '' 是点击
},
methods: {
toHot() {
location.href = './2222.html'
},
touchSatrtFunc(evt) {
try { var touch = evt.touches[0]; //获取第一个触点
this.x = Number(touch.pageX); //页面触点X坐标
this.y = Number(touch.pageY); //页面触点Y坐标 } catch (e) {
console.log('touchSatrtFunc:' + e.message);
} },
touchMoveFunc(evt) {
try {
var touch = evt.touches[0];
var x = Number(touch.pageX);
var y = Number(touch.pageY);
console.log(x, y)
// 判断是否滑动还是点击, this.touchMove_y == ''是点击 this.touchMove_y != '' 是滑动
this.touchMove_y = y
} catch (e) {
alert('touchMoveFunc:' + e.message);
}
},
touchEndFunc(evt) {
try {
// 判断是否滑动还是点击, this.touchMove_y == ''是点击 this.touchMove_y != '' 是滑动
console.log(this.touchMove_y == '')
if (this.touchMove_y == '') { alert('这是点击')
} else {
alert('这是滑动', this.y, this.x)
this.touchMove_y = '' //记录数据后 修改touchMove_y = '' 重点!!!!!!
} } catch (e) {
console.log('touchSatrtFunc:' + e.message);
}
},
bindEvent() {
document.addEventListener('touchstart', this.touchSatrtFunc, false);
document.addEventListener('touchmove', this.touchMoveFunc, false);
document.addEventListener('touchend', this.touchEndFunc, false);
}
},
mounted() {
this.bindEvent()
}
}) </script>
js手机端判断滑动还是点击的更多相关文章
- touch.js 手机端的操作手势
使用原生的touchstart总是单击.长按有冒泡冲突事件,发现百度在几年开源的touch.js库,放在现在来解决手机端的操作手势,仍然很好用.
- js 手机端触发事事件、javascript手机端/移动端触发事件
处理Touch事件能让你跟踪用户的每一根手指的位置.你可以绑定以下四种Touch事件: touchstart: // 手指放到屏幕上的时候触发 touchmove: // 手指在屏幕上移动的时候触发 ...
- 手机端左右滑动,不用写js(只有页面切换到移动端可以看)
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- 使用JS进行pc端、手机端判断
<script type="text/javascript"> (function(){ var ua = nav ...
- [js开源组件开发]js手机端浮层控件,并有多种弹出小提示,兼容pc端浏览器
js dialog组件,包含alert和confirm的实现 本组件所有的资源均在github上可以查看源代码 GitHub 本dialog的组件的例子请在这里查看 demo dialog js di ...
- JS 手机端多张图片上传
代码如下 <!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="u ...
- touch.js——手机端的操作手势
TOUCH.JS手势操作,例如一指拖动.两指旋 基本事件: touchstart //手指刚接触屏幕时触发 touchmove //手指在屏幕上移动时触发 touchend //手指 ...
- 去除手机端触摸滑动事件ontouchmove
window.ontouchmove=function(e){ e.preventDefault && e.preventDefault(); e.returnValue=false; ...
- 【手机端判断】PC_to_M自写
var current_url = window.location.href; var replace_url = [ ['笔试简章','http://beijing.ysedu.com/zt/bjt ...
随机推荐
- java 注释与标识符
JAVA基础 注释与标识符 注释 书写注释是一个非常好的习惯 三种注释: 单行,多行,文档 .单行 ://注释 .多行:/* 注释 / .文档** 注释 */ 标识符 1. 关键字 2.标识符注意点 ...
- 【爬虫】python爬虫
爬虫章节 1.python如何访问互联网 URL(网页地址)+lib=>urllib 2.有问题查文档:python document. 3.response = urllib.request. ...
- layui 表单的使用
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- php 23种设计模型 - 工厂模式
工厂模式(Factory) 工厂模式(Factory Pattern)是 Java 中最常用的设计模式之一.这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式. 在工厂模式中,我们在创建 ...
- git pull origin master 报错问题解决 fatal: couldn‘t find remote ref master
报错:fatal: couldn't find remote ref master 解决:使用以下命令 git pull origin main 替代报错命令: git pull origin mas ...
- spring——整合Mybatis
一.Mybatis整合spring 步骤: 导入相关jar包 junit mybatis mysql数据库 spring-webmvc aop织入 mybatis-spring spring-jdbc ...
- sqlmap之waf绕过
#一点补充 在老版本的安全狗中,可通过构造payload: http://xx.xx.xx.xx/sqli-labs/Less-2/index.php/x.txt?id=1 and 1=1 可通过in ...
- Bigdecimal 比较问题
- Linux 性能调优都有哪几种方法?
1.Disabling daemons (关闭 daemons). 2.Shutting down the GUI (关闭 GUI). 3.Changing kernel paramete ...
- Kafka02--Kafka生产者简要原理
前言 在Kafka01--Kafka生产者使用方式中对KafkaProducer的基本使用方式进行了了解.以上只是使用方面,一个好的开元框架必定是易于开发者使用的,但是对生产者的基本逻辑流程和数据流转 ...