目录

MediaQuery

API Declaration

Add the following configuration to the features attribute in the manifest.json file:

{"name": "system.mediaquery"}

Module Import

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")

Application Scope

Item

Description

Applicable devices

Mobile phones, tablets, and Vision products

Applicable regions

Global

Definition

API

Description

mediaquery.matchMedia(condition)

Creates a MediaQueryList object.

MediaQueryList.addListener(OBJECT)

Adds a listener to MediaQueryList.

MediaQueryList.removeListener(OBJECT)

Removes the listener from MediaQueryList.

mediaquery.getAccessType()

Obtains the value of accessType for the current media.

[h2]mediaquery.matchMedia(condition)

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.

[h2]MediaQueryList.addListener(OBJECT)

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);

[h2]MediaQueryList.removeListener(OBJECT)

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);

[h2]mediaquery.getAccessType()

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();

Version Change History

Version

Date

Description

1050

2019-09-20

Released the first version.