当前位置:首页 > VUE

vue 水印 实现

2026-01-08 03:27:08VUE

Vue 水印实现方法

使用 canvas 动态生成水印

在 Vue 组件中通过 canvas 绘制水印,并将其作为背景图片插入到目标元素中。这种方法灵活性高,支持动态调整水印内容、角度和透明度。

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

<script>
export default {
  mounted() {
    this.generateWatermark();
  },
  methods: {
    generateWatermark() {
      const canvas = document.createElement('canvas');
      canvas.width = 300;
      canvas.height = 200;
      const ctx = canvas.getContext('2d');
      ctx.font = '16px Arial';
      ctx.fillStyle = 'rgba(0, 0, 0, 0.1)';
      ctx.rotate(-20 * Math.PI / 180);
      ctx.fillText('Confidential', 50, 80);

      const watermarkUrl = canvas.toDataURL();
      this.$refs.watermark.style.backgroundImage = `url(${watermarkUrl})`;
      this.$refs.watermark.style.backgroundRepeat = 'repeat';
    }
  }
}
</script>

<style>
.watermark-container {
  width: 100%;
  height: 100%;
  position: relative;
}
</style>

使用 CSS 伪元素实现静态水印

对于简单的水印需求,可以直接通过 CSS 伪元素实现。这种方法性能更好,但灵活性较低。

<template>
  <div class="watermark-wrapper">
    <!-- 页面内容 -->
  </div>
</template>

<style>
.watermark-wrapper {
  position: relative;
}
.watermark-wrapper::after {
  content: "Watermark Text";
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  pointer-events: none;
  opacity: 0.1;
  font-size: 24px;
  color: #000;
  display: flex;
  align-items: center;
  justify-content: center;
  transform: rotate(-30deg);
}
</style>

使用第三方库 vue-watermark

对于需要快速集成的情况,可以使用专门的水印库如 vue-watermark

安装依赖:

vue 水印 实现

npm install vue-watermark --save

使用示例:

<template>
  <vue-watermark
    :text="watermarkText"
    :font="watermarkFont"
    :opacity="0.2"
    :width="200"
    :height="150"
  >
    <!-- 需要加水印的内容 -->
  </vue-watermark>
</template>

<script>
import VueWatermark from 'vue-watermark';

export default {
  components: { VueWatermark },
  data() {
    return {
      watermarkText: 'Secret',
      watermarkFont: '16px Arial'
    }
  }
}
</script>

防止水印被删除的安全方案

为防止用户通过开发者工具删除水印,可以采用以下防护措施:

vue 水印 实现

mounted() {
  this.generateWatermark();
  setInterval(() => {
    if (!document.querySelector('.watermark')) {
      this.generateWatermark();
    }
  }, 1000);
}

同时配合 MutationObserver 监听 DOM 变化:

const observer = new MutationObserver((mutations) => {
  mutations.forEach(() => {
    this.checkWatermark();
  });
});
observer.observe(document.body, { 
  childList: true, 
  subtree: true 
});

响应式水印调整

在窗口大小变化时重新计算水印位置和大小:

window.addEventListener('resize', this.handleResize);

handleResize() {
  this.generateWatermark();
}

注意在组件销毁时移除事件监听:

beforeDestroy() {
  window.removeEventListener('resize', this.handleResize);
}

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

相关文章

vue响应式实现

vue响应式实现

Vue 响应式实现原理 Vue 的响应式系统基于 Object.defineProperty(Vue 2.x)或 Proxy(Vue 3.x)实现,通过数据劫持和依赖收集实现数据变化时的自动更新。…

vue实现聊天

vue实现聊天

Vue实现聊天功能 基础项目结构搭建 使用Vue CLI或Vite创建项目,安装必要依赖如socket.io-client或axios(根据通信方式选择)。核心组件通常包括: ChatWindow…

vue实现文档

vue实现文档

Vue 实现文档的方法 在 Vue 中实现文档功能可以通过多种方式,包括使用富文本编辑器、Markdown 解析或直接渲染 HTML。以下是几种常见的实现方法。 使用富文本编辑器 富文本编辑器如 T…

vue 实现长列表

vue 实现长列表

Vue 实现长列表的优化方案 虚拟滚动技术 虚拟滚动是处理长列表的高效方式,仅渲染可视区域内的元素。通过计算滚动位置动态加载和卸载DOM节点,大幅减少内存占用和渲染压力。 <template&…

vue前端实现登录

vue前端实现登录

实现登录功能的基本流程 使用Vue实现登录功能需要结合前端框架和后端API交互。以下是一个典型的实现方案: 安装必要依赖 确保项目已安装axios用于HTTP请求: npm install a…

vue怎么实现页面返回

vue怎么实现页面返回

Vue 实现页面返回的方法 在 Vue 中实现页面返回功能,可以通过以下几种方式完成,具体取决于项目使用的路由模式和技术栈。 使用 Vue Router 的编程式导航 通过 this.$route…