尧图网站建设 尧图网络
  • 首页
  • 关于我们
  • 服务项目
  • 案例展示
  • 建站流程
  • 资讯中心
  • 联系我们
首页/资讯中心/详情

添加水印class封装

添加水印class封装
📅 发布时间:2026/6/19 19:03:55
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'); // 修改水印的文本

 

相关新闻

  • 基于模拟电荷法的MATLAB输电线路铁塔电场分布计算
  • 2025年新疆地坪厂家权威推荐榜单:环氧树脂地坪漆/环氧彩砂地坪/透水地坪源头厂家精选
  • MATLAB动态规划设备分配

最新新闻

  • 2026厦门百达翡丽回收实力排行榜!本地七大正规机构权威排名 - 薛定谔的梨花猫
  • eNSP - BGP 诊断命令实战指南
  • 北京黄金回收避坑攻略:认准“秤不改、火不假、价不虚”这三大准测 - 奢侈品回收测评
  • 联调测试:问题都藏在边界里
  • 覆盖上海近郊全域网点,2026 黄金回收城郊门店综合参考榜单 - 奢侈品回收测评
  • 技术深度解析:Win11Debloat如何实现Windows系统优化与隐私保护架构

日新闻

  • 5分钟掌握Python进化算法:Geatpy高性能优化工具完全指南
  • Microchip 24AA044 EEPROM选型与应用全指南:从参数解析到实战编程
  • 华为的鸿蒙到底有多牛?为什么称作遥遥领先?

周新闻

  • 3步解锁iOS设备:applera1n激活锁绕过完全指南
  • 39 2026 人工智能证书终极盘点,普通人选 AI 证书可以从这些方向入手
  • Redis 暴露公网有多危险?从端口检查到补救步骤

月新闻

  • 【总结】入门篇:50句话让你记住架构核心概念
  • WeChatMsg技术方案解析:实现Mac微信数据自主管理的完整解决方案
  • WeChatMsg:革新性微信数据备份方案,打造你的专属数字记忆库

关于尧图

  • 公司简介
  • 团队介绍
  • 企业文化
  • 荣誉资质

服务项目

  • 定制开发
  • 电商建站
  • UI 设计
  • 运维服务

快速链接

  • 案例展示
  • 建站流程
  • 常见问题
  • 资讯中心

联系方式

  • 📍北京市朝阳区互联网产业园 A 座 10 层
  • 📞400-888-8888
  • ✉️contact@rkmt.cn
  • 🕐周一至周日 9:00-21:00

© 2024 北京尧图网络科技有限公司 版权所有 | 京 ICP 备 XXXXXXXX 号