W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
在鼠標的單個動作觸發(fā)多個事件時,事件的順序是固定的,鼠標事件默認透傳。
名稱 | 支持冒泡 | 描述 |
---|---|---|
onHover(event: (isHover?: boolean) => void) | 否 | 鼠標進入或退出組件時觸發(fā)該回調(diào)。 isHover:表示鼠標是否懸浮在組件上,鼠標進入時為true, 退出時為false。 |
onMouse(event: (event?: MouseEvent) => void) | 是 | 當前組件被鼠標按鍵點擊時或者鼠標在組件上懸浮移動時,觸發(fā)該回調(diào),event返回值包含觸發(fā)事件時的時間戳、鼠標按鍵、動作、鼠標位置在整個屏幕上的坐標和相對于當前組件的坐標。 |
名稱 | 屬性類型 | 描述 |
---|---|---|
screenX | number | 鼠標位置相對于應(yīng)用窗口左上角的x軸坐標。 |
screenY | number | 鼠標位置相對于應(yīng)用窗口左上角的y軸坐標。 |
x | number | 鼠標位置相對于當前組件左上角的x軸坐標。 |
y | number | 鼠標位置相對于當前組件左上角的y軸坐標。 |
button | 鼠標按鍵。 | |
action | 鼠標動作。 | |
stopPropagation | () => void | 阻塞事件冒泡。 |
timestamp8+ | number | 事件時間戳。觸發(fā)事件時距離系統(tǒng)啟動的時間間隔,單位納秒。 |
target8+ | 觸發(fā)事件的元素對象顯示區(qū)域。 | |
source8+ | 事件輸入設(shè)備。 |
- // xxx.ets
- @Entry
- @Component
- struct MouseEventExample {
- @State hoverText: string = 'no hover';
- @State mouseText: string = '';
- @State action: string = '';
- @State mouseBtn: string = '';
- @State color: Color = Color.Blue;
- build() {
- Column({ space: 20 }) {
- Button(this.hoverText)
- .width(180).height(80)
- .backgroundColor(this.color)
- .onHover((isHover: boolean) => {
- // 通過onHover事件動態(tài)修改按鈕在是否有鼠標懸浮時的文本內(nèi)容與背景顏色
- if (isHover) {
- this.hoverText = 'hover';
- this.color = Color.Pink;
- } else {
- this.hoverText = 'no hover';
- this.color = Color.Blue;
- }
- })
- Button('onMouse')
- .width(180).height(80)
- .onMouse((event: MouseEvent) => {
- switch (event.button) {
- case MouseButton.None:
- this.mouseBtn = 'None';
- break;
- case MouseButton.Left:
- this.mouseBtn = 'Left';
- break;
- case MouseButton.Right:
- this.mouseBtn = 'Right';
- break;
- case MouseButton.Back:
- this.mouseBtn = 'Back';
- break;
- case MouseButton.Forward:
- this.mouseBtn = 'Forward';
- break;
- case MouseButton.Middle:
- this.mouseBtn = 'Middle';
- break;
- }
- switch (event.action) {
- case MouseAction.Hover:
- this.action = 'Hover';
- break;
- case MouseAction.Press:
- this.action = 'Press';
- break;
- case MouseAction.Move:
- this.action = 'Move';
- break;
- case MouseAction.Release:
- this.action = 'Release';
- break;
- }
- this.mouseText = 'onMouse:\nButton = ' + this.mouseBtn +
- '\nAction = ' + this.action + '\nXY=(' + event.x + ',' + event.y + ')' +
- '\nscreenXY=(' + event.screenX + ',' + event.screenY + ')';
- })
- Text(this.mouseText)
- }.padding({ top: 30 }).width('100%')
- }
- }
示意圖:
鼠標懸浮時改變文本內(nèi)容與背景顏色:
鼠標點擊時:
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: