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

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

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');
    },
},
相关推荐
CHU72903516 分钟前
定制专属美丽时刻:美容预约商城小程序的贴心设计
前端·小程序
浩~~1 小时前
反射型XSS注入
前端·xss
AwesomeDevin1 小时前
AI时代,我们的任务不应沉溺于与 AI 聊天,🤔 从“对话式编程”迈向“数字软件工厂”
前端·后端·架构
harrain1 小时前
antvG2折线图和区间range标记同时绘制
前端·javascript·vue.js·antv·g2
德育处主任Pro1 小时前
从重复搭建到高效生产,RollCode的H5开发新范式
前端
蜡台2 小时前
SPA(Single Page Application) Web 应用(即单页应用)架构模式 更新
前端·架构·vue·react·spa·spa更新
网络点点滴2 小时前
组件通信-作用域插槽
前端·javascript·vue.js
kyriewen113 小时前
异步编程:从“回调地狱”到“async/await”的救赎之路
开发语言·前端·javascript·chrome·typescript·ecmascript·html5
Old Uncle Tom3 小时前
Markdown Viewer 再升级
前端
Luna-player3 小时前
Vue3中使用vue-awesome-swiper
前端·vue.js·arcgis