参考 mouseWheel()

mouseWheel()

当鼠标滚轮移动时调用一次的函数。

声明函数 mouseWheel() 将设置一个代码块,当用户使用鼠标滚轮滚动时自动运行:

function mouseWheel() { // Code to run. }

鼠标系统变量,如 mouseXmouseY,将在调用 mouseWheel() 时更新为最新的值:

function mouseWheel() { if (mouseX < 50) { // Code to run if the mouse is on the left. }

if (mouseY > 50) { // Code to run if the mouse is near the bottom. } }

参数 event 是可选的。mouseWheel() 总是传递一个 MouseEvent 对象,该对象带有描述鼠标滚轮事件的属性:

function mouseWheel(event) { // Code to run that uses the event. console.log(event); }

event 对象有许多属性,包括 delta,一个 Number,包含用户滚动的距离。例如,当用户向上滚动时,event.delta 可能有值 5。event.delta 如果用户向上滚动则为正,向下滚动则为负。在 macOS 上启用“自然”滚动时,符号相反。

浏览器可能对各种鼠标事件附加默认行为。例如,一些浏览器在用户按下鼠标按钮时移动鼠标会突出显示文本。为了防止此事件的任何默认行为,添加 return false; 到函数的末尾。

注意:在 Safari 上,mouseWheel() 可能只有在函数末尾添加 return false; 时才能按预期工作。

示例

语法

mouseWheel([event])

参数

event
WheelEvent:

optional WheelEvent argument.

Notice any errors or typos? Please let us know. Please feel free to edit src/events/mouse.js and open a pull request!

相关参考