js-apis-mediaquery.md 4.3 KB
Newer Older
Z
zengyawen 已提交
1 2
# Media Query

W
wusongqing 已提交
3 4
> **NOTE**
>
Z
zengyawen 已提交
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
Z
zengyawen 已提交
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 28 29
**Parameters**
| Name      | Type    | Mandatory  | Description        |
| --------- | ------ | ---- | ---------- |
| condition | string | Yes   | Matching condition of a media event.|
Z
zengyawen 已提交
30

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

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


## MediaQueryListener

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


### Attributes

W
wusongqing 已提交
49 50 51 52
| 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.|
Z
zengyawen 已提交
53 54 55 56 57 58 59 60


### 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 已提交
61 62 63 64 65
**Parameters**
| Name     | Type                              | Mandatory  | Description              |
| -------- | -------------------------------- | ---- | ---------------- |
| type     | string                           | Yes   | Must enter the string **change**.|
| callback | Callback<MediaQueryResult> | Yes   | Callback registered with media query.      |
Z
zengyawen 已提交
66

W
wusongqing 已提交
67
**Example**
Z
zengyawen 已提交
68 69 70 71 72 73 74 75
  For details, see [off Example](#off).


### off

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

Unregisters a callback with the corresponding query condition by using the handle, so that no callback is triggered when the media attributes change.
W
wusongqing 已提交
76 77 78 79 80 81 82 83
**Parameters**
| Name     | Type                              | Mandatory  | Description                           |
| -------- | -------------------------------- | ---- | ----------------------------- |
| type     | boolean                          | Yes   | Must enter the string **change**.             |
| callback | Callback<MediaQueryResult> | No   | Callback to be unregistered. If the default value is used, all callbacks of the handle are unregistered.|

**Example**
  ```js
Z
zengyawen 已提交
84 85 86 87 88 89 90 91 92 93
    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 已提交
94 95
    listener.on('change', onPortrait) // Register a callback.
    listener.off('change', onPortrait) // Unregister a callback.
Z
zengyawen 已提交
96 97 98 99 100 101 102 103
  ```


## MediaQueryResult


### Attributes

W
wusongqing 已提交
104 105 106 107
| 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.|
Z
zengyawen 已提交
108 109 110 111


### Example

W
wusongqing 已提交
112
```js
Z
zengyawen 已提交
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
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() {
    portraitFunc = this.onPortrait.bind(this) //bind current js instance
    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%')
  }
}
```