我是靠谱客的博主 温婉书包,这篇文章主要介绍vue 给页面局部加水印和全部加水印页面全部加水印,现在分享给大家,希望可以做个参考。

局部加水印

在公共类js下,新建一个watermask.js,代码如下

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
export default { //tool.js addWaterMark() { const strArr = `${localStorage.getItem( "loginUserName" )}${localStorage .getItem("logunUserPhone") .slice(7, 11)}[${new Date()}]`; let ctx = document.createElement("canvas"); ctx.width = 250; ctx.height = 100; ctx.style.display = "none"; let cans = ctx.getContext("2d"); cans.rotate((-20 * Math.PI) / 180); cans.font = "12px Microsoft YaHei"; cans.fillStyle = "rgba(17, 17, 17, 0.20)"; cans.textAlign = "left"; cans.textBaseline = "Middle"; cans.fillText(strArr, 0, 100); cans.save(); return ctx.toDataURL(); } }

在需要的页面引入

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<template> //绑定样式 <el-table :data="dataList" border :style="{backgroundImage:`url(${orgBackground})`}"> </template> <script> //引入js import watermark from "@/tools/watermark"; export default { data() { return { orgBackground: "", }; }, mounted() { localStorage.setItem("loginUserName", "哈哈哈"); localStorage.setItem("logunUserPhone", "123456"); this.orgBackground = watermark.addWaterMark(); }, }; </script> <style lang='less' scoped> </style> </script>

页面全部加水印

同样在公共类js里新建一个watermark.js,代码如下

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/** 水印添加方法 */ let setWatermark = (str1, str2) => { let id = '1.23452384164.123412415' if (document.getElementById(id) !== null) { document.body.removeChild(document.getElementById(id)) } let can = document.createElement('canvas') // 设置canvas画布大小 can.width = 150 can.height = 80 let cans = can.getContext('2d') cans.rotate(-20 * Math.PI / 180) // 水印旋转角度 cans.font = '15px Vedana' cans.fillStyle = '#666666' cans.textAlign = 'center' cans.textBaseline = 'Middle' cans.fillText(str1, can.width / 2, can.height) // 水印在画布的位置x,y轴 cans.fillText(str2, can.width / 2, can.height + 22) let div = document.createElement('div') div.id = id div.style.pointerEvents = 'none' div.style.top = '40px' div.style.left = '0px' div.style.opacity = '0.15' div.style.position = 'fixed' div.style.zIndex = '100000' div.style.width = document.documentElement.clientWidth + 'px' div.style.height = document.documentElement.clientHeight + 'px' div.style.background = 'url(' + can.toDataURL('image/png') + ') left top repeat' document.body.appendChild(div) return id } // 添加水印方法 export const setWaterMark = (str1, str2) => { let id = setWatermark(str1, str2) if (document.getElementById(id) === null) { id = setWatermark(str1, str2) } } // 移除水印方法 export const removeWatermark = () => { let id = '1.23452384164.123412415' if (document.getElementById(id) !== null) { document.body.removeChild(document.getElementById(id)) } }

然后在页面中引入

复制代码
1
2
3
4
5
6
7
import { removeWatermark, setWaterMark } from '@/common/watermark' mounted() { setWaterMark('hahaha', '哈哈哈'); }, destroyed() { removeWatermark(); },

以上两种方式均已尝试,可以使用,具体选那种方式根据项目需求

转载链接:局部加水印,https://blog.csdn.net/qq_44775782/article/details/107681734

单页面加水印:https://www.jqhtml.com/62620.html

最后

以上就是温婉书包最近收集整理的关于vue 给页面局部加水印和全部加水印页面全部加水印的全部内容,更多相关vue内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(68)

评论列表共有 0 条评论

立即
投稿
返回
顶部