当前位置:首页 > VUE

vue 水印 实现

2026-02-10 16:39:52VUE

Vue 水印实现方法

使用 CSS 背景图实现

在 Vue 中可以通过 CSS 的 background-image 属性添加水印。创建一个包含水印文本的 Canvas 元素,将其转换为 Data URL 后设置为背景。

// 在 Vue 组件中
methods: {
  generateWatermark(text) {
    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.2);
    ctx.fillText(text, 10, 80);
    return canvas.toDataURL('image/png');
  }
},
mounted() {
  const watermark = this.generateWatermark('Confidential');
  document.body.style.backgroundImage = `url(${watermark})`;
  document.body.style.backgroundRepeat = 'repeat';
}

使用自定义指令实现

创建一个 Vue 自定义指令可以更灵活地控制水印的添加和移除。

// main.js 或单独的文件
Vue.directive('watermark', {
  inserted(el, binding) {
    const { text, color, fontSize } = binding.value || {};
    const canvas = document.createElement('canvas');
    canvas.width = 200;
    canvas.height = 150;
    const ctx = canvas.getContext('2d');
    ctx.font = `${fontSize || '16px'} Arial`;
    ctx.fillStyle = color || 'rgba(200, 200, 200, 0.3)';
    ctx.rotate(-0.2);
    ctx.fillText(text || 'Watermark', 10, 80);

    el.style.backgroundImage = `url(${canvas.toDataURL('image/png')})`;
    el.style.backgroundRepeat = 'repeat';
  }
});

// 在组件中使用
<template>
  <div v-watermark="{ text: 'Sample', color: 'rgba(0,0,0,0.1)', fontSize: '20px' }">
    <!-- 内容 -->
  </div>
</template>

使用第三方库

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

安装:

npm install vue-watermark

使用:

import VueWatermark from 'vue-watermark';

Vue.use(VueWatermark);

// 在组件中
<template>
  <vue-watermark
    :text="watermarkText"
    :font-size="16"
    :color="'rgba(0,0,0,0.1)'"
    :width="200"
    :height="150"
  >
    <!-- 内容 -->
  </vue-watermark>
</template>

防止水印被移除

为了防止用户通过开发者工具移除水印,可以结合 MutationObserver 监听 DOM 变化并重新添加水印。

vue 水印 实现

mounted() {
  const observer = new MutationObserver((mutations) => {
    mutations.forEach(() => {
      if (!document.body.getAttribute('data-watermark')) {
        this.addWatermark();
      }
    });
  });

  observer.observe(document.body, {
    attributes: true,
    childList: true,
    subtree: true
  });

  this.addWatermark();
},
methods: {
  addWatermark() {
    const watermark = this.generateWatermark('Protected');
    document.body.style.backgroundImage = `url(${watermark})`;
    document.body.setAttribute('data-watermark', 'true');
  }
}

这些方法提供了从简单到高级的不同水印实现方案,可以根据具体需求选择合适的实现方式。

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

相关文章

vue实现共享屏幕

vue实现共享屏幕

共享屏幕的基本实现 在Vue中实现共享屏幕功能通常需要结合浏览器的getDisplayMedia API。以下是一个基础实现示例: <template> <div>…

vue实现反转

vue实现反转

实现数组反转 在Vue中反转数组可以通过多种方式实现,以下是几种常见方法: 使用JavaScript原生reverse方法 // 在methods中定义方法 methods: { revers…

vue  实现关联属性

vue 实现关联属性

Vue 实现关联属性 在 Vue 中实现关联属性通常涉及计算属性(computed properties)或侦听器(watchers),用于根据其他属性的变化动态更新目标属性。 使用计算属性 计算属…

vue实现选择框

vue实现选择框

Vue 实现选择框的方法 Vue 中可以通过多种方式实现选择框(下拉框),包括原生 HTML 的 <select> 元素结合 Vue 的数据绑定,或者使用第三方 UI 库如 Element…

vue select实现搜索

vue select实现搜索

实现 Vue Select 搜索功能的方法 在 Vue 中实现 select 组件的搜索功能,可以通过原生 HTML select 结合自定义搜索逻辑,或使用第三方库如 vue-select 来实现。…

vue实现折叠组件

vue实现折叠组件

Vue 折叠组件实现方法 基于 v-show 和事件触发 通过 v-show 控制内容的显示与隐藏,结合点击事件切换状态。 <template> <div> &…