Add the following configuration to the features attribute in the manifest.json file:
{"name": "system.mediaquery"} Add the following configuration to the script element on the page where the API will be called:
import mediaquery from '@system.mediaquery'
Or
var mediaquery = require("@system.mediaquery") Item | Description |
|---|---|
Applicable devices | Mobile phones, tablets, and Vision products |
Applicable regions | Global |
API | Description |
|---|---|
Creates a MediaQueryList object. | |
Adds a listener to MediaQueryList. | |
Removes the listener from MediaQueryList. | |
Obtains the value of accessType for the current media. |
Description
Creates a MediaQueryList object.
OBJECT parameters
Parameter | Type | Mandatory | Description |
|---|---|---|---|
condition | string | Yes | Query condition. |
Sample code
var mql = mediaquery.matchMedia('(max-width: 600)'); MediaQueryList attributes
Attribute | Type | Description |
|---|---|---|
matches | boolean | Indicates whether any data is found based on the current query condition. If so, true is returned. Otherwise, false is returned. This parameter is read-only. |
media | string | Media query condition, which is serialized. This parameter is read-only. |
onchange | function | Function triggered when the value of matches changes. |
Description
Adds a listener to MediaQueryList.
OBJECT parameters
Parameter | Type | Mandatory | Description |
|---|---|---|---|
callback | function | Yes | Callback upon any query condition change. |
Sample code
var mql = mediaquery.matchMedia('(max-width: 600)');
function screenTest(e) {
if (e.matches) {
// change element style
} else {
// change element style
}
}
mql.addListener(screenTest); Description
Removes the listener from MediaQueryList.
OBJECT parameters
Parameter | Type | Mandatory | Description |
|---|---|---|---|
callback | function | Yes | Callback upon any query condition change. |
Sample code
mql.removeListener(screenTest);
Description
Obtains the value of accessType for the current media. This API is customized by Huawei and not formulated by the vendor alliance.
Returns
Parameter | Type | Description |
|---|---|---|
type | string | Media access type. The default value is fastapp for quick apps and card for the card type. The value can also be customized by the media, for example, hiboard. |
Sample code
var type = mediaquery.getAccessType();