实习随笔【前端技术实现全局添加水印】

        有一些数据比较重要的项目,往往需要对数据进行保护措施,本文介绍常见策略——全局添加水印。

1、创建水印组件
<template>
    <div class="water-mark">
        <div class="water-mark-content">
            <span class="phone">{{ phone }}</span>
            <span class="time">{{ time }}</span>
        </div>
    </div>
</template>
<script>
export default {
    name: 'WaterMark',
    props: {
        phone: {
            type: String,
            required: true,
        },
        time: {
            type: Number,
            required: true,
        },
    },
};
</script>
<style scoped>
  .water-mark {
    position: relative;
    width: 300px;
    height: 300px;
    background: url('../../assets/logo.png') no-repeat center;
    background-size: 80px;
    filter: brightness(0.7);
    pointer-events: none;
    color: rgba(26, 26, 26, 0.3);
    font-size: 10px;
    transform: rotate(-45deg);
    display: inline-block;
    opacity: 0.5;
  }
  .water-mark-content {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    text-align: center;
  }
  .phone {
    display: block;
    position: absolute;
    top: -15px;
    left: 50px;
  }
  .time {
    display: block;
    position: absolute;
    top: 0px;
    left: 50px;
  }
</style>
2、在公共组件使用水印组件 

        这里需要注意,水印的位置以及多少可由样式具体决定

<div class="water-wrap">
    <WaterMark
        v-for="(item, index) in waterArr"
        :key="`water-mark-${index}`"
        :phone="userInfo.username"
        :time="time.getTime()"
    />
</div>

// 水印内容——当前时间
time = new Date()

// 水印数组控制水印数量
waterArr = Array(100).fill(1)

// 水印样式控制水印显示区域
.water-wrap {
    position: fixed;
    bottom: 0;
    left: 0;
    top: 0;
    right: 0;
    overflow: hidden;
    z-index: 100000;
    pointer-events: none;
 }
3、对于不需要水印的页面(登录页)

        可以用计算属性来判断,并用v-if(vue)或&&(react)控制显隐

// 以下例子以登录页不显示水印为例
<div v-if="isLoginPage" class="water-wrap">
    <WaterMark
        v-for="(item, index) in waterArr"
        :key="`water-mark-${index}`"
        :phone="userInfo.username"
        :time="time.getTime()"
    />
</div>

computed: {
    isLoginPage() {
        // 检查当前路由路径是否包含 '/login'
        return !this.$route.path.includes('/login');
    },
},

相关推荐

  1. 实习随笔前端技术实现全局添加水印

    2024-07-16 02:18:04       21 阅读
  2. SpringBoot实现PDF添加水印

    2024-07-16 02:18:04       43 阅读
  3. SpringBoot 实现 PDF 添加水印

    2024-07-16 02:18:04       51 阅读
  4. SpringBoot实现 PDF 添加水印

    2024-07-16 02:18:04       36 阅读
  5. canvas实现给照片添加水印

    2024-07-16 02:18:04       25 阅读
  6. 前端实现水印效果的多种方案

    2024-07-16 02:18:04       33 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-16 02:18:04       66 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-16 02:18:04       70 阅读
  3. 在Django里面运行非项目文件

    2024-07-16 02:18:04       57 阅读
  4. Python语言-面向对象

    2024-07-16 02:18:04       68 阅读

热门阅读

  1. (day17)其他分组不明确的题

    2024-07-16 02:18:04       21 阅读
  2. no-fee服务器玩转LLM

    2024-07-16 02:18:04       18 阅读
  3. lvs是什么

    2024-07-16 02:18:04       17 阅读
  4. UDP传输文件和FTP传输文件

    2024-07-16 02:18:04       20 阅读
  5. flutter Android端权限

    2024-07-16 02:18:04       19 阅读
  6. .NET在工控上位机开发中有哪些成功的案例?

    2024-07-16 02:18:04       22 阅读
  7. 最短路之朴素版的dij板子

    2024-07-16 02:18:04       18 阅读
  8. c++ 生成随机字符串

    2024-07-16 02:18:04       21 阅读
  9. 顺序表(C语言)

    2024-07-16 02:18:04       18 阅读
  10. 堆、栈和队列(数据结构)

    2024-07-16 02:18:04       21 阅读
  11. 跨越空间的编码:在PyCharm中高效使用远程解释器

    2024-07-16 02:18:04       18 阅读