Skip to content

Diff 对比

前后对比滑块组件,具有像素风格的独特功能。在其标志性模式下,"之前" 侧显示原始图片,而 "之后" 侧自动将其像素化 —— 创造交互式的原图与像素化效果对比。支持自定义插槽以比较任意两段内容。

灵感来源:daisyUI Diff,并结合了本库内置的像素化引擎进行增强。

名称分类说明类型默认值
pixelSize? 风格像素块大小(值越大越像素化)number8
grayscale? 风格启用灰度模式(仅对像素化侧生效)booleanfalse
width? 尺寸容器宽度number | string
height? 尺寸容器高度number | string
palette? 颜色自定义调色板(十六进制字符串或 RGB 元组)string[] | number[][]
background? 颜色透明区域的背景色string'#FFFFFF'
color? 颜色滑块手柄主题色(primary / success / warning / info / danger)string
initialPosition? 行为滑块初始位置(0-100)number50
src? 内容图片源地址(使用时 before 显示原图,after 显示像素化版本)string
change? 事件滑块位置变化时触发(position: number) => void
rendered? 事件像素化渲染完成后触发() => void
error? 事件图片加载失败时触发(event: Event) => void
before? 插槽自定义 "之前" 面板内容(左侧)
after? 插槽自定义 "之后" 面板内容(右侧)
setPosition(n)? 方法设置滑块位置(0-100)(position: number) => void
getPosition()? 方法获取当前滑块位置() => number

基础用法

传入 src 即可对比图片与其像素化版本。拖动滑块可显示更多或更少的原始图片。

<template>
  <div style="display: flex; flex-direction: column; align-items: center; gap: 16px">
    <px-diff src="/images/pixelate/001.png" :width="500" :height="300" />
  </div>
</template>

自定义插槽

使用 beforeafter 插槽来对比任意两段内容 —— 不同的图片、文本或组件。

<template>
  <div style="display: flex; flex-direction: column; align-items: center; gap: 16px">
    <px-diff :width="500" :height="300" color="primary">
      <template #before>
        <img src="/images/pixelate/001.png" style="width: 100%; height: 100%; object-fit: cover" />
      </template>
      <template #after>
        <img src="/images/pixelate/002.jpg" style="width: 100%; height: 100%; object-fit: cover" />
      </template>
    </px-diff>
  </div>
</template>

灰度模式

grayscale 与像素化模式结合使用,为 "之后" 侧呈现复古单色效果。

<template>
  <div style="display: flex; flex-direction: column; align-items: center; gap: 16px">
    <px-diff
      src="/images/pixelate/002.jpg"
      :width="500"
      :height="300"
      :pixel-size="6"
      grayscale
      color="info"
    />
  </div>
</template>