I have the following event handler for my html element
我的html元素有以下事件处理程序
jQuery("#seek-bar").click(function(e){
var x = e.pageX - e.target.offsetLeft;
alert(x);
});
I need to find the position of the mouse on the #seek-bar at the time of clicking. I would have thought the above code should work, but it gives incorrect result
我需要在单击时找到#seek-bar上的鼠标位置。我本以为上面的代码应该可以工作,但结果却不正确
6 个解决方案
#1
209
Are you trying to get the position of mouse pointer relative
to element ( or ) simply the mouse pointer location
Try this Demo : http://jsfiddle.net/AMsK9/
您是否试图获得相对于元素(或)的鼠标指针位置的鼠标指针位置?请尝试这个演示:http://jsfiddle.net/AMsK9/
Edit :
1) event.pageX
, event.pageY
gives you the mouse position relative document !
1)事件。pageX、事件。佩吉给你鼠标位置相关文件!
Ref : http://api.jquery.com/event.pageX/
http://api.jquery.com/event.pageY/
裁判:http://api.jquery.com/event.pageX/ http://api.jquery.com/event.pageY/
2) offset()
: It gives the offset position of an element
2) offset():给出元素的偏移位置
Ref : http://api.jquery.com/offset/
裁判:http://api.jquery.com/offset/
3) position()
: It gives you the relative Position of an element i.e.,
3) position():它给出一个元素的相对位置,即,
consider an element is embedded inside another element
考虑一个元素嵌入到另一个元素中。
example :
例子:
<div id="imParent">
<div id="imchild" />
</div>
Ref : http://api.jquery.com/position/
裁判:http://api.jquery.com/position/
HTML
HTML
<body>
<div id="A" style="left:100px;"> Default <br /> mouse<br/>position </div>
<div id="B" style="left:300px;"> offset() <br /> mouse<br/>position </div>
<div id="C" style="left:500px;"> position() <br /> mouse<br/>position </div>
</body>
JavaScript
JavaScript
$(document).ready(function (e) {
$('#A').click(function (e) { //Default mouse Position
alert(e.pageX + ' , ' + e.pageY);
});
$('#B').click(function (e) { //Offset mouse Position
var posX = $(this).offset().left,
posY = $(this).offset().top;
alert((e.pageX - posX) + ' , ' + (e.pageY - posY));
});
$('#C').click(function (e) { //Relative ( to its parent) mouse position
var posX = $(this).position().left,
posY = $(this).position().top;
alert((e.pageX - posX) + ' , ' + (e.pageY - posY));
});
});
#2
11
$('#something').click(function (e){
var elm = $(this);
var xPos = e.pageX - elm.offset().left;
var yPos = e.pageY - elm.offset().top;
console.log(xPos, yPos);
});
#3
3
Try this:
试试这个:
jQuery(document).ready(function(){
$("#special").click(function(e){
$('#status2').html(e.pageX +', '+ e.pageY);
});
})
Here you can find more info with DEMO
在这里你可以找到更多的信息与演示
#4
1
If MouseEvent.offsetX is supported by your browser (all major browsers actually support it), The jQuery Event object will contain this property.
如果MouseEvent。offsetX由您的浏览器支持(所有主要浏览器实际上都支持它),jQuery事件对象将包含此属性。
The MouseEvent.offsetX read-only property provides the offset in the X coordinate of the mouse pointer between that event and the padding edge of the target node.
鼠标事件。offsetX只读属性提供鼠标指针在事件和目标节点的填充边之间的X坐标中的偏移量。
$("#seek-bar").click(function(event) {
var x = event.offsetX
alert(x);
});
#5
0
In percentage :
在百分比:
$('.your-class').click(function (e){
var $this = $(this); // or use $(e.target) in some cases;
var offset = $this.offset();
var width = $this.width();
var height = $this.height();
var posX = offset.left;
var posY = offset.top;
var x = e.pageX-posX;
x = parseInt(x/width*100,10);
x = x<0?0:x;
x = x>100?100:x;
var y = e.pageY-posY;
y = parseInt(y/height*100,10);
y = y<0?0:y;
y = y>100?100:y;
console.log(x+'% '+y+'%');
});
#6
0
see here enter link description here
在这里输入链接描述
html
html
<body>
<p>This is a paragraph.</p>
<div id="myPosition">
</div>
</body>
css
css
#myPosition{
background-color:red;
height:200px;
width:200px;
}
jquery
jquery
$(document).ready(function(){
$("#myPosition").click(function(e){
var elm = $(this);
var xPos = e.pageX - elm.offset().left;
var yPos = e.pageY - elm.offset().top;
alert("X position: " + xPos + ", Y position: " + yPos);
});
});
#1
209
Are you trying to get the position of mouse pointer relative
to element ( or ) simply the mouse pointer location
Try this Demo : http://jsfiddle.net/AMsK9/
您是否试图获得相对于元素(或)的鼠标指针位置的鼠标指针位置?请尝试这个演示:http://jsfiddle.net/AMsK9/
Edit :
1) event.pageX
, event.pageY
gives you the mouse position relative document !
1)事件。pageX、事件。佩吉给你鼠标位置相关文件!
Ref : http://api.jquery.com/event.pageX/
http://api.jquery.com/event.pageY/
裁判:http://api.jquery.com/event.pageX/ http://api.jquery.com/event.pageY/
2) offset()
: It gives the offset position of an element
2) offset():给出元素的偏移位置
Ref : http://api.jquery.com/offset/
裁判:http://api.jquery.com/offset/
3) position()
: It gives you the relative Position of an element i.e.,
3) position():它给出一个元素的相对位置,即,
consider an element is embedded inside another element
考虑一个元素嵌入到另一个元素中。
example :
例子:
<div id="imParent">
<div id="imchild" />
</div>
Ref : http://api.jquery.com/position/
裁判:http://api.jquery.com/position/
HTML
HTML
<body>
<div id="A" style="left:100px;"> Default <br /> mouse<br/>position </div>
<div id="B" style="left:300px;"> offset() <br /> mouse<br/>position </div>
<div id="C" style="left:500px;"> position() <br /> mouse<br/>position </div>
</body>
JavaScript
JavaScript
$(document).ready(function (e) {
$('#A').click(function (e) { //Default mouse Position
alert(e.pageX + ' , ' + e.pageY);
});
$('#B').click(function (e) { //Offset mouse Position
var posX = $(this).offset().left,
posY = $(this).offset().top;
alert((e.pageX - posX) + ' , ' + (e.pageY - posY));
});
$('#C').click(function (e) { //Relative ( to its parent) mouse position
var posX = $(this).position().left,
posY = $(this).position().top;
alert((e.pageX - posX) + ' , ' + (e.pageY - posY));
});
});
#2
11
$('#something').click(function (e){
var elm = $(this);
var xPos = e.pageX - elm.offset().left;
var yPos = e.pageY - elm.offset().top;
console.log(xPos, yPos);
});
#3
3
Try this:
试试这个:
jQuery(document).ready(function(){
$("#special").click(function(e){
$('#status2').html(e.pageX +', '+ e.pageY);
});
})
Here you can find more info with DEMO
在这里你可以找到更多的信息与演示
#4
1
If MouseEvent.offsetX is supported by your browser (all major browsers actually support it), The jQuery Event object will contain this property.
如果MouseEvent。offsetX由您的浏览器支持(所有主要浏览器实际上都支持它),jQuery事件对象将包含此属性。
The MouseEvent.offsetX read-only property provides the offset in the X coordinate of the mouse pointer between that event and the padding edge of the target node.
鼠标事件。offsetX只读属性提供鼠标指针在事件和目标节点的填充边之间的X坐标中的偏移量。
$("#seek-bar").click(function(event) {
var x = event.offsetX
alert(x);
});
#5
0
In percentage :
在百分比:
$('.your-class').click(function (e){
var $this = $(this); // or use $(e.target) in some cases;
var offset = $this.offset();
var width = $this.width();
var height = $this.height();
var posX = offset.left;
var posY = offset.top;
var x = e.pageX-posX;
x = parseInt(x/width*100,10);
x = x<0?0:x;
x = x>100?100:x;
var y = e.pageY-posY;
y = parseInt(y/height*100,10);
y = y<0?0:y;
y = y>100?100:y;
console.log(x+'% '+y+'%');
});
#6
0
see here enter link description here
在这里输入链接描述
html
html
<body>
<p>This is a paragraph.</p>
<div id="myPosition">
</div>
</body>
css
css
#myPosition{
background-color:red;
height:200px;
width:200px;
}
jquery
jquery
$(document).ready(function(){
$("#myPosition").click(function(e){
var elm = $(this);
var xPos = e.pageX - elm.offset().left;
var yPos = e.pageY - elm.offset().top;
alert("X position: " + xPos + ", Y position: " + yPos);
});
});