简体   繁体   English

自动更新Javascript

[英]Auto-Update Javascript

I want a div to move when the mouse is within x distance from it. 当鼠标距离它的x距离时,我想要一个div移动。

$(document).mousemove(function(e) {  
        mX = e.pageX;
        mY = e.pageY;

        distance = calculateDistance(element, mX, mY);

        if (distance<100) {
            $(element).css("left", moveAcc + "px");
            moveAcc = moveAcc + 1;
        }         
    });

JSFiddle - You have to select jquery 1.11.0 library JSFiddle - 您必须选择jquery 1.11.0库

But because the condition is inside the mousemove function , it can only move the element when the mouse is being moved too, as opposed to moving when mouse is within x distance. 但是因为条件在mousemove函数内 ,它只能在移动鼠标时移动元素, 而不是在鼠标在x距离内时移动。

For this I need sort of an update thingy, something to check if conditions are met every frame. 为此,我需要一些更新东西,检查每帧是否满足条件。

Example, in Pascal for my projects I'd put the whole code in between the repeat until () 例如,在我的项目的Pascal中,我将整个代码放在重复之间直到()

In ActionScript there's addEventListener(Event.ENTER_FRAME, function); 在ActionScript中有addEventListener(Event.ENTER_FRAME,function);

What can I use in Javascript? 我可以在Javascript中使用什么? What other ways would you come up with? 你会想出什么其他方法? - I did found this Javascript Version of AS3 enterframe event but it is 4 years old, migh be different now. - 我确实找到了这个AS3 enterframe活动的Javascript版本,但是它已经4年了,现在差不多了。

Is it ok to put the code inside setInterval function? 是否可以将代码放在setInterval函数中?

You're looking for the requestAnimationFrame method. 您正在寻找requestAnimationFrame方法。

What it does ? 它能做什么 ? It'll execute the method passed on it call before the next frame get rendered. 在下一帧渲染之前,它将执行传递给它的方法。 But beware, not all browsers support it, you can use a polyfill for it or just an setInterval 但要注意,并非所有浏览器都支持它,您可以使用polyfill或仅使用setInterval

Example: 例:

function checkDistance () {
    \\ Do your job here
    window.requestAnimationFrame(checkDistance);
}
window.requestAnimationFrame(checkDistance);

More info: 更多信息:

https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame

You have two ways to do that: 您有两种方法可以做到这一点:

  1. Add an event listener on the document object and measure the element positions and the mouse positions, then check if the current mouse position is between those positions. 在文档对象上添加事件侦听器并测量元素位置和鼠标位置,然后检查当前鼠标位置是否在这些位置之间。 (This way not suggested if you have just one element) (如果你只有一个元素,这种方式不建议)
  2. Add an event listener on the element directly and check if the current mouse position is exactly the same position you want. 直接在元素上添加一个事件监听器,并检查当前鼠标位置是否与您想要的位置完全相同。

If you have a well written mouse event listener, then you don't need to update the function using interval. 如果你有一个编写良好的鼠标事件监听器,那么你不需要使用interval更新函数。

I would try declaring your mouse X and Y variables outside the mousemove method, that way even when your mouse stops moving, the last position (current really) is still accessible.. 我会尝试在mousemove方法之外声明你的鼠标X和Y变量,这样即使你的鼠标停止移动,最后一个位置(当前真正的)仍然可以访问..

var mX;
var mY;
$(document).mousemove( function(e) {
    mX = e.pageX; 
    mY = e.pageY;
});

Then, you can have a second method on an interval that can check against the values in those variables, and move the box accordingly.. 然后,您可以在间隔上使用第二种方法来检查这些变量中的值,并相应地移动框。

setInterval(function(){

    // Code to calculate distance etc.

    distance = calculateDistance(element, mX, mY);

    if (distance<100) {
        $(element).css("left", moveAcc + "px");
        moveAcc = moveAcc + 1;
    }   

},100); // Whatever interval you see fit instead of 100ms

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

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