当前位置: 首页 > news >正文

添加水印class封装

const _x = Symbol('x')
const _body = Symbol('body')
const _base64Url = Symbol('base64Url')
class Watermark {props: any;// targetNode: HTMLElement | null;// base64Url: string;// bodyWapper: any;// observerData: Array<any>;
    constructor(props) {this.props = props;this[_base64Url] = '';this[_body] = document.body; // 初始化,默认绑定bodythis[_x ] = []}/*** @description: 增加水印* @param {HTMLElement} wapper 包裹水印的父元素,传null时,默认body* @param {string} userName    水印显示的文本* @return {*}*/    add = (wapper, userName) => {if (wapper) {this[_body] = wapper;}this.creatImg(userName);this.addDom();this._mutationObserve();};addDom = () => {const targetNode = document.createElement('div');targetNode.setAttribute('class','info');targetNode.id = '__water-mark';targetNode.style.position = 'fixed';targetNode.style.left = '0';targetNode.style.top = '0';targetNode.style.width = '100vw';targetNode.style.height = '100vh';targetNode.style.background = `url(${this[_base64Url]}) repeat`;targetNode.style.opacity = '.5';targetNode.style.zIndex = '3000';targetNode.style.pointerEvents = 'none';this[_body] && this[_body].append(targetNode);}creatImg = (userName) => {const cavansDom: any = document.createElement('CANVAS');const ctx = cavansDom.getContext('2d');cavansDom.width = 110;cavansDom.height = 80;ctx.rotate((-20 * Math.PI) / 180);ctx.font = '13px Georgia';if (this.props.color) {ctx.fillStyle = this.props.color;} else {ctx.fillStyle = '#e6e3e3';}if (userName) {ctx.fillText(`${userName}`, 20, 50);} else {ctx.fillText('测试文字', 20, 50);}this[_base64Url] = cavansDom.toDataURL('image/png');}/*** @description: 更换水印文本* @param {string} userName* @return {*}*/    change = (userName) => {this.remove()this.add(null, userName)}/*** @description: 去除水印* @param {** @return {*}*/    remove = () => {const targetNode = document.getElementById('__water-mark')if (!targetNode) {return}this[_x ][0].disconnect()this[_x ][1].disconnect()this[_x ] = []this[_body].remove(targetNode);}_mutationObserve = () => {// 禁止修改水印节点const config = {childList: true,attributes: true,characterData: true,subtree: true,attributeOldValue: true,characterDataOldValue: true,};const mutationCallback = (mutationList) => {console.log(mutationList);for (const mutation of mutationList) {const type = mutation.type;console.log(type);switch (type) {case 'attributes':const targetNode = document.getElementById('__water-mark')if (mutation.attributeName === 'style' && targetNode) {targetNode.style.position = 'fixed';targetNode.style.left = '0';targetNode.style.top = '0';targetNode.style.width = '100vw';targetNode.style.height = '100vh';targetNode.style.background = `url(${this[_base64Url]}) repeat`;targetNode.style.opacity = '.5';targetNode.style.zIndex = '3000';targetNode.style.pointerEvents = 'none';}console.log(`${mutation.attributeName}属性修改了`);break;default:break;}}};const mutationCallback1 = (mutationList) => {console.log(mutationList);for (const mutation of mutationList) {const type = mutation.type;console.log(mutation);switch (type) {case 'childList':if (mutation.removedNodes.length > 0) {mutation.target.append(mutation.removedNodes[0])}console.log('节点被删除或添加');break;case 'subtree':console.log('子树被修改');break;default:break;}}};// 创建 MutationObserver 实例this[_x ][0] = new MutationObserver(mutationCallback);// 开始监控目标节点const targetNode = document.getElementById('__water-mark')this[_x ][0].observe(targetNode, config);// 创建 MutationObserver 实例this[_x ][1] = new MutationObserver(mutationCallback1);// 开始监控目标节点this[_x ][1].observe(this[_body], config);// 停止监控// observer.disconnect()
    };}export default Watermark;

使用方式:

const wapper = document.getElementById('xxx') // 包裹水印的父元素,如果传递的不是dom对象,默认body
const userName = 'xxx'                        // 要显示的水印文本,必须传字符串
Watermark.add(wapper, userName);//Watermark.remove(); // 谨慎调用

Watermark.change('xxx'); // 修改水印的文本

 

http://www.jsqmd.com/news/29864/

相关文章:

  • 企业级AI知识库到底能做什么?PandaWiki重新定义知识生产力
  • 对象存储系统MinIO详细以及部署
  • 基于模拟电荷法的MATLAB输电线路铁塔电场分布计算
  • MATLAB动态规划设备分配
  • Jmeter汉化成中文版
  • 2025年新疆地坪厂家权威推荐榜单:环氧树脂地坪漆/环氧彩砂地坪/透水地坪源头厂家精选
  • 用docker搭建selenium grid分布式环境
  • 一文掌握RMAN基础入门:核心概念、环境配置与命令格式全解析
  • UNI-APP设计电池电量显示
  • 2025 年 11 月皮拉尼真空计,单晶炉真空计厂家最新推荐,技术实力与市场口碑深度解析
  • 2025年知名的异型轴承座实力厂家TOP推荐榜
  • springboot 读取配置文件方式
  • 启动Coze报了一个elasticsearch启动错误
  • 2025 年 11 月压阻硅真空计,薄膜硅真空计,陶瓷电容真空计厂家最新推荐,实力品牌深度解析采购无忧之选!
  • 251103
  • 2025年浙江离婚律师权威推荐榜单:离婚财产纠纷律师/离婚纠纷律师/婚姻律师团队精选
  • 2025年诚信的东莞温升试验机厂家实力及用户口碑排行榜
  • Oracle 手工备份恢复:DBA 必学的兜底技能,从原理到实操一步到位
  • 2025年诚信的十级无尘车间品牌厂家排行榜
  • 2025 年 11 月高精度红外测温仪,双色红外测温仪,温炉红外测温仪厂家最新推荐,技术实力与市场口碑深度解析
  • fastp数据质控
  • 2025年保洁服务公司新排行榜推荐,海獭顾家保洁服务反馈/能力大揭秘
  • 2025年热门的不锈钢磁力泵厂家推荐及采购参考
  • 2025.11.3博客
  • 2025 年 11 月比色红外测温仪,感应加热红外测温仪,高性价比红外测温仪,单晶炉红外测温仪厂家最新推荐,技术实力与市场口碑深度解析
  • 2025年度有实力的不锈钢编织网推荐制造商排名:资质齐全的不锈钢编织网厂家权威测评
  • 2025年包装机械行业年度排名推荐:常熟市奇威包装机械限公司
  • 2025年耐用的多风机除尘设备厂家最新热销排行
  • 2025年知名的混凝土水沟滑模机厂家推荐及采购指南
  • 2025年中国灌装机设备企业年度排名:张家港蓝海机械有限公司