ts-universal-events-click.md 2.6 KB
Newer Older
Z
zengyawen 已提交
1
# Click Event
Z
zengyawen 已提交
2 3


E
ester.zhou 已提交
4
> **NOTE**
E
ester.zhou 已提交
5
>
E
ester.zhou 已提交
6
> This event is supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version.
Z
zengyawen 已提交
7 8 9


## Required Permissions
Z
zengyawen 已提交
10 11 12

None

Z
zengyawen 已提交
13 14 15

## Events

E
ester.zhou 已提交
16
| Name | Bubbling Supported | Description |
Z
zengyawen 已提交
17
| -------- | -------- | -------- |
H
HelloCrease 已提交
18
| onClick(callback: (event?: ClickEvent) => void) | No | Called when a click event occurs. For details about the event parameters, see [ClickEvent](#clickevent). |
Z
zengyawen 已提交
19 20


E
ester.zhou 已提交
21
## ClickEvent
Z
zengyawen 已提交
22

H
HelloCrease 已提交
23
| Name | Type | Description |
Z
zengyawen 已提交
24
| -------- | -------- | -------- |
H
HelloCrease 已提交
25 26 27 28
| screenX | number | X coordinate of the click relative to the left edge of the screen. |
| screenY | number | Y coordinate of the click relative to the upper edge of the screen. |
| x | number | X coordinate of the click relative to the left edge of the component being clicked. |
| y | number | Y coordinate of the click relative to the upper edge of the component being clicked. |
E
ester.zhou 已提交
29
| target<sup>8+</sup> | [EventTarget](#eventtarget8) | Target element that is clicked. |
H
HelloCrease 已提交
30
| timestamp | number | Timestamp of the event. |
Z
zengyawen 已提交
31

E
ester.zhou 已提交
32
## EventTarget<sup>8+</sup>
33

E
ester.zhou 已提交
34 35 36
| Name | Type | Description |
| -------- | -------- | -------- |
| area | [Area](#area8) | Area information of the target element.|
37

E
ester.zhou 已提交
38
## Area<sup>8+</sup>
39

E
ester.zhou 已提交
40 41 42 43 44 45
| Name | Type | Description |
| -------- | -------- | -------- |
| width | number | Width of the target element, in vp. |
| height | number | Height of the target element, in vp. |
| position | [Position](#position8) | Position of the upper left corner of the target element relative to that of the parent element. |
| globalPosition | [Position](#position8) | Position of the upper left corner of the target element relative to that of the page. |
46

E
ester.zhou 已提交
47
## Position<sup>8+</sup>
48

E
ester.zhou 已提交
49 50 51 52
| Name | Type | Description |
| -------- | -------- | -------- |
| x | number | X-coordinate, in vp. |
| y | number | Y-coordinate, in vp. |
53

Z
zengyawen 已提交
54 55 56

## Example

Z
zengyawen 已提交
57

E
ester.zhou 已提交
58 59
```ts
// xxx.ets
Z
zengyawen 已提交
60 61 62 63 64 65 66
@Entry
@Component
struct ClickExample {
  @State text: string = ''

  build() {
    Column() {
Z
zengyawen 已提交
67
      Button('Click').backgroundColor(0x2788D9).width(100).height(40)
Z
zengyawen 已提交
68
        .onClick((event: ClickEvent) => {
Z
zengyawen 已提交
69
          console.info(this.text = 'Click Point:' + '\n  screenX:' + event.screenX + '\n  screenY:' + event.screenY
Z
zengyawen 已提交
70
          + '\n  x:' + event.x + '\n  y:' + event.y + '\ntarget:' + '\n  component globalPos:('
E
ester.zhou 已提交
71
          + event.target.area.globalPosition.x + ',' + event.target.area.globalPosition.y + ')\n  width:'
Z
zengyawen 已提交
72
          + event.target.area.width + '\n  height:' + event.target.area.height)
Z
zengyawen 已提交
73 74
        })
      Text(this.text).padding(15)
Z
zengyawen 已提交
75
    }.height(350).width('100%').padding(10)
Z
zengyawen 已提交
76 77 78 79 80
  }
}
```


Z
zengyawen 已提交
81
![en-us_image_0000001256858383](figures/en-us_image_0000001256858383.gif)