Skip to content

useCssVar

Category
Export Size
820 B
Last Changed
15 minutes ago

操作 CSS 变量

Demo

示例文字,
示例文字,--color:

Usage

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

const el = ref(null)
const color1 = useCssVar('--color', el)

const elv = ref(null)
const key = ref('--color')
const colorVal = useCssVar(key, elv)

const someEl = ref(null)
const color2 = useCssVar('--color', someEl, { initialValue: '#eee' })

类型声明

typescript
export interface UseCssVarOptions extends ConfigurableWindow {
  initialValue?: string
  /**
   * 使用 MutationObserver 监听变量更改
   * @default false
   */
  observe?: boolean
}
/**
 * 操作 CSS 变量。
 *
 * @see https://vueuse.org/useCssVar
 * @param prop
 * @param target
 * @param options
 */
export declare function useCssVar(
  prop: MaybeRefOrGetter<string | null | undefined>,
  target?: MaybeElementRef,
  options?: UseCssVarOptions,
): Ref<string | null | undefined, string | null | undefined>

Source

SourceDemoDocs

贡献者

Anthony Fu
丶远方
Antério Vieira
Anthony Fu
babu-ch
Fernando Fernández
Waleed Khaled
木荣
JD Solanki
btea
Alex Kozack

变更日志

v11.0.2 on 8/24/2024
d0523 - fix: Replace getComputedStyle with direct style property access (#4166)
v11.0.0-beta.2 on 7/17/2024
fe19c - feat(useCssVars): remove property on null/undefined (#3821)
v10.2.0 on 6/16/2023
9d34a - fix: observe changes based on element class list (#3131)
v10.0.2 on 4/14/2023
7d001 - fix: module reference, close #2972
v10.0.0-beta.4 on 4/13/2023
4d757 - feat(types)!: rename MaybeComputedRef to MaybeRefOrGetter
0a72b - feat(toValue): rename resolveUnref to toValue
v10.0.0-beta.0 on 3/14/2023
ae6e1 - feat: introduce observe option (#2800)

Released under the MIT License.