gpt4 book ai didi

javascript - 如何使用 JavaScript 计算鼠标的特定移动

转载 作者:行者123 更新时间:2023-11-30 18:09:38 25 4
gpt4 key购买 nike

我想知道如何计算鼠标的特定移动。

我想获得一些如何确定鼠标从 A 移动到 B 的建议。

比如像win8。当鼠标在窗口边上时,再往下拉,会出现侧边栏。

$(window).on('mousemove' function(e){
if(e.pageX is on area of or close to the side of window){
// how can I calculate if the mouse Y is from a point to a point??
if(Y is moved from A to B){
//do something
}
}
})

最佳答案

您可以简单地通过检查其宽度来确定 window 的左侧:

if(e.pageX >= ($(window).width() - 20))

将检查鼠标是否在窗口右侧的 20px 范围内。

要检查它移动了多远,您需要以某种方式记录最后已知的位置,然后进行比较。因此,例如,您可能会这样做:

var last_pos = { x: false, y: false },
coord_check = $(window).width() - 20; // or whatever value from the right you want to check.

$(window).on('mousemove' function(e) {

if(e.pageX >= coord_check)
{
// If they're null, we can't do anything:
if((last_pos.x !== false && last_pos.y !== false) && ((e.pageX - last_pos.x) > 20)) {
// you can access the current position through e.pageX and e.pageY
// last_post.x and last_pos.y will tell you the last known position
}
}

// Now we need to update the last position:
last_pos.x = e.pageX;
last_pos.y = e.pageY;

});

关于javascript - 如何使用 JavaScript 计算鼠标的特定移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14924964/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com