通过js给网页加上水印背景实例
程序员文章站
2024-02-08 08:31:29
水印方法
export function watermark(settings) {
//默认设置
let defaultsettings={
wate...
水印方法
export function watermark(settings) { //默认设置 let defaultsettings={ watermark_txt:"text", watermark_x:20,//水印起始位置x轴坐标 watermark_y:20,//水印起始位置y轴坐标 watermark_rows:20,//水印行数 watermark_cols:20,//水印列数 watermark_x_space:100,//水印x轴间隔 watermark_y_space:50,//水印y轴间隔 watermark_color:'#aaa',//水印字体颜色 watermark_alpha:0.4,//水印透明度 watermark_fontsize:'15px',//水印字体大小 watermark_font:'微软雅黑',//水印字体 watermark_width:210,//水印宽度 watermark_height:80,//水印长度 watermark_angle:15//水印倾斜度数 }; object.assign(defaultsettings, settings); let otemp = document.createdocumentfragment(); //获取页面最大宽度 let p_width = math.max(document.body.scrollwidth,document.body.clientwidth); let cutwidth = p_width*0.0150; let page_width=p_width-cutwidth; //获取页面最大高度 let page_height = math.max(document.body.scrollheight,document.body.clientheight)+450; // let page_height = 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)) > page_width)) { defaultsettings.watermark_cols = parseint((page_width-defaultsettings.watermark_x+defaultsettings.watermark_x_space) / (defaultsettings.watermark_width + defaultsettings.watermark_x_space)); defaultsettings.watermark_x_space = parseint((page_width - 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)) > page_height)) { defaultsettings.watermark_rows = parseint((defaultsettings.watermark_y_space + page_height - defaultsettings.watermark_y) / (defaultsettings.watermark_height + defaultsettings.watermark_y_space)); defaultsettings.watermark_y_space = parseint(((page_height - defaultsettings.watermark_y) - defaultsettings.watermark_height * defaultsettings.watermark_rows) / (defaultsettings.watermark_rows - 1)); } let x; let y; for (let i = 0; i < defaultsettings.watermark_rows; i++) { y = defaultsettings.watermark_y + (defaultsettings.watermark_y_space + defaultsettings.watermark_height) * i; for (let j = 0; j < defaultsettings.watermark_cols; j++) { x = defaultsettings.watermark_x + (defaultsettings.watermark_width + defaultsettings.watermark_x_space) * j; let mask_div = document.createelement('div'); mask_div.id = 'mask_div' + i + j; mask_div.classname = 'mask_div'; mask_div.appendchild(document.createtextnode(defaultsettings.watermark_txt)); //设置水印div倾斜显示 mask_div.style.webkittransform = "rotate(-" + defaultsettings.watermark_angle + "deg)"; mask_div.style.moztransform = "rotate(-" + defaultsettings.watermark_angle + "deg)"; mask_div.style.mstransform = "rotate(-" + defaultsettings.watermark_angle + "deg)"; mask_div.style.otransform = "rotate(-" + defaultsettings.watermark_angle + "deg)"; mask_div.style.transform = "rotate(-" + defaultsettings.watermark_angle + "deg)"; mask_div.style.visibility = ""; mask_div.style.position = "absolute"; mask_div.style.left = x + 'px'; mask_div.style.top = y + 'px'; mask_div.style.overflow = "hidden"; mask_div.style.zindex = "9999"; mask_div.style.pointerevents='none';//pointer-events:none 让水印不遮挡页面的点击事件 mask_div.style.opacity = defaultsettings.watermark_alpha; mask_div.style.fontsize = defaultsettings.watermark_fontsize; mask_div.style.fontfamily = defaultsettings.watermark_font; mask_div.style.color = defaultsettings.watermark_color; mask_div.style.textalign = "center"; mask_div.style.width = defaultsettings.watermark_width + 'px'; mask_div.style.height = defaultsettings.watermark_height + 'px'; mask_div.style.display = "block"; otemp.appendchild(mask_div); }; }; document.body.appendchild(otemp); }
使用方法
import { watermark } from './../common/watermark'; watermark({watermark_txt: '你要添加的水印'});
效果演示
源码地址
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。