是否有任何类似“onmousestop”的javascript / jQuery事件?或任何相当于“onmousewheel”的事件用于光学鼠标和/或触摸板? [英] Are there any javascript/jQuery events that are like an "onmousestop" or any events equivalent to "onmousewheel" for an optical mouse and/or touchpad?

查看:147
本文介绍了是否有任何类似“onmousestop”的javascript / jQuery事件?或任何相当于“onmousewheel”的事件用于光学鼠标和/或触摸板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的网站创建基于Javascript的非活动超时。控制不活动的功能非常简单;记录初始时间的表格值随任何事件同时重置(假设任何事件应指示活动):

I am trying to create a Javascript-based inactivity timeout for my site. The function which controls inactivity is very simple; a form value recording the "initial time" is reset concomitantly with any event (the assumption being any event should be indicative of activity):

function ResetTimeout() {
    document.forms.TimeoutForm.initialtime.value = (new Date().getTime()) / 1000;
}//This function is called on each event, such as an onclick

单独的函数,当新的Date()。getTime()(即此时的时间) - document.forms.TimeoutForm.initialtime.value(以毫秒为单位)> 720000(12分钟)时,提交一个隐藏的注销表单。现在,我将这些事件绑定到< body> ,如下所示:

In a separate function, when new Date().getTime() (that is, the time at this moment) - document.forms.TimeoutForm.initialtime.value (in milliseconds) > 720000 (12 min), a hidden logout form is submitted. Right now, I am binding these events to the <body> as follows:

<body onclick="ResetTimeout()" onkeydown="ResetTimeout()" onscroll="ResetTimeout()">

几乎适用于所有情况,因为我网站上的每个页面都需要一个垂直滚动条才能看到底部的页面。在屏幕上的任何位置也可以捕获任何鼠标或触摸板按下。然而,两个有效的异常值将是(a)从不向下滚动的用户,并且从不点击鼠标/触摸板也不按任何键但仍以某种方式参与网站和(b)用户(不太可能但可能)向左/向右移动鼠标但从不滚动,单击,也不按任何键。这两个用户在技术上都会在网站上处于活动状态,但我的代码会被视为非活动状态。

This works for almost all cases, since each page on my site requires a vertical scrollbar to see the bottom of the page. Any mouse or touchpad press is also captured, anywhere on screen. However, two valid outliers would be for (a) the user that never scrolls down, and never clicks the mouse/touchpad nor presses any keys but is still somehow engaged on the site and (b) the user that is (unlikely but possible) moving the mouse left/right but never scrolling, clicking, nor pressing any keys. Both of these users would technically be active on the site but would be considered inactive by my code.

我尝试通过绑定来计算这些用户的比例onmouseover onmousemove 事件到< body> ,但这些事件会不断触发即使你根本没有移动光标。我找到了HTML5的 onmousewheel ,但这对光学鼠标和触摸板都不起作用(函数永远不会触发,因此即使将光标移动到整个屏幕上也不会记录事件)。

I tried accounting for these users by binding the onmouseover or onmousemove events to the <body>, but these events are continuously triggered even if you do not move the cursor at all. I found HTML5's onmousewheel, but this does not work for an optical mouse nor touchpad (function never fires so event is never recorded even as you move the cursor all over the screen).

只有当用户将鼠标移动到屏幕上的任何位置时(并且不保持静止),是否在jQuery或Javascript中记录事件?也就是说,仅在xcoord2-xcoord1(或ycoord相同)时才触发的事件在某个时间间隔内不等于零?我的直觉是这可能很复杂,可能需要代码以不同的时间间隔记录x或y坐标然后进行比较。或者,是否有任何类似 onmousestop 的事件(动态光标停止移动)?我读到了onwheel(点击这里)但我需要一些东西适用于所有浏览器有什么建议?谢谢

Is there an event in jQuery or Javascript that is recorded only whenever a user MOVES the mouse anywhere on the screen (and does not remain motionless)? That is, an event which fires only when xcoord2 - xcoord1 (or same for ycoord) does not equal zero over some time interval? My gut feeling is this may be complex and may require the code to record x or y coordinates at different time intervals and then compare. Alternatively, is there any event that would be something like onmousestop (a cursor-in-motion stops moving)? I read about "onwheel" (click here) but I need something for all browsers. Any suggestions? Thanks

推荐答案

mousemove 事件


mousemove事件是当指针设备(通常是鼠标)在元素上移动时触发

The mousemove event is fired when a pointing device (usually a mouse) is moved while over an element

所以简单地

var timer;

$(document).on('mousemove', function() {
    clearTimeout(timer);

    timer = setTimeout(function() {
        logout();
    }, 720000);
});

FIDDLE

这篇关于是否有任何类似“onmousestop”的javascript / jQuery事件?或任何相当于“onmousewheel”的事件用于光学鼠标和/或触摸板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆