项目中添加水印效果
vue 项目中添加水印效果
方法 1 通过 waterMark.js 实现
- 水印会随着页面滚动而滚动
-
waterMark.js
const watermark = {}; const setWatermark = (str, targetDom) => { const id = "watermark"; if (document.getElementById(id) !== null) { document.body.removeChild(document.getElementById(id)); } const can = document.createElement("canvas"); can.width = window.screen.width; can.height = window.screen.height; const cans = can.getContext("2d"); cans.rotate((-20 * Math.PI) / 180); cans.font = "20px serif"; cans.fillStyle = "rgba(180, 180, 180, 0.3)"; cans.textAlign = "left"; cans.textBaseline = "Middle"; for (let i = -can.width / 10; i < can.width / 10; i++) { for (let j = -can.height / 20; j < can.height / 20; j++) { cans.fillText(str, i * 150, j * 80); } } const div = document.createElement("div"); div.id = id; div.style.pointerEvents = "none"; // div.style.position = "fixed"; // 水印固定 // div.style.top = targetDom.offsetTop + "px"; // div.style.left = targetDom.offsetLeft + "px"; div.style.top = "0px"; div.style.left = "0px"; div.style.position = "absolute"; div.style.zIndex = "100000"; div.style.width = targetDom.clientWidth + "px"; div.style.height = targetDom.clientHeight + "px"; div.style.background = "url(" + can.toDataURL("image/png") + ") left top repeat"; // document.body.appendChild(div); // 添加到boyd targetDom.appendChild(div); // 添加到父级 return id; }; // 如果水印存在 展示水印 如果不存在 创建之后展示 // targetDom 是水印要盖住的元素 watermark.set = (str, targetDom) => { if (document.getElementById("watermark")) { document.getElementById("watermark").style.display = "block"; } else { let id = setWatermark(str, targetDom); const timer = setInterval(() => { if (document.getElementById(id) === null) { id = setWatermark(str, targetDom); } else { clearInterval(timer); } }, 2000); window.onresize = () => { setWatermark(str, targetDom); }; } }; // 页面切换时展示水印 因为项目组件是keep-alive的 所以在beforeRouteEnter的时候调这个 watermark.show = () => { if (document.getElementById("watermark")) { document.getElementById("watermark").style.display = "block"; } }; // beforeRouteLeave切换其他页面时可能需要隐藏水印 watermark.hide = () => { document.getElementById("watermark").style.display = "none"; }; // 移除水印 watermark.remove = () => { document.getElementById("watermark").remove(); }; export default watermark;
-
index.vue
<template> <div class="detail"> <div id="detailId"> <div @click="ontest">测试</div> <div style="height: 300px"> 1 </div> <div style="height: 300px"> 2 </div> <div style="height: 300px"> 3 </div> <div style="height: 300px"> 4 </div> <div style="height: 300px"> 5 </div> </div> </div> </template> <script lang="ts"> import { Component, Vue } from "vue-property-decorator"; import watermark from "./waterMark.js"; @Component({ name: "", components: {} }) export default class extends Vue { ontest() { this.$router.push("/detail"); } mounted() { watermark.set("水印", document.getElementById("detailId")); // } getCommonDate(data) { if (data) { return this.$moment(data).format("YYYY-MM-DD"); } else { return ""; } } destroyed() { watermark.remove(); } } </script> <style lang="scss" scoped> .detail { width: 100%; height: 100%; background-color: #fff; padding: 10px; box-sizing: border-box; #detailId { position: relative; } } </style>
方法 2 通过 watermark-dom 插件实现
- 效果图
npm 安装 watermark-dom
- 水印固定显示
- 安装
- npm install watermark-dom --S
-
引入
-
import watermark from "watermark-dom";
-
使用
const options = { watermark_txt: "测试水印", watermark_color: "gray", watermark_fontsize: "24px", }; watermark.load(options);
-
移除
-
watermark.remove();
-
原文链接: https://blog.csdn.net/Shids_/article/details/140521115
-
detail.vue
<template> <div class=""> <div style="height: 300px"> 11 </div> <div style="height: 300px"> 21 </div> <div style="height: 300px"> 31 </div> <div style="height: 300px"> 41 </div> <div style="height: 300px"> 51 </div> <div style="height: 300px"> 61 </div> <div style="height: 300px"> 71 </div> </div> </template> <script lang="ts"> import { Component, Vue } from "vue-property-decorator"; import watermark from "watermark-dom"; @Component({ name: "", components: {} }) export default class extends Vue { mounted() { const options = { watermark_txt: "测试水印", watermark_color: "gray", watermark_fontsize: "24px", }; watermark.load(options); } destroyed() { watermark.remove(); } } </script> <style lang="scss" scoped> </style>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2019-11-21 时间水平坐标轴
2017-11-21 Canvas开发库封装
2017-11-21 封装一个构造函数 绘制矩形
2017-11-21 canvas制作柱形图/折线图/饼状图,Konva写动态饼状图