Skip to main content
Version: 23.8.0

Page.emulateMediaFeatures() 方法

¥Page.emulateMediaFeatures() method

签名

¥Signature

class Page {
abstract emulateMediaFeatures(features?: MediaFeature[]): Promise<void>;
}

参数

¥Parameters

范围

类型

描述

features

MediaFeature[]

(可选的)<?Array<Object>> 给定一组媒体特性对象,在页面上模拟 CSS 媒体特性。每个媒体特性对象必须具有以下属性:

Returns:

Promise<void>

示例

¥Example

await page.emulateMediaFeatures([
{name: 'prefers-color-scheme', value: 'dark'},
]);
await page.evaluate(() => matchMedia('(prefers-color-scheme: dark)').matches);
// → true
await page.evaluate(() => matchMedia('(prefers-color-scheme: light)').matches);
// → false

await page.emulateMediaFeatures([
{name: 'prefers-reduced-motion', value: 'reduce'},
]);
await page.evaluate(
() => matchMedia('(prefers-reduced-motion: reduce)').matches,
);
// → true
await page.evaluate(
() => matchMedia('(prefers-reduced-motion: no-preference)').matches,
);
// → false

await page.emulateMediaFeatures([
{name: 'prefers-color-scheme', value: 'dark'},
{name: 'prefers-reduced-motion', value: 'reduce'},
]);
await page.evaluate(() => matchMedia('(prefers-color-scheme: dark)').matches);
// → true
await page.evaluate(() => matchMedia('(prefers-color-scheme: light)').matches);
// → false
await page.evaluate(
() => matchMedia('(prefers-reduced-motion: reduce)').matches,
);
// → true
await page.evaluate(
() => matchMedia('(prefers-reduced-motion: no-preference)').matches,
);
// → false

await page.emulateMediaFeatures([{name: 'color-gamut', value: 'p3'}]);
await page.evaluate(() => matchMedia('(color-gamut: srgb)').matches);
// → true
await page.evaluate(() => matchMedia('(color-gamut: p3)').matches);
// → true
await page.evaluate(() => matchMedia('(color-gamut: rec2020)').matches);
// → false