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

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

W
wusongqing 已提交
5 6

> **NOTE**
W
wusongqing 已提交
7 8 9
>
> - The APIs of this module are no longer maintained since API version 7. You are advised to use [`@ohos.mediaquery`](js-apis-mediaquery.md) instead.
> - The initial APIs of this module are supported since API version 3. Newly added APIs will be marked with a superscript to indicate their earliest API version.
W
wusongqing 已提交
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29


## Modules to Import


```
import mediaquery from '@system.mediaquery';
```


## mediaquery.matchMedia

matchMedia(condition: string): MediaQueryList

Creates a **MediaQueryList** object based on the query condition.

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

**Parameters**

W
wusongqing 已提交
30 31 32
| Name      | Type    | Mandatory  | Description      |
| --------- | ------ | ---- | -------- |
| condition | string | Yes   | Query condition.|
W
wusongqing 已提交
33 34 35

**Return value**

W
wusongqing 已提交
36 37
| Type          | Description                                      |
| -------------- | ---------------------------------------- |
W
wusongqing 已提交
38 39 40 41 42
| MediaQueryList | Attributes of the **MediaQueryList** object created. For details, see **MediaQueryList** attributes.|

**Example**

```
W
wusongqing 已提交
43
var mMediaQueryList = mediaquery.matchMedia('(max-width: 466)');    
W
wusongqing 已提交
44 45 46 47 48 49 50 51
```

## MediaQueryEvent

Defines a media query event.

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

W
wusongqing 已提交
52 53 54
| Name     | Type   | Mandatory  | Description   |
| ------- | ------- | ---- | ----- |
| matches | boolean | Yes   | Matching result.|
W
wusongqing 已提交
55 56 57 58 59 60 61 62 63

## MediaQueryList

Defines a media query list.

### Attributes

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

W
wusongqing 已提交
64 65 66 67
| Name     | Type   | Mandatory  | Description               |
| ------- | ------- | ---- | ----------------- |
| media   | string  | No   | Serialized media query condition. This parameter is read-only.|
| matches | boolean | Yes   | Matching result.            |
W
wusongqing 已提交
68 69 70 71 72 73 74 75 76 77 78

### onchange

onchange?: (matches: boolean) => void

Called when the **matches** value changes.

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

**Parameters**

W
wusongqing 已提交
79 80 81
| Name    | Type     | Mandatory  | Description            |
| ------- | ------- | ---- | -------------- |
| matches | boolean | Yes   | New **matches** value.|
W
wusongqing 已提交
82 83 84 85 86 87 88 89 90 91 92 93


### MediaQueryList.addListener

addListener(callback: (event: MediaQueryEvent) => void): void

Adds a listener for this **MediaQueryList** object. The listener must be added before **onShow** is called, that is, it must be added in the **onInit** or **onReady** API.

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

**Parameters**

W
wusongqing 已提交
94 95
| Name     | Type                              | Mandatory  | Description            |
| -------- | -------------------------------- | ---- | -------------- |
W
wusongqing 已提交
96
| callback | (event: MediaQueryEvent) => void | Yes   | Callback invoked when the query condition changes.|
W
wusongqing 已提交
97 98 99 100

**Example**

```
W
wusongqing 已提交
101 102 103 104 105
function maxWidthMatch(e){
  if(e.matches){
    // do something
  }
}
W
wusongqing 已提交
106 107 108 109 110 111 112 113 114 115 116 117 118 119
mMediaQueryList.addListener(maxWidthMatch);
```


### MediaQueryList.removeListener

removeListener(callback: (event: MediaQueryEvent) => void): void

Removes the listener for this **MediaQueryList** object.

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

**Parameters**

W
wusongqing 已提交
120 121
| Name     | Type                               | Mandatory  | Description            |
| -------- | --------------------------------- | ---- | -------------- |
W
wusongqing 已提交
122
| callback | (event: MediaQueryEvent) => void) | Yes   | Callback invoked when the query condition changes.|
W
wusongqing 已提交
123 124 125 126

**Example**

```
W
wusongqing 已提交
127 128 129 130 131
function maxWidthMatch(e){
  if(e.matches){
    // do something
  }
}
W
wusongqing 已提交
132 133
mMediaQueryList.removeListener(maxWidthMatch);
```