惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Recorded Future
Recorded Future
Security Archives - TechRepublic
Security Archives - TechRepublic
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Jina AI
Jina AI
I
InfoQ
D
DataBreaches.Net
人人都是产品经理
人人都是产品经理
腾讯CDC
GbyAI
GbyAI
V
Visual Studio Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Microsoft Azure Blog
Microsoft Azure Blog
F
Fortinet All Blogs
博客园 - 聂微东
美团技术团队
The Register - Security
The Register - Security
Engineering at Meta
Engineering at Meta
Apple Machine Learning Research
Apple Machine Learning Research
雷峰网
雷峰网
S
Schneier on Security
量子位
A
About on SuperTechFans
H
Help Net Security
MongoDB | Blog
MongoDB | Blog
S
SegmentFault 最新的问题
Know Your Adversary
Know Your Adversary
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
Simon Willison's Weblog
Simon Willison's Weblog
PCI Perspectives
PCI Perspectives
B
Blog
K
Kaspersky official blog
V
Vulnerabilities – Threatpost
aimingoo的专栏
aimingoo的专栏
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
U
Unit 42
G
Google Developers Blog
L
LINUX DO - 最新话题
Forbes - Security
Forbes - Security
AWS News Blog
AWS News Blog
P
Palo Alto Networks Blog
Security Latest
Security Latest
爱范儿
爱范儿
Attack and Defense Labs
Attack and Defense Labs
IT之家
IT之家
L
LINUX DO - 热门话题
D
Docker
P
Proofpoint News Feed
Y
Y Combinator Blog
P
Proofpoint News Feed

博客园 - 骨月枫🍁

使用vLLM部署Qwen/Qwen3.5-35B-A3B-FP8并且在DIFY中调用 记录个人数组、字符串自己常忘记的方法,以及ES常用处理方式 解决webpack vue 项目打包生成的文件,资源文件均404问题 js 复制粘贴功能记录 记录下工作中使用的pdf.js 记录下jplayer的简单demo js 函数节流 用ajax与fetch调用阿里云免费接口 简要谈谈javascript bind 方法 h5启动原生APP总结 mac上安装mongoDb以及简单使用 mac快捷键整理以及node的基本使用 html5视频video积累 html5 实现简单的上传 canvas基础学习(四) canvas基础学习(三) canvas基础学习(二) canvas基础学习(一) 移动端使用百度分享代码
整理下PC和移动获取点击、移动坐标的代码和坑
骨月枫🍁 · 2016-03-30 · via 博客园 - 骨月枫🍁

一、PC

PC是通过鼠标点击和移动,相对比较简单,比如onmousedown、onmouseup、onmousemove、onmouseout鼠标按键按下、按键起来、鼠标在元素上移动、鼠标从元素上离开。

canvas.onmousedown = function(e) {
    console.log(e.clientX, e.clientY);
}
canvas.onmouseup = function(e) {
    console.log(e.clientX, e.clientY);
}
canvas.onmousemove = function(e) {
    console.log(e.clientX, e.clientY);
}
canvas.onmouseout = function(e) {
    console.log(e.clientX, e.clientY);
}

PC端可以直接通过事件的clientX和clientY来获取点击的坐标,这个坐标(e.clientX , e.clientY)是相对于window的左上角来确定的。

同样事件参数evnet,有一些常用方法如阻止事件在浏览器中的默认操作和事件冒泡。

e.preventDefault()为阻止事件在浏览器中的默认操作,如input type为submit时的提交操作,我一般在事件函数中都会使用它,特别是在移动端。记得之前有个坑,在微信内置浏览器中,touchmove事件触发时,微信内置浏览器会默认拖动整个WebView向上或向下移动,显示出那个QQ浏览器内核什么鬼的文字,而没有执行想要的操作。

e.stopPropagation()为防止事件穿透,不调用此方法时,事件被捕获后,仍然会继续传播可能会被下方的元素事件所捕获进而造成影响。所以在元素覆盖元素,且都有绑定事件的时候需要阻止事件冒泡。

二、移动

移动和PC在处理事件上有些不同之处,首先事件上不同,移动这边是touchstart、touchmove、touchend这3个事件。

canvas.addEventListener("touchstart", function(e) {
    console.log(e.touches[0].pageX, e.touches[0].pageY);
});
canvas.addEventListener("touchmove", function(e) {
    if(e.touches.length > 1 || e.scale && e.scale !== 1) return;
    console.log(e.touches[0].pageX, e.touches[0].pageY);
});
canvas.addEventListener("touchend", function(e) {});

移动端由于是手指操作而非鼠标,所以存在多点触控,即多根手指在屏幕上触发事件。所以,不在跟PC一样,通过e.clientX来获取单个点坐标。而是事件event中存在一个触控集合touches这个数组,通过取数组的第一个元素来获取坐标位置,即第一个触碰屏幕手指的坐标(e.touches[0].pageX , e.touches[0].pageY)。而有时往往有需要获取全部触碰点的位置,那就要循环数组了,逐个处理。另一个坑就是有时要防止多点触碰,以及手指对屏幕进行缩放的影响,可以加入以上判断if(e.touches.length > 1 || e.scale && e.scale != 1)。

最后就是touchend事件,代表手指离开屏幕不存在触控,所以e.touches这个数组的长度为0,也就不能在touchend的处理函数中获取pageX属性了。

三、元素内的位置

通过以上方法获取的坐标是以window左上角为原点的坐标,但是在实际中,往往获取的是以某个元素左上角为原点的坐标,这时就需要做个转化。以canvas为例

function windowToCanvas(x, y) { //window坐标转canvas坐标
    return {
        x: Math.round(x - document.getElementById("canvas").getBoundingClientRect().left - document.body.scrollLeft),
        y: Math.round(y - document.getElementById("canvas").getBoundingClientRect().top - document.body.scrollTop)
    }
}

getBoundingClientRect()方法可以获取元素的盒子模型,通过top、left、right、bottom这4个属性可以拿到四周的边距。