看看这个小提琴,看看这是否接近你正在寻找的东西。
http://jsfiddle.net/cF6fE/2/
var posx = 0;
var posy = 0;
window.onload = function() {
document.getElementById('cursor').addEventListener("mousedown", mousePos, false);
}
function mousePos(e) {
posx = e.clientX;
posy = e.clientY;
document.getElementById('cursor').addEventListener("mousemove", mousemoveCalc);
}
function mousemoveCalc(e) {
console.log(e);
var x = e.clientX;
var y = e.clientY;
var xOffset = posx-x;
var yOffset = posy-y;
console.log(x);
console.log(y);
console.log(xOffset);
console.log(yOffset);
}
http://jsfiddle.net/uGuHk/
function mousePos(e) {
var posx = e.clientX;
var posy = e.clientY;
document.getElementById('cursor').addEventListener("mousemove", mousemoveCalc.bind(this, posx,posy));
}
function mousemoveCalc(xax,yax,e) {
console.log(e);
var x = e.clientX;
var y = e.clientY;
var xOffset = xax-x;
var yOffset = yax-y;
console.log(x); //undefined
console.log(y); //undefined
console.log(xOffset); //undefined
console.log(yOffset); //undefined
}
更新2:-关闭
http://jsfiddle.net/FG6uW/
function mousePos(e) {
var posx = e.clientX;
var posy = e.clientY;
document.getElementById('cursor').addEventListener("mousemove", function() {
(function(x, y) {
mousemoveCalc(x, y)
})(posx, posy)
});
}
function mousemoveCalc(xax, yax) {
var x = event.clientX;
var y = event.clientY;
var xOffset = xax - x;
var yOffset = yax - y;
console.log(xax);
console.log(yax);
document.getElementById('cursor').innerText = xax + '-' + yax + '|' + xOffset + '-' + yOffset;
console.log(xOffset);
console.log(yOffset);
}