js 添加水印

 

index.js:

 

export const watermark = (settings) =>{

// 默认设置

var defaultSettings = {

watermark_txt: 'text',

watermark_x: 20, // 水印起始位置x轴坐标

watermark_y: 20, // 水印起始位置Y轴坐标

watermark_rows: 0, // 水印行数

watermark_cols: 20, // 水印列数

watermark_x_space: 100, // 水印x轴间隔

watermark_y_space: 50, // 水印y轴间隔

watermark_color: '#aaa', // 水印字体颜色

watermark_alpha: 0.2, // 水印透明度

watermark_fontsize: '15px', // 水印字体大小

watermark_font: '微软雅黑', // 水印字体

watermark_width: 220, // 水印宽度

watermark_height: 80, // 水印长度

watermark_angle: 15, // 水印倾斜度数

element: document.body

}

 

// 采用配置项替换默认值,作用类似jquery.extend

if (typeof settings === 'object') {

var src = settings || {}

for (let key in src) {

if (src[key] && defaultSettings[key] && src[key] === defaultSettings[key]) { continue } else if (src[key]) { defaultSettings[key] = src[key] }

}

}

 

var oTemp = document.createDocumentFragment()

 

// 获取页面最大宽度

var pageWidth = Math.max(document.body.scrollWidth, document.body.clientWidth) || 1280

var cutWidth = pageWidth * 0.0150

pageWidth = pageWidth - cutWidth

// 获取页面最大高度

var pageHeight = Math.min(document.body.scrollHeight, document.body.clientHeight) || 768

// var pageHeight = document.body.scrollHeight+document.body.scrollTop;

// 如果将水印列数设置为0,或水印列数设置过大,超过页面最大宽度,则重新计算水印列数和水印x轴间隔

if (defaultSettings.watermark_cols === 0 || (parseInt(defaultSettings.watermark_x + defaultSettings.watermark_width * defaultSettings.watermark_cols + defaultSettings.watermark_x_space * (defaultSettings.watermark_cols - 1)) > pageWidth)) {

defaultSettings.watermark_cols = parseInt((pageWidth - defaultSettings.watermark_x + defaultSettings.watermark_x_space) / (defaultSettings.watermark_width + defaultSettings.watermark_x_space))

defaultSettings.watermark_x_space = parseInt((pageWidth - defaultSettings.watermark_x - defaultSettings.watermark_width * defaultSettings.watermark_cols) / (defaultSettings.watermark_cols - 1))

}

// 如果将水印行数设置为0,或水印行数设置过大,超过页面最大长度,则重新计算水印行数和水印y轴间隔

if (defaultSettings.watermark_rows === 0 || (parseInt(defaultSettings.watermark_y + defaultSettings.watermark_height * defaultSettings.watermark_rows + defaultSettings.watermark_y_space * (defaultSettings.watermark_rows - 1)) > pageHeight)) {

defaultSettings.watermark_rows = parseInt((defaultSettings.watermark_y_space + pageHeight - defaultSettings.watermark_y) / (defaultSettings.watermark_height + defaultSettings.watermark_y_space))

defaultSettings.watermark_y_space = parseInt(((pageHeight - defaultSettings.watermark_y) - defaultSettings.watermark_height * defaultSettings.watermark_rows) / (defaultSettings.watermark_rows - 1))

}

var x

var y

for (var i = 0; i < defaultSettings.watermark_rows; i++) {

y = defaultSettings.watermark_y + (defaultSettings.watermark_y_space + defaultSettings.watermark_height) * i

for (var j = 0; j < defaultSettings.watermark_cols; j++) {

x = defaultSettings.watermark_x + (defaultSettings.watermark_width + defaultSettings.watermark_x_space) * j

 

var maskDiv = document.createElement('div')

maskDiv.id = 'maskDiv' + i + j

maskDiv.className = 'maskDiv'

var txtSpan = document.createElement('span')

txtSpan.innerHTML = defaultSettings.watermark_txt

txtSpan.style.setProperty('font-size', defaultSettings.watermark_fontsize, 'important')

maskDiv.appendChild(txtSpan)

// 设置水印div倾斜显示

maskDiv.style.webkitTransform = 'rotate(-' + defaultSettings.watermark_angle + 'deg)'

maskDiv.style.MozTransform = 'rotate(-' + defaultSettings.watermark_angle + 'deg)'

maskDiv.style.msTransform = 'rotate(-' + defaultSettings.watermark_angle + 'deg)'

maskDiv.style.OTransform = 'rotate(-' + defaultSettings.watermark_angle + 'deg)'

maskDiv.style.transform = 'rotate(-' + defaultSettings.watermark_angle + 'deg)'

maskDiv.style.visibility = ''

maskDiv.style.position = 'absolute'

maskDiv.style.left = x + 'px'

maskDiv.style.top = y + 'px'

maskDiv.style.overflow = 'hidden'

maskDiv.style.zIndex = '9999'

maskDiv.style.pointerEvents = 'none'// pointer-events:none 让水印不遮挡页面的点击事件

// maskDiv.style.border="solid #eee 1px";

maskDiv.style.opacity = defaultSettings.watermark_alpha

maskDiv.style.fontFamily = defaultSettings.watermark_font

maskDiv.style.color = defaultSettings.watermark_color

maskDiv.style.textAlign = 'center'

maskDiv.style.width = defaultSettings.watermark_width + 'px'

maskDiv.style.height = defaultSettings.watermark_height + 'px'

maskDiv.style.display = 'block'

oTemp.appendChild(maskDiv)

}

}

defaultSettings.element.appendChild(oTemp)

};

 

 

 

import { watermark } from '@/watermark';

 

watermark({

watermark_txt: 'admin ' + this.dateFormatter(new Date()) +'
',

watermark_fontsize: '18px',

element: document.getElementById('app')

});

 

dateFormatter(d) {

if (d != null && d != undefined && d != "") {

if (typeof d == "object") {

let year = d.getFullYear();

let month = this.change(d.getMonth() + 1);

let day = this.change(d.getDate());

let hour = this.change(d.getHours());

let minute = this.change(d.getMinutes());

let second = this.change(d.getSeconds());

return (

year +

"-" +

month +

"-" +

day +

" " +

hour +

":" +

minute +

":" +

second

);

} else {

return d;

}

} else {

return "";

}

},

 

你可能感兴趣的:(js 添加水印)