js-apis-mediaquery.md 4.7 KB
Newer Older
W
wusongqing 已提交
1 2
# Media Query

W
wusongqing 已提交
3 4
> **NOTE**
>
W
wusongqing 已提交
5 6 7 8 9
> The APIs of this module are supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version.


## Modules to Import

W
wusongqing 已提交
10
```js
W
wusongqing 已提交
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
import mediaquery from '@ohos.mediaquery'
```


## Required Permissions

None.


## mediaquery.matchMediaSync

matchMediaSync(condition: string): MediaQueryListener

Sets the media query criteria and returns the corresponding listening handle.

W
wusongqing 已提交
26 27
**System capability**: SystemCapability.ArkUI.ArkUI.Full

W
wusongqing 已提交
28
**Parameters**
W
wusongqing 已提交
29 30 31
| Name      | Type    | Mandatory  | Description                                      |
| --------- | ------ | ---- | ---------------------------------------- |
| condition | string | Yes   | Matching condition of a media event. For details, see "Syntax of Media Query Conditions" in [Media Query](../../ui/ui-ts-layout-mediaquery.md). |
W
wusongqing 已提交
32

W
wusongqing 已提交
33 34 35
**Return value**
| Type                | Description                    |
| ------------------ | ---------------------- |
W
wusongqing 已提交
36
| MediaQueryListener | Listening handle to a media event, which is used to register or deregister the listening callback.|
W
wusongqing 已提交
37

W
wusongqing 已提交
38
**Example**
W
wusongqing 已提交
39

W
wusongqing 已提交
40 41
  ```js
listener = mediaquery.matchMediaSync('(orientation: landscape)'); // Listen for landscape events.
W
wusongqing 已提交
42 43 44 45 46 47 48
  ```


## MediaQueryListener

Media query handle, including the first query result when the handle is applied for.

W
wusongqing 已提交
49
**System capability**: SystemCapability.ArkUI.ArkUI.Full
W
wusongqing 已提交
50 51 52

### Attributes

W
wusongqing 已提交
53 54 55 56
| Name     | Type   | Readable  | Writable  | Description        |
| ------- | ------- | ---- | ---- | ---------- |
| matches | boolean | Yes   | No   | Whether the match condition is met. |
| media   | string  | Yes   | No   | Matching condition of a media event.|
W
wusongqing 已提交
57 58 59 60 61 62 63 64


### on

on(type: 'change', callback: Callback<MediaQueryResult>): void

Registers a callback with the corresponding query condition by using the handle. This callback is triggered when the media attributes change.

W
wusongqing 已提交
65 66
**System capability**: SystemCapability.ArkUI.ArkUI.Full

W
wusongqing 已提交
67 68 69 70 71
**Parameters**
| Name     | Type                              | Mandatory  | Description              |
| -------- | -------------------------------- | ---- | ---------------- |
| type     | string                           | Yes   | Must enter the string **change**.|
| callback | Callback<MediaQueryResult> | Yes   | Callback registered with media query.      |
W
wusongqing 已提交
72

W
wusongqing 已提交
73
**Example**
W
wusongqing 已提交
74 75 76 77 78 79 80
  For details, see [off Example](#off).


### off

off(type: 'change', callback?: Callback<MediaQueryResult>): void

W
wusongqing 已提交
81 82 83 84
Deregisters a callback with the corresponding query condition by using the handle, so that no callback is triggered when the media attributes change.

**System capability**: SystemCapability.ArkUI.ArkUI.Full

W
wusongqing 已提交
85 86 87 88
**Parameters**
| Name     | Type                              | Mandatory  | Description                           |
| -------- | -------------------------------- | ---- | ----------------------------- |
| type     | boolean                          | Yes   | Must enter the string **change**.             |
W
wusongqing 已提交
89
| callback | Callback<MediaQueryResult> | No   | Callback to be deregistered. If the default value is used, all callbacks of the handle are deregistered.|
W
wusongqing 已提交
90 91 92

**Example**
  ```js
W
wusongqing 已提交
93 94 95 96 97 98 99 100 101 102
    import mediaquery from '@ohos.mediaquery'
    
    listener = mediaquery.matchMediaSync('(orientation: landscape)'); // Listen for landscape events.
    function onPortrait(mediaQueryResult) {
        if (mediaQueryResult.matches) {
            // do something here
        } else {
            // do something here
        }
    }
W
wusongqing 已提交
103
    listener.on('change', onPortrait) // Register a callback.
W
wusongqing 已提交
104
    listener.off('change', onPortrait) // Deregister a callback.
W
wusongqing 已提交
105 106 107 108 109 110 111 112
  ```


## MediaQueryResult


### Attributes

W
wusongqing 已提交
113 114 115 116
| Name     | Type   | Readable  | Writable  | Description        |
| ------- | ------- | ---- | ---- | ---------- |
| matches | boolean | Yes   | No   | Whether the match condition is met. |
| media   | string  | Yes   | No   | Matching condition of a media event.|
W
wusongqing 已提交
117 118 119 120


### Example

W
wusongqing 已提交
121
```js
W
wusongqing 已提交
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
import mediaquery from '@ohos.mediaquery'

let portraitFunc = null

@Entry
@Component
struct MediaQueryExample {
  @State color: string = '#DB7093'
  @State text: string = 'Portrait'
  listener = mediaquery.matchMediaSync('(orientation: landscape)')

  onPortrait(mediaQueryResult) {
    if (mediaQueryResult.matches) {
      this.color = '#FFD700'
      this.text = 'Landscape'
    } else {
      this.color = '#DB7093'
      this.text = 'Portrait'
    }
  }

  aboutToAppear() {
W
wusongqing 已提交
144
    portraitFunc = this.onPortrait.bind(this) // Bind the current JS instance.
W
wusongqing 已提交
145 146 147 148 149 150 151 152 153 154 155
    this.listener.on('change', portraitFunc)
  }

  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Text(this.text).fontSize(24).fontColor(this.color)
    }
    .width('100%').height('100%')
  }
}
```