js-apis-mediaquery.md 4.9 KB
Newer Older
1
# @ohos.mediaquery
W
wusongqing 已提交
2

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
import mediaquery from '@ohos.mediaquery'
```


## mediaquery.matchMediaSync

matchMediaSync(condition: string): MediaQueryListener

21
Sets the media query condition. This API returns the corresponding media query listener.
W
wusongqing 已提交
22

W
wusongqing 已提交
23 24
**System capability**: SystemCapability.ArkUI.ArkUI.Full

W
wusongqing 已提交
25
**Parameters**
26

W
wusongqing 已提交
27 28
| Name      | Type    | Mandatory  | Description                                      |
| --------- | ------ | ---- | ---------------------------------------- |
29
| condition | string | Yes   | Media query condition. For details, see [Syntax of Media Query Conditions](../../ui/ui-ts-layout-mediaquery.md#syntax-of-media-query-conditions).|
W
wusongqing 已提交
30

W
wusongqing 已提交
31
**Return value**
32

W
wusongqing 已提交
33 34
| Type                | Description                    |
| ------------------ | ---------------------- |
35
| MediaQueryListener | Media query listener, which is used to register or deregister the listening callback.|
W
wusongqing 已提交
36

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

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


## MediaQueryListener

46
Implements the media query listener, including the first query result when the listener is applied for.
W
wusongqing 已提交
47

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

### Attributes

52 53 54 55
| Name   | Type   | Readable| Writable| Description                |
| ------- | ------- | ---- | ---- | -------------------- |
| matches | boolean | Yes  | No  | Whether the media query condition is met.  |
| media   | string  | Yes  | No  | Media query condition.|
W
wusongqing 已提交
56 57 58 59 60 61


### on

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

62
Registers the media query listener. The callback is triggered when the media attributes change.
W
wusongqing 已提交
63

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

W
wusongqing 已提交
66
**Parameters**
67

W
wusongqing 已提交
68 69
| Name     | Type                              | Mandatory  | Description              |
| -------- | -------------------------------- | ---- | ---------------- |
70
| type     | string                           | Yes   | Listener type. The value is fixed at **'change'**.|
W
wusongqing 已提交
71
| callback | Callback<MediaQueryResult> | Yes   | Callback registered with media query.      |
W
wusongqing 已提交
72

W
wusongqing 已提交
73
**Example**
74

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


### off

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

82
Deregisters the media query listener, so that no callback is triggered when the media attributes change.
W
wusongqing 已提交
83 84 85

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

W
wusongqing 已提交
86
**Parameters**
87 88 89 90 91

| Name  | Type                            | Mandatory| Description                                                      |
| -------- | -------------------------------- | ---- | ---------------------------------------------------------- |
| type     | string                           | Yes  | Listener type. The value is fixed at **'change'**.                                  |
| callback | Callback<MediaQueryResult> | No  | Callback to be deregistered. If the default value is used, all callbacks of the handle are deregistered.|
W
wusongqing 已提交
92 93

**Example**
94

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

## MediaQueryResult

112 113 114 115
Provides the media query result.

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

W
wusongqing 已提交
116 117 118

### Attributes

119 120 121 122
| Name   | Type   | Readable| Writable| Description                |
| ------- | ------- | ---- | ---- | -------------------- |
| matches | boolean | Yes  | No  | Whether the media query condition is met.  |
| media   | string  | Yes  | No  | Media query condition.|
W
wusongqing 已提交
123 124 125 126


### Example

G
Gloria 已提交
127
```ts
W
wusongqing 已提交
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
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 已提交
149
    let portraitFunc = this.onPortrait.bind(this) // Bind the current JS instance.
W
wusongqing 已提交
150 151 152 153 154 155 156 157 158 159 160
    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%')
  }
}
```