Vue使用自定義指令實(shí)現(xiàn)頁(yè)面底部加水印
給項(xiàng)目的整個(gè)背景加上自定義水印,可以改變水印的文案和字體顏色等
實(shí)現(xiàn)思路 這里使用的技術(shù)主要是canvas,在實(shí)現(xiàn)水印的過(guò)程中,主要使用了canvas的特性 使用 canvas 特性生成 base64 格式的圖片文件,然后設(shè)置其字體大小,顏色等 最后將其設(shè)置為背景圖片,這就實(shí)現(xiàn)了頁(yè)面的水印效果實(shí)現(xiàn)效果

實(shí)現(xiàn)代碼
<template> <div > <div v-waterMarker='{text:’卡洛背心 - 版權(quán)所有’,textColor:’rgba(180, 180, 180, 0.4)’}'><div class='water-marker-item'>測(cè)試問(wèn)題啊測(cè)試問(wèn)題啊測(cè)試問(wèn)題啊測(cè)試問(wèn)題啊測(cè)試問(wèn)題啊測(cè)試問(wèn)題啊</div> </div> </div></template><script>import waterMarker from ’../../directive/test/waterMarker’export default { directives: { waterMarker }, data(){ return{ } }, methods:{ }}</script><style lang='scss'>.water-marker{ height: 300px; .water-marker-item{ line-height: 300px; }}</style>
waterMarker.js文件如下:
function addWaterMarker(str, parentNode, font, textColor) { // 水印文字,父元素,字體,文字顏色 var can = document.createElement(’canvas’) parentNode.appendChild(can) can.width = 200 can.height = 150 can.style.display = ’none’ var cans = can.getContext(’2d’) cans.rotate((-20 * Math.PI) / 180) cans.font = font || ’16px Microsoft JhengHei’ cans.fillStyle = textColor || ’rgba(180, 180, 180, 0.3)’ cans.textAlign = ’left’ cans.textBaseline = ’Middle’ cans.fillText(str, can.width / 10, can.height / 2) parentNode.style.backgroundImage = ’url(’ + can.toDataURL(’image/png’) + ’)’}const waterMarker = { bind: function (el, binding) { addWaterMarker(binding.value.text, el, binding.value.font, binding.value.textColor) },}export default waterMarker
到此這篇關(guān)于Vue使用自定義指令實(shí)現(xiàn)頁(yè)面底部加水印的文章就介紹到這了,更多相關(guān)Vue 頁(yè)面底部加水印內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. idea設(shè)置提示不區(qū)分大小寫(xiě)的方法2. 使用AJAX(包含正則表達(dá)式)驗(yàn)證用戶(hù)登錄的步驟3. Java PreparedStatement用法詳解4. Java實(shí)現(xiàn)的迷宮游戲5. django queryset相加和篩選教程6. IDEA 2020.1.2 安裝教程附破解教程詳解7. Java利用TCP協(xié)議實(shí)現(xiàn)客戶(hù)端與服務(wù)器通信(附通信源碼)8. JS圖片懶加載庫(kù)VueLazyLoad詳解9. 利用ajax+php實(shí)現(xiàn)商品價(jià)格計(jì)算10. Spring如何集成ibatis項(xiàng)目并實(shí)現(xiàn)dao層基類(lèi)封裝

網(wǎng)公網(wǎng)安備