美文网首页
vue页面设置水印

vue页面设置水印

作者: jesse28 | 来源:发表于2021-03-03 16:59 被阅读0次

    参考链接:https://blog.csdn.net/weixin_44205155/article/details/104961334

    1.创建watermark.js文件


    image.png
    
    'use strict'
    
    const watermark = {}
    
    const setWatermark = (str) => {
        const id = '1.23452384164.123412415'
    
        if (document.getElementById(id) !== null) {
            document.body.removeChild(document.getElementById(id))
        }
    
        const can = document.createElement('canvas')
        can.width = 320  //这里表示水印的内容修改长度
        can.height = 320  //这里表示水印的内容修改高度
    
        const cans = can.getContext('2d')
        cans.rotate(-20 * Math.PI / 180)
        cans.font = '20px Vedana'
        cans.fillStyle = 'rgba(200, 200, 200, 0.20)'
        cans.textAlign = 'left'
        cans.textBaseline = 'Middle'
        cans.fillText(str, can.width / 3, can.height / 2)
    
        const div = document.createElement('div')
        div.id = id
        div.style.pointerEvents = 'none'
        div.style.top = '70px'
        div.style.left = '10px'
        div.style.position = 'fixed'
        div.style.zIndex = '100000'
        div.style.width = document.documentElement.clientWidth - 10 + 'px'
        div.style.height = document.documentElement.clientHeight - 100 + 'px'
        div.style.background = 'url(' + can.toDataURL('image/png') + ') left top repeat'
        document.body.appendChild(div)
        return id
    }
    
    // 该方法只允许调用一次
    watermark.set = (str) => {
        let id = setWatermark(str)
        setInterval(() => {
            if (document.getElementById(id) === null) {
                id = setWatermark(str)
            }
        }, 500)
        window.onresize = () => {
            setWatermark(str)
        }
    }
    const outWatermark = (id) => {
        if (document.getElementById(id) !== null) {
            const div = document.getElementById(id)
            div.style.display = 'none'
        }
    }
    
    watermark.out = () => {
        const str = '1.23452384164.123412415'
        outWatermark(str)
    }
    
    
    export default watermark
    
    

    2.在permission.js引入waterMark.js文件


    image.png
    >permission.js文件的主要代码
    
    router.afterEach(() => {
      // finish progress bar
      NProgress.done();
      const title = getToken()
      if (title) {
        waterMark.set("欢迎大家来一起") // 设置水印title
      } else {
        waterMark.out() // 清除水印
      }
    
    })
    

    相关文章

      网友评论

          本文标题:vue页面设置水印

          本文链接:https://www.haomeiwen.com/subject/mevqqltx.html