Watermark 水印
給一塊區域或整個頁面鋪上重複的文字或圖片水印,用於標記內部資料、防止截圖外傳。
引入
import { HuiWatermark } from '@nscode/handy-ui'
基礎用法
把要加水印的內容放進預設插槽,用 content 設定水印文字。水印層蓋在內容上面,不影響點擊和選擇文字。
<script setup lang="ts">
import { HuiWatermark } from '@nscode/handy-ui'
</script>
<template>
<HuiWatermark content="HandyUI">
<div style="height: 300px">這是一個帶有水印的區域</div>
</HuiWatermark>
</template>
溢出內容與外距
水印容器按普通塊級版面配置處理外距,不裁剪溢出內容,也不自動建立捲動容器。需要裁剪或捲動時,在業務容器上顯式配置 overflow。
<script setup lang="ts">
import { HuiButton, HuiWatermark } from '@nscode/handy-ui'
</script>
<template>
<HuiWatermark
content="版面配置驗證"
style="height: 180px; border: 1px solid var(--hui-border-color)"
>
<div style="margin: 24px; height: 210px">
<p>內容外距保持 24px,底部按鈕超出水印區域仍可見、可點擊。</p>
<HuiButton style="margin-top: 120px">溢出區域按鈕</HuiButton>
</div>
</HuiWatermark>
<div style="height: 100px" />
</template>
多行文字
content 傳陣列時每個元素一行,多行文字居中對齊。總高度超過 height 時,水印塊會按文字實際高度擴大,不會切掉下面的行。
<template>
<HuiWatermark :content="['HandyUI', 'Vue 3 元件庫']">
<div style="height: 300px">多行水印示例</div>
</HuiWatermark>
</template>
常見的寫法是使用者名加日期,截圖外傳時能看出是誰截的:
<template>
<HuiWatermark :content="['張三 工號 1024', '2026-11-18 內部資料']" :gap="[60, 60]">
<div style="height: 240px; padding: 16px">
<HuiParagraph>這是一份內部報表,水印裡有查看人和日期。</HuiParagraph>
<HuiButton type="primary">水印不影響點擊</HuiButton>
</div>
</HuiWatermark>
</template>
圖片水印
image 傳圖片地址,width、height 是圖片的繪製尺寸。同時傳了 content 時,圖片載入失敗會退回文字水印。
<template>
<HuiWatermark image="/logo.png" :width="48" :height="48" :alpha="0.3">
<div style="height: 300px">圖片水印示例</div>
</HuiWatermark>
</template>
圖片地址錯了、載入失敗時,有 content 就顯示文字水印,沒有就不顯示:
<template>
<HuiWatermark image="/images/broken.png" content="圖片載入失敗,顯示文字">
<div style="height: 200px">圖片載入失敗的示例</div>
</HuiWatermark>
</template>
自訂字體
font 設定顏色、字級、字重、字體和字體樣式,沒寫的欄位用預設值。fontWeight 取 CSS font-weight 的合法值:'normal'、'bold'、'lighter'、'bolder' 或數字(如 300、600)。
<template>
<HuiWatermark
content="機密文件"
:font="{ color: 'rgba(255, 77, 79, 0.3)', fontSize: 20, fontWeight: 'bold' }"
:gap="[80, 80]"
:rotate="-30"
>
<div style="height: 300px">紅色水印樣式</div>
</HuiWatermark>
</template>
font 的各個欄位分別改,放在一起對比。水印是畫在 canvas 上的,color 要寫具體的顏色值,不能用 var(--hui-...) 這類 CSS 變數:
<script setup lang="ts">
import { HuiWatermark } from '@nscode/handy-ui'
import type { WatermarkFontConfig } from '@nscode/handy-ui'
const items: { label: string; font: WatermarkFontConfig }[] = [
{ label: '預設', font: {} },
{ label: 'fontSize: 20', font: { fontSize: 20 } },
{ label: "fontWeight: 'bold'", font: { fontWeight: 'bold' } },
{ label: "fontWeight: 'lighter'", font: { fontWeight: 'lighter' } },
{ label: "fontStyle: 'italic'", font: { fontStyle: 'italic' } },
{ label: "fontStyle: 'oblique'", font: { fontStyle: 'oblique' } },
{ label: "fontFamily: 'serif'", font: { fontFamily: 'serif' } },
{ label: "fontFamily: 'monospace'", font: { fontFamily: 'monospace' } },
{ label: "color: 'rgba(24, 144, 255, 0.3)'", font: { color: 'rgba(24, 144, 255, 0.3)' } }
]
</script>
<template>
<div class="grid">
<div v-for="item in items" :key="item.label">
<p class="label">{{ item.label }}</p>
<HuiWatermark content="HandyUI 水印" :font="item.font" :gap="[40, 40]">
<div class="box" />
</HuiWatermark>
</div>
</div>
</template>
<style scoped>
.grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
gap: var(--hui-spacing-4);
}
.label {
margin: 0 0 4px;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
.box {
height: 140px;
border: 1px solid var(--hui-border-color-light);
}
</style>
旋轉、間距和偏移
rotate 是旋轉角度,gap 是相鄰兩個水印之間的水平、垂直間距,offset 是整個水印圖案的平移量。水印塊的大小按文字寬度和旋轉後的範圍自動計算,長文字和旋轉後的文字都能完整顯示。
<template>
<HuiWatermark content="rotate -45" :rotate="-45" :gap="[120, 120]" :offset="[0, 0]">
<div style="height: 300px" />
</HuiWatermark>
</template>
不同的旋轉角度,預設是 -22,負數逆時針、正數順時針:
<template>
<div class="grid">
<div v-for="angle in [0, -22, -45, 30, 90]" :key="angle">
<p class="label">rotate {{ angle }}{{ angle === -22 ? '(預設)' : '' }}</p>
<HuiWatermark content="HandyUI" :rotate="angle" :gap="[40, 40]">
<div class="box" />
</HuiWatermark>
</div>
</div>
</template>
<style scoped>
.grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
gap: var(--hui-spacing-4);
}
.label {
margin: 0 0 4px;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
.box {
height: 140px;
border: 1px solid var(--hui-border-color-light);
}
</style>
不同的間距和偏移,gap 預設 [100, 100],offset 預設 [50, 50]:
<template>
<div class="grid">
<div>
<p class="label">gap [20, 20]</p>
<HuiWatermark content="HandyUI" :gap="[20, 20]"><div class="box" /></HuiWatermark>
</div>
<div>
<p class="label">gap [100, 100](預設)</p>
<HuiWatermark content="HandyUI"><div class="box" /></HuiWatermark>
</div>
<div>
<p class="label">gap [160, 40]</p>
<HuiWatermark content="HandyUI" :gap="[160, 40]"><div class="box" /></HuiWatermark>
</div>
<div>
<p class="label">offset [0, 0]</p>
<HuiWatermark content="HandyUI" :offset="[0, 0]"><div class="box" /></HuiWatermark>
</div>
<div>
<p class="label">offset [50, 50](預設)</p>
<HuiWatermark content="HandyUI"><div class="box" /></HuiWatermark>
</div>
<div>
<p class="label">offset [120, 10]</p>
<HuiWatermark content="HandyUI" :offset="[120, 10]"><div class="box" /></HuiWatermark>
</div>
</div>
</template>
<style scoped>
.grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
gap: var(--hui-spacing-4);
}
.label {
margin: 0 0 4px;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
.box {
height: 160px;
border: 1px solid var(--hui-border-color-light);
}
</style>
透明度
alpha 是整個水印的透明度,0 到 1,預設 1。它和顏色本身的透明度疊加:預設顏色已經是 15% 的不透明度,alpha 再調低會更淡。下面用了不透明的藍色,方便看出區別:
<template>
<div class="grid">
<div v-for="alpha in [1, 0.6, 0.3, 0.1]" :key="alpha">
<p class="label">alpha {{ alpha }}{{ alpha === 1 ? '(預設)' : '' }}</p>
<HuiWatermark
content="HandyUI"
:alpha="alpha"
:font="{ color: 'rgb(24, 144, 255)', fontSize: 16 }"
:gap="[40, 40]"
>
<div class="box" />
</HuiWatermark>
</div>
</div>
</template>
<style scoped>
.grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
gap: var(--hui-spacing-4);
}
.label {
margin: 0 0 4px;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
.box {
height: 140px;
border: 1px solid var(--hui-border-color-light);
}
</style>
調整參數
拖曳滑桿、修改文字,水印立即重新生成:
<script setup lang="ts">
import { computed, ref } from 'vue'
import { HuiInput, HuiSlider, HuiWatermark } from '@nscode/handy-ui'
const content = ref('HandyUI 水印')
const fontSize = ref(16)
const rotate = ref(-22)
const gapX = ref(100)
const gapY = ref(100)
const offsetX = ref(50)
const offsetY = ref(50)
const alpha = ref(1)
const font = computed(() => ({ fontSize: fontSize.value }))
const gap = computed<[number, number]>(() => [gapX.value, gapY.value])
const offset = computed<[number, number]>(() => [offsetX.value, offsetY.value])
</script>
<template>
<div class="controls">
<span>content</span>
<HuiInput v-model="content" placeholder="水印文字" />
<span>fontSize {{ fontSize }}</span>
<HuiSlider v-model="fontSize" :min="12" :max="40" />
<span>rotate {{ rotate }}</span>
<HuiSlider v-model="rotate" :min="-90" :max="90" />
<span>gap {{ gapX }} / {{ gapY }}</span>
<div class="pair">
<HuiSlider v-model="gapX" :min="0" :max="200" />
<HuiSlider v-model="gapY" :min="0" :max="200" />
</div>
<span>offset {{ offsetX }} / {{ offsetY }}</span>
<div class="pair">
<HuiSlider v-model="offsetX" :min="0" :max="200" />
<HuiSlider v-model="offsetY" :min="0" :max="200" />
</div>
<span>alpha {{ alpha.toFixed(1) }}</span>
<HuiSlider v-model="alpha" :min="0" :max="1" :step="0.1" />
</div>
<HuiWatermark
:content="content"
:font="font"
:rotate="rotate"
:gap="gap"
:offset="offset"
:alpha="alpha"
>
<div class="box" />
</HuiWatermark>
</template>
<style scoped>
.controls {
display: grid;
grid-template-columns: 136px 1fr;
gap: var(--hui-spacing-2) var(--hui-spacing-4);
align-items: center;
max-width: 560px;
margin-bottom: var(--hui-spacing-4);
font-size: var(--hui-font-size-sm);
}
.pair {
display: grid;
grid-template-columns: 1fr 1fr;
gap: var(--hui-spacing-4);
}
.box {
height: 300px;
border: 1px solid var(--hui-border-color-light);
}
</style>
層級
z-index 是水印層的 z-index,預設 9。內容裡有層級更高的定位元素時會蓋住水印,這時把 z-index 調大:
<template>
<div class="grid">
<div>
<p class="label">預設 9:被 z-index 為 10 的卡片蓋住</p>
<HuiWatermark content="HandyUI" :gap="[40, 40]">
<div class="box"><div class="raised">z-index: 10</div></div>
</HuiWatermark>
</div>
<div>
<p class="label">z-index 11:水印蓋在卡片上面</p>
<HuiWatermark content="HandyUI" :gap="[40, 40]" :z-index="11">
<div class="box"><div class="raised">z-index: 10</div></div>
</HuiWatermark>
</div>
</div>
</template>
<style scoped>
.grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
gap: var(--hui-spacing-4);
}
.label {
margin: 0 0 4px;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
.box {
display: flex;
align-items: center;
justify-content: center;
height: 180px;
border: 1px solid var(--hui-border-color-light);
}
.raised {
position: relative;
z-index: 10;
display: flex;
align-items: center;
justify-content: center;
width: 60%;
height: 60%;
border-radius: var(--hui-border-radius);
background: var(--hui-color-primary-bg);
}
</style>
預設顏色隨主題變化
不傳 font.color 時,元件按容器的文字顏色判斷明暗:亮色主題下是 rgba(0, 0, 0, 0.15),暗色主題下是 rgba(255, 255, 255, 0.15)。切換 <html> 的 data-theme、class,或者系統切換明暗模式時會自動重新生成。
判斷依據是容器的文字顏色,所以放在深色區域、文字是白色時,水印也會自動用淺色:
<template>
<div class="grid">
<HuiWatermark content="HandyUI" :gap="[40, 40]">
<div class="box">文字顏色跟著主題</div>
</HuiWatermark>
<div class="dark">
<HuiWatermark content="HandyUI" :gap="[40, 40]">
<div class="box">深色區域,文字是白色,水印用淺色</div>
</HuiWatermark>
</div>
</div>
</template>
<style scoped>
.grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
gap: var(--hui-spacing-4);
}
.box {
height: 160px;
padding: var(--hui-spacing-3);
box-sizing: border-box;
border: 1px solid var(--hui-border-color-light);
}
.dark {
color: var(--hui-color-white);
background: #1f2937;
}
</style>
全螢幕水印
fullscreen 為 true 時水印層改成 position: fixed,鋪滿整個視口。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiWatermark } from '@nscode/handy-ui'
const show = ref(false)
</script>
<template>
<HuiButton @click="show = !show">{{ show ? '關閉' : '打開' }}全螢幕水印</HuiButton>
<HuiWatermark v-if="show" content="HandyUI" fullscreen />
</template>
Props
| 屬性 | 類型 | 預設值 | 說明 |
|---|---|---|---|
| content | string | string[] | undefined | 水印文字,陣列表示多行;空字串和空陣列不顯示水印 |
| image | string | undefined | 圖片水印地址,優先於文字;載入失敗時退回文字水印 |
| width | number | 120 | 水印內容的最小寬度(px),文字更寬時按文字寬度;圖片按這個寬度繪製 |
| height | number | 64 | 水印內容的最小高度(px),多行文字更高時按文字高度;圖片按這個高度繪製 |
| rotate | number | -22 | 旋轉角度(度) |
| zIndex | number | 9 | 水印層的 z-index |
| gap | [number, number] | [100, 100] | 水印之間的間距 [水平, 垂直](px) |
| offset | [number, number] | [50, 50] | 水印圖案整體平移 [水平, 垂直](px),即 background-position |
| font | WatermarkFontConfig | 見下表 | 字體配置 |
| alpha | number | 1 | 透明度,0 到 1 |
| fullscreen | boolean | false | 是否鋪滿整個視口(position: fixed) |
WatermarkFontConfig
| 屬性 | 類型 | 預設值 | 說明 |
|---|---|---|---|
| color | string | 隨主題變化 | 文字顏色。不傳時亮色下是 rgba(0, 0, 0, 0.15),暗色下是 rgba(255, 255, 255, 0.15) |
| fontSize | number | 14 | 字級(px) |
| fontWeight | 'normal' | 'bold' | 'lighter' | 'bolder' | number | 'normal' | 字重 |
| fontFamily | string | 'sans-serif' | 字體 |
| fontStyle | 'normal' | 'italic' | 'oblique' | 'normal' | 字體樣式 |
Slots
| 插槽 | 說明 |
|---|---|
| default | 需要添加水印的內容 |
類型
公開類型使用元件前綴。相容映射:FontConfig → WatermarkFontConfig。舊名仍可從 @nscode/handy-ui 導入,已標記 @deprecated,將在 1.0 刪除。
import type { WatermarkFontConfig, WatermarkProps } from '@nscode/handy-ui'
interface WatermarkFontConfig {
color?: string
fontSize?: number
fontWeight?: 'normal' | 'bold' | 'lighter' | 'bolder' | number
fontFamily?: string
fontStyle?: 'normal' | 'italic' | 'oblique'
}
interface WatermarkProps {
content?: string | string[]
image?: string
width?: number
height?: number
rotate?: number
zIndex?: number
gap?: [number, number]
offset?: [number, number]
font?: WatermarkFontConfig
alpha?: number
fullscreen?: boolean
}