jQuery代码来获得鼠标的位置下面
jQuery(document).ready(function(){
$(document).mousemove(function(e){
$('#status').html(e.pageX +', '+ e.pageY);
});
})
显然,你必须叫格“状态”
<div id="status">0, 0</div>
要检查是否光标移动到左边或右边你是对的,你必须存储以前的位置,然后与新的比较。
在这里,我给你写了完整的示例:
http://jsfiddle.net/cB9Wq/
_编辑:
如果你需要让你也需要知道的位置的分区内COORDS在div:
$(".div_container").mousemove(function(e){
var relativeXPosition = (e.pageX - this.offsetLeft); //offset -> method allows you to retrieve the current position of an element 'relative' to the document
var relativeYPosition = (e.pageY - this.offsetTop);
$("#header").html("<p><strong>X-Position: </strong>"+relativeXPosition+" | <strong>Y-Position: </strong>"+relativeYPosition+"</p>")
}).mouseout(function(){
$("#header").html("<p>Move mouse on the below blue div container! :)</p>")
});
要检查鼠标变为向左或向右,我用这个sintax:
xPrev<e.pageX ? $('#lr').html("right") : $('#lr').html("left");
xPrev=e.pageX;
注:这是等同于:
if(xPrev<e.pageX) {
$('#lr').html("right");
}
else {
$('#lr').html("left");
}
xPrev=e.pageX;
在这里,你有工作示例:http://jsfiddle.net/cB9Wq/2/
参见HTTP://小号tackoverflow.com/a/2725963/287948 –