Skip to content

可见性指令

可见性指令帮助您管理元素可见性和懒加载。

v-lazy

图片进入视口时懒加载。

基本用法

vue
<template>
  <img v-lazy="imageUrl" />
  <img v-lazy="{ src: imageUrl, placeholder: 'placeholder.jpg' }" />
  <div v-lazy="backgroundImageUrl"></div>
</template>

API

选项类型默认值描述
srcstring-图片源 URL
placeholderstring-占位图 URL
errorstring-错误图 URL
preloadnumber0预加载距离(像素)
attemptnumber1重试次数
onLoadFunction-加载成功回调
onErrorFunction-加载失败回调

v-intersect

观察元素与视口的交叉状态。

基本用法

vue
<template>
  <div v-intersect="handleIntersect">观察我</div>
  <div v-intersect="{ onEnter: handleEnter, onLeave: handleLeave }">
    跟踪可见性
  </div>
</template>

<script setup>
function handleIntersect(entry, observer) {
  console.log('交叉中:', entry.isIntersecting)
}
</script>

API

选项类型默认值描述
handlerFunction-元素交叉时的回调
onEnterFunction-元素进入视口时的回调
onLeaveFunction-元素离开视口时的回调
thresholdnumber | number[]0触发阈值
rootMarginstring'0px'根元素边距
oncebooleanfalse只触发一次

v-visible

切换元素可见性,支持动画。

基本用法

vue
<template>
  <div v-visible="showElement">显示/隐藏</div>
  <div v-visible="{ useHidden: true, initial: false }">
    使用 visibility: hidden
  </div>
</template>

API

选项类型默认值描述
handlerFunction-可见性改变时的回调
useHiddenbooleanfalse使用 visibility: hidden
initialbooleantrue初始可见性状态

v-loading

在元素上显示加载遮罩。

基本用法

vue
<template>
  <div v-loading="isLoading">内容</div>
  <div v-loading="{ value: isLoading, text: '加载中...', lock: true }">
    内容
  </div>
</template>

API

选项类型默认值描述
valuebooleantrue加载状态
textstring-加载文本
backgroundstring'rgba(255, 255, 255, 0.9)'背景颜色
lockbooleanfalse加载时锁定滚动
spinnerstring-自定义加载器 HTML

基于 MIT 许可发布