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

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

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');
    },
},
相关推荐
百***359415 分钟前
如何在树莓派部署Nginx并实现无公网ip远程访问内网制作的web网站
前端·tcp/ip·nginx
花果山总钻风21 分钟前
Chrome 插件框架 Plasmo 基本使用示例
前端·chrome
资讯第一线23 分钟前
《Chrome》 [142.0.7444.60][绿色便携版] 下载
前端·chrome
会篮球的程序猿35 分钟前
原生表格文本过长展示问题,参考layui长文本,点击出现文本域
前端·javascript·layui
top_designer36 分钟前
Firefly 样式参考:AI 驱动的 UI 资产“无限”生成
前端·人工智能·ui·aigc·ux·设计师
蜗牛前端1 小时前
使用 Trae AI 开发完整的开源 npm 包:snail-git-add
前端
Dontla1 小时前
React useMemo(当依赖项未变化,重复渲染时直接返回上一次缓存计算结果,而非重新执行计算)
前端·react.js·缓存
花生Peadar1 小时前
AI编程从入门到精通
前端·后端·代码规范
bug爱好者1 小时前
vue3.x 使用vue3-tree-org实现组织架构图 + 自定义模版内容 - 附完整示例
前端·javascript·vue.js
达达尼昂2 小时前
🎯 Flutter 拖拽选择组件:flutter_drag_selector —— 像选文件一样选择列表项
前端·flutter