简体   繁体   English

jquery 鼠标移动捕捉不准确

[英]jquery mouse move capture inaccuracy

I'm facing an strange problem.我面临一个奇怪的问题。 I capture the mouse movements with:我通过以下方式捕捉鼠标移动:

var mmoves = [];
jQuery(document).mousemove(function(event) {
   mmoves.push({x:event.pageX, y:event.pageY})
}

Then I attach a div to the page like:然后我将一个 div 附加到页面上,例如:

$("body").append('<div id="mouseemul" style="padding:0; margin:0; color: red; background-color: blue; width: 1px; height: 1px;">*</div>');

and then try to playback the moves然后尝试播放动作

It works ok on most pages but on some pages the playback starts ("*" initial position) some pixels to the right (x).它在大多数页面上都可以正常工作,但在某些页面上播放开始(“*”初始位置)右侧(x)一些像素。 The y is ok but the x is about 120px to the right. y 没问题,但 x 向右大约 120 像素。 On other pages it is accurate.在其他页面上是准确的。 On the not accurate pages, when the mouse goes close the right scrollbar it goes beyond the right page border and produces a horizontal scrollbar.在不准确的页面上,当鼠标靠近右侧滚动条时,它会超出右侧页面边框并产生水平滚动条。

I think this has to do with some css styling of the page being playback.我认为这与正在播放的页面的一些 css 样式有关。

Does anybody has an idea what may be causing this?有人知道是什么原因造成的吗? How could I get the actual offset (in case there is an offset for such pages)?我怎样才能得到实际的偏移量(如果这些页面有偏移量)?

Thanks a lot,非常感谢,

Hernan埃尔南

--Edited-- It is obvious that the x displacement is due to the positioning of the main document. --Edited-- 很明显x位移是由于主文档的定位造成的。 The first element gives a $.position() of 0,134 and if I SUBSTRACT that amount from the recorded data the playback is accurate.第一个元素给出的 $.position() 为 0,134,如果我从记录的数据中减去该数量,则播放是准确的。 The problem is that this displacement does not happen in every page and I dont know how to figure out when the displacement occurs and when not (to correct it by substracting).问题是这种位移并不是在每一页都发生,我不知道如何确定位移何时发生,何时不发生(通过减法来纠正)。

Recording记录

If you want to capture and replay mouse movement you can try "recording" from the document .如果您想捕捉和回放鼠标移动,您可以尝试从document中“录制”。 This would use the x and y chords from the window.这将使用 window 中的 x 和 y 和弦。

To do this you can use the document DOM element:为此,您可以使用文档 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 });

});

Replaying重播

HTML/CSS HTML/CSS

Your HTML/CSS should be a div on the page set with position: fixed which should match your javascript chord samples as fixed is absolutely positioned to the window:您的 HTML/CSS 应该是设置为position: fixed应该与您的 javascript 和弦样本匹配,因为fixed绝对定位到 window:

<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>

Javascript Javascript

To replay your captured chords you can use something like this:要重播您捕获的和弦,您可以使用以下内容:

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();

Demo演示

Try it out on this demo .试试这个演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM