Her*_* M. 7 javascript jquery dom
我面临一个奇怪的问题.我用以下方法捕捉鼠标移动:
var mmoves = [];
jQuery(document).mousemove(function(event) {
mmoves.push({x:event.pageX, y:event.pageY})
}
Run Code Online (Sandbox Code Playgroud)
然后我将div附加到页面上,如:
$("body").append('<div id="mouseemul" style="padding:0; margin:0; color: red; background-color: blue; width: 1px; height: 1px;">*</div>');
Run Code Online (Sandbox Code Playgroud)
然后尝试播放动作
它在大多数页面上都可以正常工作,但在某些页面上,播放开始("*"初始位置)一些像素向右(x).y是可以的,但x在右边约120px.在其他页面上它是准确的.在不准确的页面上,当鼠标靠近右侧滚动条时,它会超出右侧页边框并生成水平滚动条.
我认为这与正在播放的页面的某些CSS样式有关.
有没有人知道可能导致这种情况的原因?我怎样才能得到实际的偏移量(如果这些页面有偏移量)?
非常感谢,
埃尔南
- 编辑 - 显然x位移是由于主文档的定位.第一个元素给出$ .position()为0,134,如果我从记录的数据中取消该量,则回放是准确的.问题是这个位移不会发生在每个页面中,我不知道如何找出何时发生位移,何时不知道(通过减去来纠正它).
如果您想捕获并重播鼠标移动,您可以尝试从document. 这将使用窗口中的 x 和 y 和弦。
为此,您可以使用文档 DOM 元素:
var m = [];
// Using the document instead of body might solve your issue
$( document ).mousemove(function( e ){
m.push({ x : e.pageX, y : e.pageY });
});
Run Code Online (Sandbox Code Playgroud)
您的 HTML/CSS 应该是页面集上的一个 div,position: fixed它应该与您的 javascript 和弦示例相匹配,因为fixed它绝对定位于窗口:
<style>
.replay {
/* Use position fixed to match window chords */
position: fixed;
top: 0;
left: 0;
/* These are just for show */
border-radius: 20px;
background: red;
width: 10px;
height: 10px;
}
</style>
<div class="replay"></div>
Run Code Online (Sandbox Code Playgroud)
要重放捕获的和弦,您可以使用如下命令:
var $replay = $('.replay'), // Get mouse simulator
i = 0, l = m.length,
pos, t;
// Recursive animation function
function anim(){
// Cache current position
pos = m[i];
// Move to next position
$replay.css({ top: pos.y, left: pos.x });
i++;
// Exit recursive loop
if ( i === l )
clearTimeout( t );
// Or keep going
else
t = setTimeout(anim, 100); // Timeout speed controls animation speed
}
// Start animation loop
anim();
Run Code Online (Sandbox Code Playgroud)
尝试一下这个demo。
| 归档时间: |
|
| 查看次数: |
1213 次 |
| 最近记录: |