How to get the MouseEvent coordinates for an element that has CSS3 Transform?
I want to detect where a MouseEvent
has
occurred, in coordinates relative to the clicked element. Why? Because I want to add an absolutely positioned child element
at the clicked location.
I know how to detect it when no CSS3 transformations exist (see description below). However, when I add a CSS3 Transform, then my algorithm breaks, and I don't know how to fix it.
I'm not using any JavaScript library, and I want to understand how things work in plain JavaScript. So, please, don't answer with "just use jQuery".
By the way, I want a solution that works for all MouseEvents, not just "click". Not that it matters, because I believe all mouse events share the same properties, thus the same solution should work for all of them.
Background information
According to DOM
Level 2 specification, a MouseEvent
has
few properties related to getting the event coordinates:
screenX
andscreenY
return
the screen coordinates (the origin is the top-left corner of user's monitor)clientX
andclientY
return
the coordinates relative the document viewport.
Thus, in order to find the position of the MouseEvent
relative
to the clicked element content, I must do this math:
ev.clientX - this.getBoundingClientRect().left - this.clientLeft + this.scrollLeft
ev.clientX
is
the coordinate relative to the document viewportthis.getBoundingClientRect().left
is
the position of the element relative to the document viewportthis.clientLeft
is
the amount of border (and scrollbar) between the element boundary and the inner coordinatesthis.scrollLeft
is
the amount of scrolling inside the element
getBoundingClientRect()
, clientLeft
and scrollLeft
are
specified at CSSOM
View Module.
Experiment without CSS Transform (it works)
Confusing? Try the following piece
of JavaScript and HTML. Upon clicking, a red dot should appear exactly where the click has happened. This version is "quite simple" and works as expected.
function click_handler(ev) {
var rect = this.getBoundingClientRect();
var left = ev.clientX - rect.left - this.clientLeft + this.scrollLeft;
var top = ev.clientY - rect.top - this.clientTop + this.scrollTop;
var dot = document.createElement('div');
dot.setAttribute('style', 'position:absolute; width: 2px; height: 2px; top: '+top+'px; left: '+left+'px; background: red;');
this.appendChild(dot);
}
document.getElementById("experiment").addEventListener('click', click_handler, false);
<div id="experiment" style="border: 5px inset #AAA; background: #CCC; height: 400px; position: relative; overflow: auto;">
<div style="width: 900px; height: 2px;"></div>
<div style="height: 900px; width: 2px;"></div>
</div>
Experiment adding a CSS Transform (it fails)
Now, try adding a CSS transform
:
#experiment {
transform: scale(0.5);
-moz-transform: scale(0.5);
-o-transform: scale(0.5);
-webkit-transform: scale(0.5);
/* Note that this is a very simple transformation. */
/* Remember to also think about more complex ones, as described below. */
}
The algorithm doesn't know about the transformations, and thus calculates a wrong position. What's more, the results are different between Firefox 3.6 and Chrome 12. Opera 11.50 behaves just like Chrome.
In this example, the only transformation was scaling, so I could multiply the scaling factor to calculate the correct coordinate. However, if we think about arbitrary transformations (scale, rotate, skew, translate, matrix), and even nested transformations
(a transformed element inside another transformed element), then we really need a better way to calculate the coordinates.
How to get the MouseEvent coordinates for an element that has CSS3 Transform?的更多相关文章
- Why AlloyFinger is so much smaller than hammerjs?
AlloyFinger is the mobile web gesture solution at present inside my company, major projects are in u ...
- OpenSceneGraph控制模型
OpenSceneGraph控制模型 转自:http://www.cppblog.com/eryar/archive/2012/05/28/176538.html 一.简介 对模型的控制就是修改模型的 ...
- 【RobotFramework】Selenium2Library类库关键字使用说明
Add CookieArguments:[ name | value | path=None | domain=None | secure=None | expiry=None ]Adds a coo ...
- pageX/Y, offset(), position(), scrollTop(), screenX/Y, clientX/Y, pageX/Y
event.pageX get mouse position Description: The mouse position relative to the left edge of the docu ...
- cocos2d中的可见性检测
游戏的在进行一次渲染的时候,通常会提交大量的渲染对象给gpu.在这些需要渲染的对象中,并不是所有对象都会出现镜头中,即有一部分对象是不可见的. 通常有两种方式来完成不可见对象的剔除工作: (1)直接交 ...
- js 导出Excel
最近从Silverlight这边转到javascript过来,现在要导出一个导出excel的功能.上级领导指示当页显示多少数据,就导出多少数据,没有必要从后台在去数据.以前也没有接触过这方面的,在网上 ...
- Easy Multiple Copy to Clipboard by ZeroClipboard
要实现在多个复制按钮复制的功能(具体代码在附件中,路径修改一下就行了): <%@ page language="java" import="java.util.*& ...
- 转:VS2008 vs2010中JQUERY智能提醒
第一步: 安装VS 2008 SP1 VS 2008 SP1 在Visual Studio中加了更丰富的JavaScript intellisense支持,对很大部分的JavaScript库加了代码完 ...
- appium点击屏幕(手势)
在android测试过程中,会遇到要点击一下屏幕的需求. 在appium旧版本使用下面代码点击android屏幕,没有报错.Map tap = new HashMap(); tap.put(" ...
随机推荐
- How to improve Java's I/O performance( 提升 java i/o 性能)
原文:http://www.javaworld.com/article/2077523/build-ci-sdlc/java-tip-26--how-to-improve-java-s-i-o-per ...
- void型指针
void型指针,表示这个指针指向的内存中的数据的类型要由用户来指定. 比方内存分配函数malloc函数返回的指针就是void *型. 用户在使用这个指针的时候.要进行强制类型转换,也就是显式说明该指针 ...
- html与JacaScript中的重要思想:预留后路、向后兼容、js分离
以一个简单的web程序为例 详细设计模式请配合代码及凝视食用 <!DOCTYPE html> <!-- 1 预留退路:假设用户禁用了js.链接还能正常显示吗?(href) 2 分离j ...
- android中进程的优先级
android中进程的优先级
- php实现邮箱激活功能
php实现邮箱激活功能 一.样例 二.文件结构 其中swiftmailer-master是第三方插件,用来发验证邮件 三.核心代码 doAction.php 响应页面 <?php header( ...
- Caffe C++API 提取任意一张图片的特征系列二----MemoryData
介绍一种更加灵活的方法,用MemoryData层输入数据,可以直接用opencv接口读入我们的图片再添加的网络中. 第一个问题:仍然是工程建立问题,提示卷积层或其他层没有注册,解决方法与上一篇博客一 ...
- mysql授予IP远程访问访问语句
mysql -u root -p; GRANT ALL PRIVILEGES ON *.* TO '用户名'@'你的IP地址' IDENTIFIED BY '密码' WITH GRANT OPTION ...
- HD-ACM算法专攻系列(19)——Leftmost Digit
问题描述: AC源码: 解题关键是,数据很大,不能强算,需要使用技巧,这里使用科学计算法,令N^N=a*10^n ,取对数后变为 N*log10(N)=log10(a)+n,令x = log10(a) ...
- 开发辅助 | 前端开发工程师对 UI设计、交互设计的认知
1.UI 用户界面 UI:User Interfase 用户界面 UID:User Interfase Designer 用户界面设计师,多指移动 app 的界面设计: 2.一个合格的 UI 设计师, ...
- android 给imageView,文字等加上阴影[记录]
1.链接 https://github.com/Devlight/ShadowLayout 2.效果 3.code compile 'com.github.devlight.shadowlayout: ...