javascript - Canvas offsetTop 和 offsetLeft

标签 javascript html

在 HTML5 Canvas 中什么是 offsetTop 和 offsetLeft ?

我正在尝试获取鼠标单击事件的 X 和 Y。我知道我可以通过:

mycanvas.onclick = function (evt) {
    var offX = evt.layerX - mycanvas.offsetLeft;
    var offY = evt.layerY - mycanvas.offsetTop;
}

但什么是 offsetLeft 和 offsetTop?什么是 LayerX 和 LayerY?

最佳答案

offsetLeft 属性特定于元素,在 documentation 中有描述。作为:

Returns the number of pixels that the upper left corner of the current element is offset to the left within the offsetParent node.

特定于事件的 LayerX 并在此 documentation 中进行了描述作为:

Returns the horizontal coordinate of the event relative to the current layer.

希望对您有所帮助!

关于javascript - Canvas offsetTop 和 offsetLeft,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7821962/

相关文章:

javascript - Node js链接命令运行错误

html - EPUB3音频按钮太大

html - z-index 和 CSS 旋转

javascript - 在浏览器中打开 pptx、potx、docs 等文档,其中文档托管在 VPN 服务器上

javascript - 从当前 tr 开始寻找第一个非空 td

html - 将鼠标悬停在导航链接上时如何删除空格?

javascript - 在子元素的指令中获取 CSS 属性

javascript - 我已经将这个简单的脚本组合在一起来旋转图片并将它们链接到指定的 URL,当我将鼠标悬停在图片上时如何让它暂停?

javascript - Angular.js 中的 input[radio] 问题未定义且未经检查

javascript - google.visualization.DataTable过滤重绘时需要重新计算列值