Webkit firing mousemove event unexpectedly (mouse not moving)

▼魔方 西西 提交于 2019-12-11 07:27:12

问题


I have a slideshow playing that scrolls a div to reveal the next photo in the slideshow. I also have a function set up that hides the photo description when the mouse is inactive, but shows the description when the mouse moves.

In Firefox, there is no problem, the div scrolls to new photos and no mousemove event is fired. However, in Webkit with the mouse on the window, but inactive, two-three mousemove events are fired everytime the div scrolls to a new photo.

Here's the website for you to have a look. Navigate to the portfolio page in a webkit browser (with a console open I suppose) and that footer should stay hidden while the photos cycle through. http://96.0.13.132/


回答1:


Yes, webkit browsers do that, and i think every browser should. Because the cursor IS in a different position after the scrolling, and that could avoid me a lot of problems as a developer.

Anyway, if you want to avoid its consequences in your script, just log the latest clientX and clientY position and check if those have changed since the last "mousemove"; something like this:

window.addEventListener("mousemove",function(e){
    if(window.lastX !== e.clientX || window.lastY !== e.clientY){
        // Code when the (physical) mouse actually moves
    }   
    window.lastX = e.clientX
    window.lastY = e.clientY
})


来源:https://stackoverflow.com/questions/8761730/webkit-firing-mousemove-event-unexpectedly-mouse-not-moving

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!