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

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

1、创建水印组件
javascript 复制代码
<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、在公共组件使用水印组件

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

javascript 复制代码
<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)控制显隐

javascript 复制代码
// 以下例子以登录页不显示水印为例
<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');
    },
},
相关推荐
臣妾没空8 分钟前
里程碑5:完成框架npm包抽象封装并发布
前端·npm
Wect8 分钟前
LeetCode 210. 课程表 II 题解:Kahn算法+DFS 双解法精讲
前端·算法·typescript
cxxcode12 分钟前
搞懂 JS 异步的底层真相:从 V8 源码看微任务与宏任务
前端
欧阳的棉花糖12 分钟前
React 小误区:派生值 vs useEffect
前端
马可菠萝16 分钟前
从零开始,用 Tauri + Vue 3 打造轻量级桌面应用
前端
陆枫Larry17 分钟前
JavaScript 字符串处理实战:从 `startsWith` 到链式 `replace` 的避坑指南
前端
天蓝色的鱼鱼33 分钟前
你的项目真的需要SSR吗?还是只是你的简历需要?
前端·架构
恋猫de小郭1 小时前
移动端开发稳了?AI 目前还无法取代客户端开发,小红书的论文告诉你数据
前端·flutter·ai编程
文心快码BaiduComate1 小时前
百度云与光本位签署战略合作:用AI Agent 重构芯片研发流程
前端·人工智能·架构
闲云一鹤2 小时前
nginx 快速入门教程 - 写给前端的你
前端·nginx·前端工程化