移动端事件

文章目录

  • 移动端事件
    • 概述
    • 兼容性
    • Touch触摸事件
      • 事件类型
      • 是否支持事件
      • 使用
      • event对象
      • touch对象
      • 阻止浏览器默认行为
      • 单指拖拽
    • Pointer指针事件
      • 事件类型
      • 是否支持事件
      • 使用
      • event对象
      • 阻止浏览器默认行为
      • 单指拖拽

移动端事件

概述

移动端事件可分为:

  • Touch触摸事件
  • Pointer指针事件

没有移动端事件时,大部分鼠标事件可以在移动端中使用。

苹果推出Touch触摸事件,Touch事件在PC端不会触发。

微软推出Pointer事件,直接继承了鼠标事件,在此基础上又添加了其他一些内容,处理 Pointer 事件和处理鼠标事件几乎一致。移动端和pc端适配推荐使用pointer事件。

兼容性

在这里插入图片描述

在这里插入图片描述

Touch触摸事件

事件类型

事件类型说明
touchstart手指触摸时触发
touchmove手指触摸移动时触发
touchend手指离开时触发
touchcancel触摸被

是否支持事件

if ("ontouchstart" in window) {
    console.log("支持ontouchstart");
} else {
    console.log("不支持ontouchstart");
}

使用

<!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" />
    <title>Touch事件</title>
    <style>
      * {
        padding: 0;
        margin: 0;
      }
      #box {
        margin: 10px;
        width: 200px;
        height: 200px;
        background-color: pink;
      }
    </style>
  </head>
  <body>
    <div id="box"></div>
    <script>
      if ("ontouchstart" in window) {
        console.log("支持ontouchstart");
      } else {
        console.log("不支持ontouchstart");
      }
      let box = document.getElementById("box");
      box.addEventListener("touchstart", startHandler);
      box.addEventListener("touchmove", moveHandler);
      box.addEventListener("touchend", endHandler);
      box.addEventListener("touchcancel", cancelHandler);
      function startHandler(e) {
        console.log("touchstart");
      }
      function moveHandler() {
        console.log("touchmove");
      }
      function endHandler() {
        console.log("touchend");
      }
      function cancelHandler() {
        console.log("touchcancel");
      }
    </script>
  </body>
</html>

event对象

event对象属性说明
type事件类型
target目标元素
touches当前屏幕上所有的触摸点
targetTouches目标元素上的所有触摸点
changedTouches记录触摸状态发生改变的触摸点信息,即只包括新增加的触摸点和移除的触摸点。
box.addEventListener("touchstart", startHandler);
function startHandler(e) {
    console.log("touchstart");
    console.log(e.type);
    console.log(e.target);
    console.log(e.touches);
    console.log(e.targetTouches);
    console.log(e.changedTouches);
}

touch对象

触摸点属性说明
identifier触摸点id(唯一标识),一般多用于多点触摸
target目标元素
screenX/screenY触点相对于屏幕的X、Y坐标
clientX/clientY触摸点相对于浏览器的X、Y坐标 ,不计算滚动条
pageX/pageY触摸点相对于页面的X、Y坐标 ,计算滚动条
function startHandler(e) {
    console.log("touchstart");
    const touch = e.changedTouches[0];
    console.log(touch);
    console.log(touch.identifier); //0
    console.log(touch.target);
    console.log(touch.screenX, touch.screenY); //110 323
    console.log(touch.clientX, touch.clientY); //95 112
    console.log(touch.pageX, touch.pageY); //95 112
}

阻止浏览器默认行为

e.preventDefault()可以阻止滚动、缩放、鼠标事件等默认行为。

<!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" />
    <title>Touch事件</title>
    <style>
      * {
        padding: 0;
        margin: 0;
      }
      body {
        height: 2000px;
      }
      #box {
        position: absolute;
        width: 200px;
        height: 200px;
        background-color: pink;
      }
    </style>
  </head>
  <body>
    <div id="box">
      <img src="./images/1.jpg" alt="" />
    </div>
    <script>
      let box = document.getElementById("box");
      box.addEventListener("touchstart", pointerHandler, false);
      function pointerHandler(e) {
        e.preventDefault();
      }
    </script>
  </body>
</html>

单指拖拽

<!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" />
    <title>Touch事件</title>
    <style>
      * {
        padding: 0;
        margin: 0;
      }
      body {
        height: 2000px;
      }
      #box {
        position: absolute;
        width: 200px;
        height: 200px;
        background-color: pink;
      }
    </style>
  </head>
  <body>
    <div id="box"></div>
    <script>
      let box = document.getElementById("box");

      // 开始点
      const startPoint = {};
      // 目标元素当前位置
      const currentPos = { x: 0, y: 0 };

      box.addEventListener("touchstart", startHandler);
      box.addEventListener("touchmove", moveHandler);
      box.addEventListener("touchend", endHandler);
      box.addEventListener("touchcancel", endHandler);
      function startHandler(e) {
        e.preventDefault();
        const touch = e.changedTouches[0];
        startPoint.x = touch.pageX;
        startPoint.y = touch.pageY;
      }
      function moveHandler(e) {
        const touch = e.changedTouches[0];
        // x轴移动距离
        let distanceX = touch.pageX - startPoint.x;
        // y轴移动距离
        let distanceY = touch.pageY - startPoint.y;
        box.style.left = distanceX + currentPos.x + "px";
        box.style.top = distanceY + currentPos.y + "px";
      }
      function endHandler(e) {
        currentPos.x = parseInt(box.style.left);
        currentPos.y = parseInt(box.style.top);
      }
    </script>
  </body>
</html>

Pointer指针事件

事件类型

事件类型说明
pointerdown指针按下时触发
pointerup指针抬起时触发
pointermove指针移动时触发
pointerenter指针进入目标元素时触发
pointerleave指针离开目标元素时触发
pointerover指针悬停在目标元素上时触发
pointerout指针离开目标元素时触发

是否支持事件

if ("onpointerdown" in window) {
    console.log("支持Pointer事件");
} else {
    console.log("不支持Pointer事件");
}

使用

let box = document.getElementById("box");

box.addEventListener("pointerover", pointerHandler, false);
box.addEventListener("pointerenter", pointerHandler, false);
box.addEventListener("pointerdown", pointerHandler, false);
box.addEventListener("pointermove", pointerHandler, false);
box.addEventListener("pointerup", pointerHandler, false);
box.addEventListener("pointerout", pointerHandler, false);
box.addEventListener("pointerleave", pointerHandler, false);

function pointerHandler(evt) {
    console.log(evt.type);
}

event对象

event对象属性说明
pointerId指针id(唯一标识)
type事件类型
pointerType指针类型(鼠标、笔、触摸)
target目标元素
screenX/screenY指针相对于屏幕的X、Y坐标
clientX/clientY指针相对于可视区域的X、Y坐标,不包括任何滚动偏移
x/yclientX/clientY 的别名
pageX/pageY指针相对于文档的X、Y坐标,包括滚动偏移
box.addEventListener("pointerdown", pointerHandler, false);
function pointerHandler(e) {
    console.log(e);
    console.log(e.pointerId);
    console.log(e.type);
    console.log(e.pointerType);
    console.log(e.target);
    console.log(e.screenX, e.screenY); //114 300
    console.log(e.clientX, e.clientY); //98 144
    console.log(e.x, e.y); //98 144
    console.log(e.pageX, e.pageY); //98 144
}

阻止浏览器默认行为

  • 方式一:通过evt.preventDefault() 阻止默认行为,主要用于PC端,不能阻止滚动、缩放、鼠标事件,可以阻止图片拖动行为。
  • 方式二:通过CSS属性touch-action :none可以阻止所有默认行为。还有其他属性值:
    • auto:默认行为处理触摸事件。
    • none:禁止所有默认行为。
    • pan-x:只处理水平平移手势。
    • pan-y:值处理垂直平移手势。
    • pan-x pan-y:处理水平和垂直平移手势。
    • manipulation:启动所有手势,但经营缩放和滚动手势。
<!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" />
    <title>Touch事件</title>
    <style>
      * {
        padding: 0;
        margin: 0;
      }
      #box {
        position: absolute;
        width: 200px;
        height: 200px;
        background-color: pink;
      }
    </style>
  </head>
  <body>
    <div id="box">
      <img src="./images/1.jpg" alt="" />
    </div>
    <script>
      let box = document.getElementById("box");
      box.addEventListener("pointerdown", pointerHandler, false);
      function pointerHandler(e) {
        e.preventDefault();
      }
    </script>
  </body>
</html>
<!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" />
    <title>Touch事件</title>
    <style>
      * {
        padding: 0;
        margin: 0;
      }
      body {
        height: 2000px;
        /* 阻止所有默认行为 */
        touch-action: none; 
      }
      #box {
        position: absolute;
        width: 200px;
        height: 200px;
        background-color: pink;
      }
    </style>
  </head>
  <body>
    <div id="box">
      <img src="./images/1.jpg" alt="" />
    </div>
    </script>
  </body>
</html>

单指拖拽

<!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" />
    <title>Touch事件</title>
    <style>
      * {
        padding: 0;
        margin: 0;
      }
      body {
        height: 2000px;
      }
      #box {
        position: absolute;
        width: 200px;
        height: 200px;
        background-color: pink;
      }
    </style>
  </head>
  <body>
    <div id="box"></div>
    <script>
      let box = document.getElementById("box");
      const startPoint = {};
      const currentPos = { x: 0, y: 0 };
      box.addEventListener("pointerdown", startHandler);
      box.addEventListener("touchstart", (e) => {
        e.preventDefault();
      });
      box.addEventListener("pointermove", moveHandler);
      box.addEventListener("pointerup", endHandler);
      box.addEventListener("pointercancel", endHandler);
      function startHandler(e) {
        startPoint.x = e.pageX;
        startPoint.y = e.pageY;
      }
      function moveHandler(e) {
        let distanceX = e.pageX - startPoint.x;
        let distanceY = e.pageY - startPoint.y;
        box.style.left = currentPos.x + distanceX + "px";
        box.style.top = currentPos.y + distanceY + "px";
      }
      function endHandler(e) {
        currentPos.x = parseInt(box.style.left);
        currentPos.y = parseInt(box.style.top);
      }
    </script>
  </body>
</html>

试试换一种写法:

<!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" />
    <title>Touch事件</title>
    <style>
      * {
        padding: 0;
        margin: 0;
      }
      body {
        height: 2000px;
      }
      #box {
        position: absolute;
        width: 200px;
        height: 200px;
        background-color: pink;
      }
    </style>
  </head>
  <body>
    <div id="box"></div>
    <script>
      let box = document.getElementById("box");
      const startPoint = {};
      const currentPos = { x: 0, y: 0 };
      box.addEventListener("pointerdown", startHandler);
      box.addEventListener("touchstart", (e) => {
        e.preventDefault();
      });
      function startHandler(e) {
        startPoint.x = e.pageX;
        startPoint.y = e.pageY;
        document.addEventListener("pointermove", moveHandler);
        document.addEventListener("pointerup", endHandler);
        document.addEventListener("pointercancel", endHandler);
      }
      function moveHandler(e) {
        e.preventDefault;
        let distanceX = e.pageX - startPoint.x;
        let distanceY = e.pageY - startPoint.y;
        box.style.left = currentPos.x + distanceX + "px";
        box.style.top = currentPos.y + distanceY + "px";
      }
      function endHandler(e) {
        currentPos.x = parseInt(box.style.left);
        currentPos.y = parseInt(box.style.top);
        document.removeEventListener("pointermove", moveHandler);
        document.removeEventListener("pointerup", endHandler);
        document.removeEventListener("pointercancel", endHandler);
      }
    </script>
  </body>
</html>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/15155.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

【Bard】谷歌的人工智能工具—Bard初体验

文章目录 一、Bard介绍二、Bard体验1、加入Bard的候补名单2、登入Bard篇3、使用Bard篇&#xff08;1&#xff09;提供三种预选方式✨&#xff08;2&#xff09;创作生成各类文案&#xff08;3&#xff09;无生成图画能力&#xff08;4&#xff09;支持语音转文本输入✨&#xf…

实景区剧本杀系统开发

实景区剧本杀系统开发需要考虑以下几个方面&#xff1a; 场地选取&#xff1a;选择合适的场地&#xff0c;足够容纳游戏人数和游戏内容&#xff0c;同时需要考虑安全性和便利性。 剧情设定&#xff1a;根据场地和游戏类型设计剧情&#xff0c;包括人物角色、任务目标、…

SpringBoot日志文件

文章目录&#xff1a;一.日志的作用 二.日志的使用&#xff08;1&#xff09;系统默认日志输出 &#xff08;2&#xff09;自定义日志输出 三.日志级别的分类 &#xff08;1&#xff09;默认级别 &#xff08;2&#xff09;自定义级别 四.日志的持久化 &#xff08;1&…

又一次503 service unavailable处理

出现了&#xff1a;503 service unavailable 1&#xff09;查看系统日志 通过事件查看器&#xff0c;查看iis的日志,如下&#xff1a; 在错误信息中提示是 应用程序池提供服务的进程中出现错误。 其他警告也可通过日志目录查看 C:\inetpub\ 出现上述问题的可能是&#xf…

Node第三方包 【Request】

文章目录 &#x1f31f;前言&#x1f31f;Request&#x1f31f;安装与使用&#x1f31f;流&#xff08;stream&#xff09;操作&#x1f31f;Form表单&#x1f31f;application/x-www-form-urlencoded (URL编码的Form)&#x1f31f;multipart/form-data (Multipart Form 上传) …

http协议(一)/应用层

学习目标&#xff1a;⭐理解应用层的作用&#xff0c;理解协议&#xff0c;理解序列化和反序列化&#xff0c;并且实现网络版计算器⭐HTTP协议。⭐手写一个简单的http协议。 应用层 我们写的一个个解决实际问题, 满足我们日常需求的网络程序, 都是在应用层。 协议/序列化与反…

ChatGPT原理剖析

文章目录 ChatGPT常见误解1. 罐头回应2. 网络搜寻重组 ChatGPT真正做的事——文字接龙ChatGPT背后的关键技术——预训练&#xff08;Pre-train&#xff09;一般机器是怎样学习的&#xff1f; ChatGPT带来的研究问题1. 如何精准提出需求2. 如何更改错误3. 侦测AI生成的物件4. 不…

十、v-model的基本使用

一、v-model的基本使用 表单提交是开发中非常常见的功能&#xff0c;也是和用户交互的重要手段&#xff1a; 比如用户在登录、注册时需要提交账号密码&#xff1b;比如用户在检索、创建、更新信息时&#xff0c;需要提交一些数据&#xff1b; 这些都要求我们可以在代码逻辑中…

系统分析师《企业信息化战略与实施》高频知识点

企业信息化战略与实施---企业信息化与电子商务 业务流程重组&#xff08;Business Process Reengineering BPR&#xff09;是针对企业业务流程的基本问题进行反思&#xff0c;并对它进行彻底的重新设计&#xff0c;使业绩取得显著性提高。与目标管理、全面质量管理、战略管理等…

输入捕获实验

实验内容 用TIM5 的通道 1&#xff08;PA0&#xff09;来做输入捕获&#xff0c;捕获 PA0 上高电平的脉宽&#xff08;用 WK_UP 按键输入高电平&#xff09;&#xff0c;通过串口打印高电平脉宽时间。 输入捕获简介 输入捕获模式可以用来测量脉冲宽度或者测量频率。STM32 的…

快速搭建Electron+Vite3+Vue3+TypeScript5脚手架 (无需梯子,快速安装Electron)

一、介绍 &#x1f606; &#x1f601; &#x1f609; Electron是一个使用 JavaScript、HTML 和 CSS 构建桌面应用程序的框架。 嵌入 Chromium 和 Node.js 到 二进制的 Electron 允许您保持一个 JavaScript 代码代码库并创建 在Windows上运行的跨平台应用 macOS和Linux——不需…

宝塔面板搭建自己的网站,并发布公网远程访问

文章目录 1. 环境安装2. 安装cpolar内网穿透3. 内网穿透4.固定http地址5. 配置二级子域名6.创建一个测试页面 宝塔面板简单几步搭建本地web站点&#xff0c;并做内网穿透&#xff0c;实现公网用户也可以正常远程访问&#xff0c;无需公网IP&#xff0c;无需设置路由器。 1. 环…

Volatile系列(一):Volatile测试案例一可见性

系列文章 Volatile测试案例一可见性 目录 前言 测试1 逻辑 代码 结果 测试2 逻辑 代码 结果 结论 原理探讨&#xff08;可见性&#xff09; 前言 多线程是 JAVA 并发编程的主要应用&#xff0c;并发环境能大幅提高应用性能&#xff0c;提高 CPU 使用率&#xff0c…

优雅的接口防刷处理方案

前言 本文为描述通过Interceptor以及Redis实现接口访问防刷Demo 这里会通过逐步找问题&#xff0c;逐步去完善的形式展示 原理 通过ip地址uri拼接用以作为访问者访问接口区分 通过在Interceptor中拦截请求&#xff0c;从Redis中统计用户访问接口次数从而达到接口防刷目的 …

不应使用Excel进行项目资源规划的 7 个原因

项目资源规划早期仅限于基本分配和调度。因此&#xff0c;企业使用自制工具或excel表来执行这一简单功能。然而&#xff0c;随着技术和业务流程的发展&#xff0c;资源规划变得复杂&#xff0c;并包括其他组成部分&#xff0c;如预测和容量规划&#xff0c;优化等。 由于传统…

掌握好这几款TikTok数据分析工具,让你轻松提高曝光率!

为什么别人在TikTok发的普普通通的视频却有那么高的流量、几天内疯狂涨粉&#xff0c;而自己想破脑袋装饰自己的视频&#xff0c;结果却不如人意。 其实原因很简单&#xff0c;TikTok不像国内的抖音只面向中华民族&#xff0c;而是覆盖了150多个国家和75种语言用户&#xff0c…

【通信接口】UART、IIC、SPI

目录 一、预备知识 1、串行与并行 2、单工与双工 3、波特率 二、UART 三、IIC 四、SPI &#xff08;一对一、一对多&#xff09; 五、IIC、SPI异同点 参考文章&#xff1a;这些单片机接口&#xff0c;一定要熟悉&#xff1a;UART、I2C、SPI、TTL、RS232、RS422、RS485…

【Java EE初阶】计算机简介及多线程之创建线程

目录 1.计算机发展史 2.冯诺依曼体系 3.操作系统 操作系统的作用&#xff1a; 4.进程 1.PID&#xff08;进程编号&#xff09; 2.内存指针 应用程序申请到的内存中的首地址 3.文件描述符表 问&#xff1a;什么是并发&#xff1f;什么是并行&#xff1f; 4.进程的优先级&a…

卡尔曼滤波原理及代码

目录 一.简介 二.原理 1.先验估计原理 2.后验估计原理 3.总结 三.示例 一.简介 卡尔曼滤波&#xff08;Kalman filtering&#xff09;是一种利用线性系统状态方程&#xff0c;通过系统输入输出观测数据&#xff0c;对系统状态进行最优估计的算法&#xff0c;它可以在任意…

StarRocks 3.0 集群安装手册

本文介绍如何以二进制安装包方式手动部署最新版 StarRocks 3.0集群。 什么是 StarRocks StarRocks 是新一代极速全场景 MPP (Massively Parallel Processing) 数据库。StarRocks 的愿景是能够让用户的数据分析变得更加简单和敏捷。用户无需经过复杂的预处理&#xff0c;就可以…
最新文章