Skip to content

useMediaQuery

Category
Export Size
505 B
Last Changed
15 minutes ago

响应式的 媒体查询。一旦您创建了一个 MediaQueryList 对象,您可以检查查询结果或在结果变化时接收通知。

Demo

isLargeScreen: false
prefersDark: false

使用方法

js
import { useMediaQuery } from '@vueuse/core'

const isLargeScreen = useMediaQuery('(min-width: 1024px)')
const isPreferredDark = useMediaQuery('(prefers-color-scheme: dark)')

类型声明

typescript
/**
 * 响应式媒体查询。
 *
 * @see https://vueuse.org/useMediaQuery
 * @param query
 * @param options
 */
export declare function useMediaQuery(
  query: MaybeRefOrGetter<string>,
  options?: ConfigurableWindow,
): Ref<boolean, boolean>

Source

SourceDemoDocs

贡献者

Anthony Fu
Antério Vieira
丶远方
Anthony Fu
Indrek Ardel
Zhousg
Young
Jelf
Dominik Freier
webfansplz
Eureka
karma
Alex Kozack
Jacob Clevenger

变更日志

v10.3.0 on 7/30/2023
f3969 - fix: only add/remove event listeners on query change (#3236)
v10.0.0-beta.4 on 4/13/2023
4d757 - feat(types)!: rename MaybeComputedRef to MaybeRefOrGetter
10e98 - feat(toRef)!: rename resolveRef to toRef
v10.0.0-beta.3 on 4/12/2023
4e7b0 - fix(useWindowSize)!: use media query instead of orientationchange listener (#2919)

Released under the MIT License.