概述
鼠标事件问题
See more on JavaScript events
进一步了解JavaScript事件
When looking at mouse events we have the ability to interact with
在查看鼠标事件时,我们可以与之交互
mousedown
the mouse button was pressedmousedown
鼠标按钮mouseup
the mouse button was releasedmouseup
释放了鼠标按钮click
a click eventclick
点击事件dblclick
a double click eventdblclick
双击事件mousemove
when the mouse is moved over the element将鼠标移到元素上时
mousemove
mouseover
when the mouse is moved over an element or one of its child elements将鼠标移到一个元素或其子元素之一上时的
mouseover
mouseenter
when the mouse is moved over an element. Similar tomouseover
but does not bubble (more on this soon!)将鼠标移到元素上时的
mouseenter
。 与mouseover
类似,但不会冒泡(稍后会更多!)mouseout
when the mouse is moved out of an element, and when the mouse enters a child elements将鼠标移出元素时,以及进入子元素时的
mouseout
mouseleave
when the mouse is moved out of an element. Similar tomouseout
but does not bubble (more on this soon!)将鼠标移出元素时的
mouseleave
。 与mouseout
类似,但不会冒泡(稍后会更多!)contextmenu
when the context menu is opened, e.g. on a right mouse button clickcontextmenu
时上下文菜单打开时,如在点击鼠标右键
Events overlap. When you track a click
event, it’s like tracking a mousedown
followed by a mouseup
event. In the case of dblclick
, click
is also fired two times.
事件重叠。 跟踪click
事件时,就像跟踪mousedown
事件和mouseup
事件一样。 在dblclick
的情况下, click
也被触发两次。
mousedown
, mousemove
and mouseup
can be used in combination to track drag-and-drop events.
mousedown
, mousemove
和mouseup
可以组合使用来跟踪拖放事件。
Be careful with mousemove
, as it fires many times during the mouse movement. We need to apply throttling, which is something we’ll talk more when we’ll analyze scrolling.
小心mousemove
,因为它在鼠标移动期间会触发多次。 我们需要应用节流 ,这是我们在分析滚动时将要讨论的更多内容。
When inside an eventh handler we have access to lots of properties.
在一个偶数处理程序中,我们可以访问许多属性。
For example on a mouse event we can check which mouse button was pressed by checking the button
property of the event object:
例如,在鼠标事件中,我们可以通过检查事件对象的button
属性来检查按下了哪个鼠标按钮:
const link = document.getElementById('my-link')
link.addEventListener('mousedown', event => {
// mouse button pressed
console.log(event.button) //0=left, 2=right
})
Here are all the properties we can use:
这是我们可以使用的所有属性:
altKey
true if alt key was pressed when the event was firedaltKey
如果触发事件时按下alt键,altKey
truebutton
if any, the number of the button that was pressed when the mouse event was fired (usually 0 = main button, 1 = middle button, 2 = right button). Works on events caused by clicking the button (e.g. clicks)如果有
button
则为触发鼠标事件时按下的按钮编号(通常为0 =主按钮,1 =中间按钮,2 =右键)。 对通过单击按钮引起的事件起作用(例如,单击)buttons
if any, a number indicating the button(s) pressed on any mouse event.buttons
如果有),数字表示在任何鼠标事件中按下的按钮。clientX
/clientY
the x and y coordinates of the mouse pointer relative to the browser window, regardless of scrollingclientX
/clientY
鼠标指针相对于浏览器窗口的x和y坐标,与滚动无关ctrlKey
true if ctrl key was pressed when the event was firedctrlKey
如果触发事件时按下ctrl键,则为truemetaKey
true if meta key was pressed when the event was firedmetaKey
如果触发事件时按下了meta键,metaKey
truemovementX
/movementY
the x and y coordinates of the mouse pointer relative to the position of the last mousemove event. Used to track the mouse velocity while moving it aroundmovementX
/movementY
x和相对于最后mousemove事件的位置的鼠标指针的y坐标。 用于在移动鼠标时跟踪鼠标速度region
used in the Canvas APICanvas API中使用的
region
relatedTarget
the secondary target for the event, for example when movingrelatedTarget
事件的次要目标,例如在移动时screenX
/screenY
the x and y coordinates of the mouse pointer in the screen coordinatesscreenX
/screenY
屏幕坐标中鼠标指针的x和y坐标shiftKey
true if shift key was pressed when the event was firedshiftKey
如果触发事件时按下Shift键,则为true
翻译自: https://flaviocopes.com/mouse-events/
鼠标事件问题
最后
以上就是贤惠百褶裙为你收集整理的鼠标事件问题_鼠标事件的全部内容,希望文章能够帮你解决鼠标事件问题_鼠标事件所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复