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

G
Gloria 已提交
3 4
The **mediaquery** module provides different styles for different media types.

W
wusongqing 已提交
5 6
> **NOTE**
>
W
wusongqing 已提交
7 8 9 10 11
> 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 已提交
12
```js
W
wusongqing 已提交
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
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 已提交
28 29
**System capability**: SystemCapability.ArkUI.ArkUI.Full

W
wusongqing 已提交
30
**Parameters**
W
wusongqing 已提交
31 32
| Name      | Type    | Mandatory  | Description                                      |
| --------- | ------ | ---- | ---------------------------------------- |
G
Gloria 已提交
33
| condition | string | Yes   | Matching condition of a media event. For details, see [Syntax of Media Query Conditions](../../ui/ui-ts-layout-mediaquery.md#syntax-of-media-query-conditions).|
W
wusongqing 已提交
34

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

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


## MediaQueryListener

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

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

### Attributes

W
wusongqing 已提交
54 55 56 57
| 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 已提交
58 59 60 61 62 63 64 65


### 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 已提交
66 67
**System capability**: SystemCapability.ArkUI.ArkUI.Full

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

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


### off

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

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

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


## MediaQueryResult


### Attributes

W
wusongqing 已提交
114 115 116 117
| 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 已提交
118 119 120 121


### Example

G
Gloria 已提交
122
```ts
W
wusongqing 已提交
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'


@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() {
G
Gloria 已提交
144
    let 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%')
  }
}
```