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

vue 页面加水印

首先创建一个waterMark.js文件,当然文件命名可自定义,

'use strict'const watermark = {}/**** @param {要设置的水印的内容} str* @param {需要设置水印的容器} container*/
const setWatermark = (str, container) => {const id = '1.23452384164.123412415'if (container === undefined) {return}// 查看页面上有没有,如果有则删除if (document.getElementById(id) !== null) {const childelement = document.getElementById(id)childelement.parentNode.removeChild(childelement)}var containerWidth = container.offsetWidth // 获取父容器宽var containerHeight = container.offsetHeight // 获取父容器高container.style.position = 'relative' // 设置布局为相对布局// 创建canvas元素(先制作一块背景图)const can = document.createElement('canvas')can.width = 200 // 设置每一块的宽度can.height = 200 // 高度can.size= 20const cans = can.getContext('2d') // 获取canvas画布cans.rotate(-20 * Math.PI / 180) // 逆时针旋转π/9  cans.font = '20px Vedana' // 设置字体cans.fillStyle = 'rgba(0, 0, 0, 0.20)' // 设置字体的颜色cans.textAlign = 'left' // 文本对齐方式cans.textBaseline = 'Middle' // 文本基线cans.fillText(str, 0, 4 * can.height / 5) // 绘制文字// 创建一个div元素const div = document.createElement('div')div.id = id // 设置iddiv.style.pointerEvents = 'none' // 取消所有事件div.style.top = '0px'div.style.left = '0px'div.style.position = 'absolute'div.style.zIndex = '100000'div.style.width = containerWidth + 'px'div.style.height = containerHeight + 'px'div.style.background = 'url(' + can.toDataURL('image/png') + ') left top repeat'container.appendChild(div) // 追加到页面return id
}// 该方法只允许调用一次
watermark.set = (str, container) => {let id = setWatermark(str, container)setInterval(() => {if (document.getElementById(id) === null) {id = setWatermark(str, container)}}, 500)// 监听页面大小的变化window.onresize = () => {setWatermark(str, container)}
}export default watermark
<div ref="fristhome">vue 页面</div>

在需要的加水印的页面导入watermark.js文件

import  waterMark from "../../utils/waterMark"
// 这是笔者的文件路径,具体的根据个人所放的路径即可,

方法中调用

waterMark.set('水印', this.$refs.fristhome)

效果:
在这里插入图片描述

http://www.lryc.cn/news/159467.html

相关文章:

  • Android ImageView详解
  • ElasticSearch第二讲:ES详解 - ElasticSearch基础概念
  • Ajax模拟视频点赞功能
  • java解决 衣服尺码 Compare T-Shirt Sizes
  • 基于python+Django深度学习的音乐推荐方法研究系统设计与实现
  • 【枚举区间+线段树】CF Ehu 152 E
  • 宏定义天坑记录
  • Git的一些常用概念与操作方法分享
  • webpack实战:某网站JS逆向分析
  • 826. 安排工作以达到最大收益;2257. 统计网格图中没有被保卫的格子数;816. 模糊坐标
  • JAVA毕业设计097—基于Java+Springboot+Vue+uniapp的医院挂号小程序系统(源码+数据库)
  • 4.3.3.1 【MySQL】CHAR(M)列的存储格式
  • js 处理数组合并vs对象合并
  • Webpack vs Vite的核心差异
  • 53、springboot对websocket的支持有两种方式-------1、基于注解开发 WebSocket ,简洁实现多人聊天界面
  • 18 Linux之Python定制篇-Python开发平台Ubuntu
  • AMEYA360:士兰微推出600A/1200V IGBT汽车驱动模块,提升充电速度与行驶动力
  • 【Linux】Epoll Reactor【反应堆】模式的工作流程
  • Php“梦寻”淘宝天猫商品详情数据接口,淘宝商品详情数据API接口,淘宝API接口申请指南(含代码示例)
  • 驱动轴相机参数设置Web前端界面开发
  • 论文简读 LORA: LOW-RANK ADAPTATION OF LARGE LANGUAGE MODELS
  • 23062网络编程day7
  • Java面向对象学习笔记-2
  • 入栏需看——学习记忆
  • [C++]杨辉三角
  • 算法通关村十三关-白银:数字与数学高频问题
  • 【Linux】线程安全-互斥同步
  • 1.初识爬虫
  • TLA+学习记录1——hello world
  • 基于QWebEngine实现无头浏览器