gpt4 book ai didi

javascript - 鼠标坐标似乎错误地放置了 Raphael.js 对象

转载 作者:行者123 更新时间:2023-11-30 19:23:28 26 4
gpt4 key购买 nike

我在拉斐尔空间上有一些矩形。我设置了快速鼠标坐标显示以精确放置对象:

document.addEventListener("mousemove",function(event){

var x = event.clientX;
var y = event.clientY;

document.getElementById('position').innerHTML = " X = "+ x +" Y = "+ y;

});

但是,矩形似乎比预期的位置大了大约 10 像素。为什么会这样? (我也知道使用 style 而不是 css 文件是不好的做法,但我只是想直接跳到玩拉斐尔而不用担心)

var p = Raphael("paper");
let r1, r2, r3, l1;


r1 = p.rect(200, 200, 40, 40)
.attr({
'fill': 'red',
'cursor': 'pointer',
'href': 'https://www.google.com/',
});
r2 = p.rect(277, 320, 50, 50)
.attr({
'fill': 'blue',
'cursor': 'pointer',
'href': 'https://www.google.com/',
});
p.path("M 200,200 L 240,240");


r3 = p.rect(377, 300, 30, 80)
.attr({
'fill': 'yellow',
'cursor': 'pointer',
'href': 'https://www.google.com/',
});




document.addEventListener("mousemove", function(event) {

var x = event.clientX;
var y = event.clientY;

document.getElementById('position').innerHTML = " X = " + x + " Y = " + y;

});
<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8">
<title>tester2</title>
<script src="raphael-min.js"></script>
</head>

<body>
<div id="content">
<div id="paper"></div>
</div>
<p id="position">Test</p>
<style>
#paper {
height: 500px;
width: 500px;
border: 1px solid black;
}
</style>
</body>

我怀疑我的鼠标坐标系完全关闭了,但如果是这样我不知道如何相对于 Raphael("paper");

配置它

此处问题的实时链接:https://jsfiddle.net/oLwydha6/

我该如何解决这个问题?

最佳答案

总的差异是 9px,主体的边距 (8px) 和边框 (1px)。您可能需要考虑使用 offsetX而不是 clientX获取位置:

var p = Raphael("paper");
let r1, r2, r3, l1;


r1 = p.rect(200, 200, 40, 40)
.attr({
'fill': 'red',
'cursor': 'pointer',
'href': 'https://www.google.com/',
});
r2 = p.rect(277, 320, 50, 50)
.attr({
'fill': 'blue',
'cursor': 'pointer',
'href': 'https://www.google.com/',
});
p.path("M 200,200 L 240,240");


r3 = p.rect(377, 300, 30, 80)
.attr({
'fill': 'yellow',
'cursor': 'pointer',
'href': 'https://www.google.com/',
});

document.addEventListener("mousemove", function(event) {

var x = event.clientX;
var y = event.clientY;
document.getElementById('position').innerHTML = " clientX = " + x + " clientY = " + y;
document.getElementById('position_offset').innerHTML = " offsetX = " + event.offsetX + " offsetY = " + event.offsetY;
});
#paper {
height: 500px;
width: 500px;
border: 1px solid black;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/raphael/2.2.8/raphael.min.js"></script>
<div id="content">
<div id="paper"></div>
</div>
<p id="position">Test</p>
<p id="position_offset">Test</p>
<h1 id="h1">hello world</h1>

关于javascript - 鼠标坐标似乎错误地放置了 Raphael.js 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57193529/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com