openharmony 鸿蒙 js-components-common-events

2025-06-12 浏览 (1)

Universal Events

NOTE

Universal events are supported since API version 4. Updates will be marked with a superscript to indicate their earliest API version.

Event Description

  • Events are bound to components. When a component meets the event triggering condition, the corresponding event callback in the JS is executed to implement the interaction between the UI and the JS logic layer of the page.

  • The event callback can carry additional information through parameters, such as the dataset on the component and event-specific callback parameters.

Different from private events, universal events can be bound to most components.

NameParameterDescriptionBubbling SupportedCapturing Supported
touchstartTouchEventTriggered when the tapping starts. For details about TouchEvent, see Table 2.Yes5+Yes5+
touchmoveTouchEventTriggered when the tapping moves.Yes5+Yes5+
touchcancelTouchEventTriggered when the tapping is interrupted.Yes5+Yes5+
touchendTouchEventTriggered when the tapping ends.Yes5+Yes5+
clickBaseEventTriggered when a component is clicked.Yes6+No
doubleclick7+BaseEventTriggered when a component is double-clicked.No
Bubbling is supported since API version 9.
No
longpressBaseEventTriggered when a component is long pressed.No
Bubbling is supported since API version 9.
No
swipe5+SwipeEventTriggered when a user quickly swipes on a component. For details about SwipeEvent, see Table 4.No
Bubbling is supported since API version 9.
No
attached6+-Triggered after the current component node is mounted to the render tree.NoNo
detached6+-Triggered when the current component node is removed from the render tree.NoNo
pinchstart7+PinchEventTriggered when a pinch operation is started.
For details about PinchEvent, see Table 5.
NoNo
pinchupdate7+PinchEventTriggered when a pinch operation is in progress.NoNo
pinchend7+PinchEventTriggered when a pinch operation is ended.NoNo
pinchcancel7+PinchEventTriggered when a pinch operation is interrupted.NoNo
dragstart7+DragEventTriggered when dragging starts.
For details about DragEvent, see Table 6.
NoNo
drag7+DragEventTriggered when dragging is in progress.NoNo
dragend7+DragEventTriggered when dragging is ended.NoNo
dragenter7+DragEventTriggered when the dragged component enters a drop target.NoNo
dragover7+DragEventTriggered when the dragged component is being dragged over a drop target.NoNo
dragleave7+DragEventTriggered when the dragged component leaves a drop target.NoNo
drop7+DragEventTriggered when the dragged component is dropped on a drop target.NoNo

NOTE

Events not listed in the preceding table do not support bubbling, such as the change event of the <input> component. For details, see the description of the specific component.

Table 1 BaseEvent

AttributeTypeDescription
typestringEvent type, such as click and longpress.
timestampnumberTimestamp when the event is triggered.
deviceId6+numberID of the device that triggers the event.
target6+TargetTarget object that triggers the event.

Table 2 TouchEvent (inherited from BaseEvent)

AttributeTypeDescription
touchesArray<TouchInfo>Attribute set of the touch event, including the information array of the touch points on the screen.
changedTouchesArray<TouchInfo>Attribute set when a touch event occurs, including the information array of changed touch points on the screen. changedTouches has the same data format as touches and indicates touch point changes, including changes in the number and location of touch points. For example, when the user's finger leaves the screen, which means that the number of touch points changes from 1 to 0, changedTouches has the relevant data generated, but not touches.

Table 3 TouchInfo

AttributeTypeDescription
globalXnumberHorizontal distance from the upper left corner of the screen (excluding the status bar), which acts as the origin of coordinates.
globalYnumberVertical distance from the upper left corner of the screen (excluding the status bar), which acts as the origin of coordinates.
localXnumberHorizontal distance from the upper left corner of the touched component, which acts as the origin of coordinates.
localYnumberVertical distance from the upper left corner of the touched component, which acts as the origin of coordinates.
sizenumberTouch area.
force6+numberTouch force.

Table 4 SwipeEvent (inherited from BaseEvent)

AttributeTypeDescription
directionstringSwiping direction. The value can be one of the following:
- left: Swipe left.
- right: Swipe right.
- up: Swipe up.
- down: Swipe down.
distance6+numberSwiping distance in the swiping direction.

Table 5 PinchEvent7+

AttributeTypeDescription
scalenumberScale factor.
pinchCenterXnumberX-coordinate of the pinch center, in px.
pinchCenterYnumberY-coordinate of the pinch center, in px.

Table 6 DragEvent7+ (inherited from BaseEvent)

AttributeTypeDescription
typestringEvent name.
globalXnumberHorizontal distance from the upper left corner of the screen, which acts as the origin of coordinates.
globalYnumberVertical distance from the upper left corner of the screen, which acts as the origin of coordinates.
timestampnumberTimestamp.
dataTransfer9+DataTransferObject for data transfer.

Target6+

When a component triggers an event, the event callback receives an event object by default. You can obtain the corresponding information through the event object.

AttributeTypeDescription
dataSet6+ObjectCustom attribute set defined through data-*.

Example

<!-- xxx.hml -->
<div>
  <div data-a="dataA" data-b="dataB" 
    style="width: 100%; height: 50%; background-color: saddlebrown;"@touchstart='touchstartfunc'></div>
</div>
// xxx.js
export default {
  touchstartfunc(msg) {
    console.info(`on touch start, point is: ${msg.touches[0].globalX}`);
    console.info(`on touch start, data is: ${msg.target.dataSet.a}`);
  }
}

DataTransfer9+

Use dataTransfer to transfer data during the drag operation, so you can perform operations on the data when the drag operation is complete.

setData9+

setData(key: string, value: object): boolean

Sets the data associated with the specified key. If there is no data associated with the key, the data will be appended. If there is already data associated with the key, the data will replace the existing data in the same position.

Parameters

NameTypeMandatoryDescription
keystringYesData key.
valueobjectYesData to be stored.

Return value

TypeDescription
booleanOperation result. The value true means that the operation is successful, and false means the opposite.

Example

// value in setData can be of a primitive type.
dragStart(e) {
	var isSetOk = e.dataTransfer.setData('name', 1);
},
// value in setData can be of the object type.
dragStart(e) {
	var person = new Object();
	person.name = "tom";
	person.age = 21;
	var isSetOk = e.dataTransfer.setData('person', person);
}

getData9+

getData(key: string): object

Obtains the data associated with the specified key. If no data is associated with the key, an empty string will be returned.

Parameters

NameTypeMandatoryDescription
keystringYesData key.

Return value

TypeDescription
objectObtained data.

Example

dragStart(e) {
	var person = new Object();
	person.name = "tom";
	person.age = 21;
	e.dataTransfer.setData('person', person);
},
dragEnd(e){
	var person = e.dataTransfer.getData('person');
},

clearData9+

clearData(key?: string): boolean

Deletes data associated with the specified key. If there is no data associated with the key, this API will not have any effect. If the key is null, all data will be deleted.

Parameters

NameTypeMandatoryDescription
keystringNoData key.

Return value

TypeDescription
booleanOperation result. The value true means that the operation is successful, and false means the opposite.

Example

dragEnd(e) {
	var isSuccess = e.dataTransfer.clearData('name');
}

setDragImage9+

setDragImage(pixelmap: PixelMap, offsetX: number,offsetY: number): boolean

Sets a custom drag image.

Parameters

NameTypeMandatoryDescription
pixelmapPixelMapYesImage transferred from the frontend.
offsetXnumberYesHorizontal offset relative to the image.
offsetYnumberYesVertical offset relative to the image.

Return value

TypeDescription
booleanOperation result. The value true means that the operation is successful, and false means the opposite.

Example

import image from '@ohos.multimedia.image';

export default {
    createPixelMap() {
        let color = new ArrayBuffer(4 * 96 * 96);
        var buffer = new Uint8Array(color);
        for (var i = 0; i < buffer.length; i++) {
            buffer[i] = (i + 1) % 255;
        }
        let opts = {
            alphaType: 0,
            editable: true,
            pixelFormat: 4,
            scaleMode: 1,
            size: {
                height: 96, width: 96
            }
        }
        const promise = image.createPixelMap(color, opts);
        promise.then((data) => {
            console.error('-create pixmap has info message:' + JSON.stringify(data));
            this.pixelMap = data;
            this.pixelMapReader = data;
        })
    },

    onInit() {
        this.createPixelMap
    },

    dragStart(e) {
        e.dataTransfer.setDragImage(this.pixelMapReader, 50, 50);
    }
}

你可能感兴趣的鸿蒙文章

harmony 鸿蒙JavaScript-compatible Web-like Development Paradigm (ArkUI.Full)

harmony 鸿蒙Data Type Attributes

harmony 鸿蒙button

harmony 鸿蒙chart

harmony 鸿蒙divider

harmony 鸿蒙image-animator

harmony 鸿蒙image

harmony 鸿蒙input

harmony 鸿蒙label

harmony 鸿蒙marquee

  • 所属分类: 后端技术
  • 本文标签: 鸿蒙 软件
  • 版权声明: 本文链接 https://seaxiang.com/blog/rQKSqb