在答案部分发布我快速编写的代码以保持可读性。这可能会间接解决您的问题。
$(function(){
var mousex = mousey = null;
$('body').mousemove(function(ev){
if(mousex === null || mousey === null){
//get initial x/y position for mouse cursor
mousex = ev.pageX;
mousey = ev.pageY;
}
window.clearTimeout(window.mouse_timeout);
window.mouse_timeout = window.setTimeout(function(){
console.log('mouse moved', ev.pageX - mousex, ev.pageY - mousey);
/* now that you've the absolute position of the mouse and delta x/y here, you can move your background accordingly */
//update the variables to the latest picket position of the mouse
mousex = ev.pageX;
mousey = ev.pageY;
}, 10)
});
});
更新
jsfiddle此代码将初始鼠标位置标记为中心参考点,并稍后参考该点计算鼠标移动。
标记
<div id="outer_container">
<div id="imagePan">
<div class="container">
<div id="mov">
<img src="http://i.imgur.com/92Z5zCM.jpg" class="panning" />
</div>
</div>
</div>
</div>
CSS
html, body{ height: 100%; }
body{ margin: 0; padding: 0; background: #eee; }
#outer_container{ position: relative; margin: auto; padding: 4px; border: 8px solid #dadada; height: 90%; width: 80%; background: #CDD7E0}
#imagePan{ position: relative; overflow: hidden; cursor: crosshair; height: 100%; width: 100%; }
#imagePan .container{ position: relative; left: 0; }
#mov{ margin: -51px -391px 0 }
Javascript
var originX = originY = xUnit = yUnit = dX = dY = mov = null;
$(window).load(function () {
mov = $('#mov');
initMarginL = parseInt(mov.css('margin-left'));
initMarginT = parseInt(mov.css('margin-top'));
$('#outer_container').mousemove(function (ev) {
if (originX === null || originY === null) {
originX = ev.pageX;
originY = ev.pageY;
xUnit = parseFloat((Math.max(originX, $(window).width()) / $(window).width()).toFixed(2));
yUnit = parseFloat((Math.max(originY, $(window).height()) / $(window).height()).toFixed(2));
}
var dX = originX - ev.pageX;
var dY = originY - ev.pageY;
mov.css({
marginLeft: initMarginL + (dX * xUnit),
marginTop: initMarginT + (dY * yUnit)
});
});
});