当前位置:首页 > VUE

vue 水印 实现

2026-03-07 16:29:19VUE

Vue 水印实现方法

使用 canvas 生成水印

通过 canvas 绘制水印文字或图片,转换为 base64 图片后设置为背景。这种方法适用于需要动态生成水印的场景。

<template>
  <div class="watermark-container"></div>
</template>

<script>
export default {
  mounted() {
    this.createWatermark()
  },
  methods: {
    createWatermark() {
      const canvas = document.createElement('canvas')
      canvas.width = 200
      canvas.height = 150
      const ctx = canvas.getContext('2d')

      ctx.font = '16px Arial'
      ctx.fillStyle = 'rgba(200, 200, 200, 0.3)'
      ctx.rotate(-0.3)
      ctx.fillText('Confidential', 10, 80)

      const watermark = canvas.toDataURL('image/png')
      const container = document.querySelector('.watermark-container')
      container.style.backgroundImage = `url(${watermark})`
      container.style.backgroundRepeat = 'repeat'
    }
  }
}
</script>

CSS 伪元素实现水印

对于简单的水印需求,可以使用 CSS 伪元素实现,性能更好但灵活性较低。

vue 水印 实现

<template>
  <div class="watermarked-content">
    <!-- 页面内容 -->
  </div>
</template>

<style>
.watermarked-content {
  position: relative;
}

.watermarked-content::after {
  content: "Watermark Text";
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  pointer-events: none;
  opacity: 0.2;
  font-size: 48px;
  color: #999;
  display: flex;
  align-items: center;
  justify-content: center;
  transform: rotate(-30deg);
  z-index: 9999;
}
</style>

使用第三方库

对于更复杂的水印需求,可以考虑使用专门的水印库如 vue-watermark

安装:

vue 水印 实现

npm install vue-watermark

使用:

<template>
  <watermark
    :text="watermarkText"
    :fontSize="16"
    :opacity="0.3"
    :width="200"
    :height="150"
  >
    <!-- 需要加水印的内容 -->
  </watermark>
</template>

<script>
import Watermark from 'vue-watermark'

export default {
  components: {
    Watermark
  },
  data() {
    return {
      watermarkText: 'Confidential'
    }
  }
}
</script>

防止水印被移除

为防止用户通过开发者工具移除水印,可以结合 MutationObserver 监控 DOM 变化,自动重新添加被移除的水印。

mounted() {
  this.createWatermark()
  this.protectWatermark()
},
methods: {
  protectWatermark() {
    const targetNode = document.querySelector('.watermark-container')
    const config = { attributes: true, childList: true, subtree: true }

    const callback = (mutationsList) => {
      for(let mutation of mutationsList) {
        if(mutation.type === 'childList' || 
           mutation.type === 'attributes') {
          this.createWatermark()
        }
      }
    }

    const observer = new MutationObserver(callback)
    observer.observe(targetNode, config)
  }
}

水印样式自定义建议

  • 透明度建议设置在 0.2-0.4 之间,既不影响内容阅读又能清晰显示
  • 旋转角度通常在 -15° 到 -45° 之间效果较好
  • 字体大小根据容器尺寸调整,一般 16px-24px
  • 颜色推荐使用灰色系,如 #999 或 rgba(150,150,150,0.3)
  • 水印密度根据需求调整,可以适当增加重复间距

以上方法可以根据实际项目需求选择或组合使用,canvas 方法灵活性最高,CSS 方法性能最好,第三方库则提供了开箱即用的解决方案。

标签: 水印vue
分享给朋友:

相关文章

vue实现旋转

vue实现旋转

Vue 实现旋转效果的方法 在 Vue 中实现旋转效果可以通过多种方式完成,以下是几种常见的方法: 使用 CSS 动画 通过 Vue 的 :class 或 :style 绑定 CSS 动画实现旋转效…

vue vnode实现

vue vnode实现

Vue VNode 实现原理 VNode(Virtual Node)是 Vue 的核心概念之一,用于描述真实 DOM 的轻量级虚拟表示。VNode 的实现涉及以下关键点: VNode 的基本结构 V…

vue事件实现

vue事件实现

Vue 事件实现方法 在Vue中,事件处理是通过v-on指令或@简写来实现的。以下是几种常见的事件处理方式: 使用v-on指令绑定事件 <button v-on:click="handleC…

vue实现dialog

vue实现dialog

Vue 实现 Dialog 的方法 使用 Vue 原生组件实现 Vue 可以通过组件化的方式实现 Dialog,以下是一个简单的实现示例: <template> <div>…

vue实现图片循环

vue实现图片循环

实现图片循环的基本方法 在Vue中实现图片循环通常使用v-for指令结合数组数据。以下是基础实现方式: <template> <div> <img v-fo…

实现vue模板解析

实现vue模板解析

Vue 模板解析的基本原理 Vue 的模板解析是将模板字符串转换为渲染函数的过程。核心步骤包括模板编译、AST 生成、优化和代码生成。 模板编译阶段 Vue 使用 vue-template-comp…