网上的代码杂七杂八,  我搞个简单明了的!!  你们直接复制粘贴,  手机上 电脑上 可以直接测试!!!

上图:

上代码:

<!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手机端判断滑动还是点击的更多相关文章

  1. touch.js 手机端的操作手势

    使用原生的touchstart总是单击.长按有冒泡冲突事件,发现百度在几年开源的touch.js库,放在现在来解决手机端的操作手势,仍然很好用.

  2. js 手机端触发事事件、javascript手机端/移动端触发事件

    处理Touch事件能让你跟踪用户的每一根手指的位置.你可以绑定以下四种Touch事件: touchstart: // 手指放到屏幕上的时候触发 touchmove: // 手指在屏幕上移动的时候触发 ...

  3. 手机端左右滑动,不用写js(只有页面切换到移动端可以看)

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  4. 使用JS进行pc端、手机端判断

     <script type="text/javascript">            (function(){                var ua = nav ...

  5. [js开源组件开发]js手机端浮层控件,并有多种弹出小提示,兼容pc端浏览器

    js dialog组件,包含alert和confirm的实现 本组件所有的资源均在github上可以查看源代码 GitHub 本dialog的组件的例子请在这里查看 demo dialog js di ...

  6. JS 手机端多张图片上传

    代码如下 <!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="u ...

  7. touch.js——手机端的操作手势

    TOUCH.JS手势操作,例如一指拖动.两指旋 基本事件: touchstart   //手指刚接触屏幕时触发 touchmove    //手指在屏幕上移动时触发 touchend     //手指 ...

  8. 去除手机端触摸滑动事件ontouchmove

    window.ontouchmove=function(e){ e.preventDefault && e.preventDefault(); e.returnValue=false; ...

  9. 【手机端判断】PC_to_M自写

    var current_url = window.location.href; var replace_url = [ ['笔试简章','http://beijing.ysedu.com/zt/bjt ...

随机推荐

  1. 国产化之银河麒麟安装.NetCore-包管理器方式

    背景 某个项目需要实现基础软件全部国产化,其中操作系统指定银河麒麟,数据库使用达梦V8,CPU平台的范围包括x64.龙芯.飞腾.鲲鹏等. 考虑到这些基础产品对.NETCore的支持,最终选择了3.1版 ...

  2. Laravel-多条件检索方案

    控制器 public function index(Request $request) { $status = $request->input('status'); $title = $requ ...

  3. php 数组相关的函数?

    array()----创建数组array_combine()----通过合并两个数组来创建一个新数组range()----创建并返回一个包含指定范围的元素的数组compact()----建立一个数组a ...

  4. .Net Core(.NET6)中接入Log4net和NLog进行日志记录

    一.接入Log4net 1.按日期和大小混合分割日志 nuget包安装 log4net Microsoft.Extensions.Logging.Log4Net.AspNetCore 配置文件 配置文 ...

  5. MySQL 8.0无法通过Navicat远程连接 1045 - Access denied for user 'root'@'::1' (using password: YES) 的解决办法

    用navicat远程连接数据库的时候无法连接,提示信息:1045 - Access denied for user 'root'@'::1' (using password: YES). 是由于密码策 ...

  6. 35 异常机制 异常处理机制 异常处理五个关键字 try、catch、finally、throw、thorws 代码

    异常处理机制 概念 抛出异常 捕获异常 异常处理五个关键字 try.catch.finally.throw.thorws 代码 // main { int a = 1; int b = 0; // 假 ...

  7. java笔记:00 数据类型

  8. 『德不孤』Pytest框架 — 14、Pytest中的conftest.py文件

    目录 1.conftest.py文件介绍 2.conftest.py的注意事项 3.conftest.py的使用 4.不同位置conftest.py文件的优先级 5.conftest.py中Fixtu ...

  9. sqlmap + DVWA 入门

    sqlmap 入门 前言 项目地址: sqlmap: https://github.com/sqlmapproject/sqlmap DVWA: https://github.com/ethicalh ...

  10. 如何让 if/else 更优雅?

    if else 是我们写代码时,使用频率最高的关键词之一,然而有时过多的 if else 会让我们感到脑壳疼,有没有什么方法可以让我们避免来写这么多的 if else 呢? 1.使用 return 我 ...