Vue如何给页面加水印(超简单)

5 篇文章 0 订阅

给页面加上水印

  • 用canvas画一张背景图
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();
    }
}
  • 需要添加的页面引入
<template>
    <div class="content" :style="{backgroundImage:`url(${orgBackground})`}">123</div>
</template>

<script>
import tool from "../plus/tool";
export default {
    data() {
        return {
            orgBackground: "",
        };
    },
    name: "test",
    mounted() {
        localStorage.setItem("loginUserName", "ruan");
        localStorage.setItem("logunUserPhone", "123456");
        //实际只需要一行代码
        this.orgBackground = tool.addWaterMark();
        //监听dom属性改变重新生成水印(如果页面有变化需要去监听dom)
        // const org = document.querySelector(".content");
        // const options = {
        //     attributes: true,
        //     childList: true,
        //     subtree: true,
        // };
        // const observer = new MutationObserver((mutationList) => {
        // this.orgBackground = tool.addWaterMark();
        // });
        // observer.observe(org, options);
    },
};
</script>

<style lang='less' scoped>
.content {
    position: relative;
    width: 100vw;
    height: 100vh;
}
</style>
</script>

在这里插入图片描述

  • 2
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
你可以使用 CSS3 的伪元素 ::after 来实现页面水印效果,同时使用 JavaScript 获取系统时间来显示水印内容。以下是一个示例代码: HTML: ```html <div id="watermark"> <div class="watermark-content"> <span>系统时间:</span> <span id="system-time"></span> </div> </div> ``` CSS: ```css #watermark { position: fixed; top: 0; left: 0; right: 0; bottom: 0; z-index: -1; pointer-events: none; } .watermark-content { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); font-size: 16px; color: rgba(0,0,0,0.2); } .watermark-content::after { content: ''; position: absolute; top: 0; left: 0; right: 0; bottom: 0; z-index: -1; background-image: url('data:image/svg+xml,%3Csvg xmlns="http://www.w3.org/2000/svg" width="100" height="100" viewBox="0 0 100 100"%3E%3Ctext x="50%" y="50%" dy="0.4em" text-anchor="middle" font-size="35"%3EWatermark%3C/text%3E%3C/svg%3E'); background-repeat: repeat; opacity: 0.2; } ``` JavaScript: ```javascript function updateSystemTime() { const now = new Date(); const year = now.getFullYear(); const month = now.getMonth() + 1; const date = now.getDate(); const hour = now.getHours(); const minute = now.getMinutes(); const second = now.getSeconds(); const systemTime = `${year}-${month}-${date} ${hour}:${minute}:${second}`; document.getElementById('system-time').textContent = systemTime; } setInterval(updateSystemTime, 1000); ``` 你可以将以上代码保存为一个 vue 文件,然后在需要显示水印页面中引入即可。注意,以上代码只是一个简单的示例,你需要根据实际情况进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值