当前位置:首页 > VUE

vue实现列表水印

2026-02-09 11:01:18VUE

实现列表水印的方法

使用CSS背景图

通过CSS的background-image属性为列表容器添加水印背景。水印可以是文字或图片,通过调整透明度实现半透明效果。

<template>
  <div class="watermarked-list">
    <ul>
      <li v-for="item in items" :key="item.id">{{ item.text }}</li>
    </ul>
  </div>
</template>

<style>
.watermarked-list {
  background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="200" height="200" opacity="0.1"><text x="50%" y="50%" font-size="20" text-anchor="middle" dominant-baseline="middle" transform="rotate(-45, 100, 100)">WATERMARK</text></svg>');
  background-repeat: repeat;
}
</style>

使用Canvas动态生成水印

通过Canvas绘制水印文字,将Canvas转为图片后设置为列表背景。这种方式可以动态调整水印内容和样式。

export default {
  methods: {
    generateWatermark(text) {
      const canvas = document.createElement('canvas');
      canvas.width = 200;
      canvas.height = 200;
      const ctx = canvas.getContext('2d');
      ctx.font = '20px Arial';
      ctx.fillStyle = 'rgba(0, 0, 0, 0.1)';
      ctx.textAlign = 'center';
      ctx.textBaseline = 'middle';
      ctx.translate(100, 100);
      ctx.rotate(-Math.PI / 4);
      ctx.fillText(text, 0, 0);
      return canvas.toDataURL('image/png');
    }
  },
  mounted() {
    const watermark = this.generateWatermark('CONFIDENTIAL');
    document.querySelector('.watermarked-list').style.backgroundImage = `url(${watermark})`;
  }
}

使用伪元素叠加

通过CSS伪元素在列表容器上叠加水印内容,这种方式不需要修改DOM结构。

.watermarked-list {
  position: relative;
}
.watermarked-list::after {
  content: "WATERMARK";
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 48px;
  color: rgba(0, 0, 0, 0.1);
  pointer-events: none;
  transform: rotate(-45deg);
}

使用第三方库

对于复杂的水印需求,可以使用专门的水印库如vue-watermark。安装后简单配置即可使用。

vue实现列表水印

import VueWatermark from 'vue-watermark'

Vue.use(VueWatermark, {
  text: 'SECRET',
  opacity: 0.2,
  width: 200,
  height: 200,
  rotate: -45
})
<template>
  <div v-watermark>
    <ul>
      <li v-for="item in items" :key="item.id">{{ item.text }}</li>
    </ul>
  </div>
</template>

每种方法各有优缺点,CSS背景图最简单但不够灵活,Canvas可以动态生成但稍复杂,伪元素不需要额外DOM节点,第三方库功能最全面但增加依赖。根据具体需求选择合适方案。

标签: 水印列表
分享给朋友:

相关文章

vue 实现长列表

vue 实现长列表

vue 实现长列表的优化方法 使用虚拟滚动技术,只渲染可视区域内的元素,大幅减少DOM节点数量。通过计算滚动位置动态更新显示内容,降低内存占用和渲染压力。 <template> &l…

vue实现列表

vue实现列表

实现列表的基本方法 在Vue中实现列表通常使用v-for指令,这是Vue的核心功能之一。v-for可以遍历数组或对象,为每个元素生成对应的DOM节点。 <template> <…

vue实现列表组件

vue实现列表组件

Vue 列表组件实现方法 基础列表渲染 使用 v-for 指令渲染数组数据,需配合 :key 提升性能: <template> <ul> <li v-for…

vue实现水印效果

vue实现水印效果

实现水印效果的几种方法 在Vue中实现水印效果可以通过多种方式,以下是几种常见的方法: 使用CSS背景图 通过CSS的background-image属性可以实现简单的水印效果。定义一个包含水印文本…

vue实现列表点击

vue实现列表点击

Vue 实现列表点击的方法 在 Vue 中实现列表点击功能通常涉及 v-for 渲染列表和 v-on(或 @)绑定点击事件。以下是几种常见实现方式: 基础列表点击 使用 v-for 渲染列表,并通过…

vue实现分类列表

vue实现分类列表

Vue 实现分类列表的方法 数据结构设计 使用数组存储分类数据,每个分类对象包含 id、name 等属性,子分类通过 children 字段嵌套。例如: categories: [ {…