HTML添加水印的方法

之前有个项目做完给用户演示的时候,界面上加了水印效果,今天把具体实现的方法分享一下。

首先看下效果

核心js代码(shuiyin.js):

  1 function watermark(settings) {
  2     //默认设置
  3     var defaultSettings = {
  4         watermark_txt: "text",
  5         watermark_x: 20, //水印起始位置x轴坐标
  6         watermark_y: 20, //水印起始位置Y轴坐标
  7         watermark_rows: 20, //水印行数
  8         watermark_cols: 20, //水印列数
  9         watermark_x_space: 100, //水印x轴间隔
 10         watermark_y_space: 50, //水印y轴间隔
 11         watermark_color: '#aaa', //水印字体颜色
 12         watermark_alpha: 0.4, //水印透明度
 13         watermark_fontsize: '15px', //水印字体大小
 14         watermark_font: '微软雅黑', //水印字体
 15         watermark_width: 210, //水印宽度
 16         watermark_height: 80, //水印长度
 17         watermark_angle: 20 //水印倾斜度数
 18     };
 19     if (arguments.length === 1 && typeof arguments[0] === "object") {
 20         var src = arguments[0] || {};
 21         for (key in src) {
 22             if (src[key] && defaultSettings[key] && src[key] === defaultSettings[key]) continue;
 23             else if (src[key]) defaultSettings[key] = src[key];
 24         }
 25     }
 26     var oTemp = document.createDocumentFragment();
 27     //获取页面最大宽度
 28     var page_width = Math.max(document.body.scrollWidth, document.body.clientWidth);
 29     var cutWidth = page_width * 0.0150;
 30     var page_width = page_width - cutWidth;
 31     //获取页面最大高度
 32     var page_height = Math.max(document.body.scrollHeight, document.body.clientHeight) + 450;
 33     page_height = Math.max(page_height, window.innerHeight - 30);
 34     //如果将水印列数设置为0,或水印列数设置过大,超过页面最大宽度,则重新计算水印列数和水印x轴间隔
 35     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)) {
 36         defaultSettings.watermark_cols = parseInt((page_width - defaultSettings.watermark_x + defaultSettings.watermark_x_space) / (defaultSettings.watermark_width + defaultSettings.watermark_x_space));
 37         defaultSettings.watermark_x_space = parseInt((page_width - defaultSettings.watermark_x - defaultSettings.watermark_width * defaultSettings.watermark_cols) / (defaultSettings.watermark_cols - 1));
 38     }
 39     //如果将水印行数设置为0,或水印行数设置过大,超过页面最大长度,则重新计算水印行数和水印y轴间隔
 40     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)) {
 41         defaultSettings.watermark_rows = parseInt((defaultSettings.watermark_y_space + page_height - defaultSettings.watermark_y) / (defaultSettings.watermark_height + defaultSettings.watermark_y_space));
 42         defaultSettings.watermark_y_space = parseInt(((page_height - defaultSettings.watermark_y) - defaultSettings.watermark_height * defaultSettings.watermark_rows) / (defaultSettings.watermark_rows - 1));
 43     }
 44     var x;
 45     var y;
 46     for (var i = 0; i < defaultSettings.watermark_rows; i++) {
 47         y = defaultSettings.watermark_y + (defaultSettings.watermark_y_space + defaultSettings.watermark_height) * i;
 48         for (var j = 0; j < defaultSettings.watermark_cols; j++) {
 49             x = defaultSettings.watermark_x + (defaultSettings.watermark_width + defaultSettings.watermark_x_space) * j;
 50             var mask_div = document.createElement('div');
 51             mask_div.id = 'mask_div' + i + j;
 52             mask_div.className = 'mask_div';
 53             mask_div.appendChild(document.createTextNode(defaultSettings.watermark_txt));
 54             //设置水印div倾斜显示
 55             mask_div.style.webkitTransform = "rotate(-" + defaultSettings.watermark_angle + "deg)";
 56             mask_div.style.MozTransform = "rotate(-" + defaultSettings.watermark_angle + "deg)";
 57             mask_div.style.msTransform = "rotate(-" + defaultSettings.watermark_angle + "deg)";
 58             mask_div.style.OTransform = "rotate(-" + defaultSettings.watermark_angle + "deg)";
 59             mask_div.style.transform = "rotate(-" + defaultSettings.watermark_angle + "deg)";
 60             mask_div.style.visibility = "";
 61             mask_div.style.position = "absolute";
 62             mask_div.style.left = x + 'px';
 63             mask_div.style.top = y + 'px';
 64             mask_div.style.overflow = "hidden";
 65             mask_div.style.zIndex = "9999";
 66             //让水印不遮挡页面的点击事件
 67             mask_div.style.pointerEvents = 'none';
 68             mask_div.style.opacity = defaultSettings.watermark_alpha;
 69             mask_div.style.fontSize = defaultSettings.watermark_fontsize;
 70             mask_div.style.fontFamily = defaultSettings.watermark_font;
 71             mask_div.style.color = defaultSettings.watermark_color;
 72             mask_div.style.textAlign = "center";
 73             mask_div.style.width = defaultSettings.watermark_width + 'px';
 74             mask_div.style.height = defaultSettings.watermark_height + 'px';
 75             mask_div.style.display = "block";
 76             oTemp.appendChild(mask_div);
 77         };
 78     };
 79     document.body.appendChild(oTemp);
 80 }
 81 
 82 function getNow() {
 83     var d = new Date();
 84     var year = d.getFullYear();
 85     var month = change(d.getMonth() + 1);
 86     var day = change(d.getDate());
 87     var hour = change(d.getHours());
 88     var minute = change(d.getMinutes());
 89     var second = change(d.getSeconds());
 90 
 91     function change(t) {
 92         if (t < 10) {
 93             return "0" + t;
 94         } else {
 95             return t;
 96         }
 97     }
 98     var time = year + '' + month + '' + day + '' + hour + '' + minute + '' + second + '';
 99     return time;
100 }

调用方法

 在html中引入jquery.js,shuiyin.js,然后使用以下代码调用:

watermark({ "watermark_txt": "水印文字内容"});

本文最开始预览效果的调用方式如下(不包含html部分,html无要求):

1 var now = getNow();
2 watermark({ "watermark_txt": "样稿" + now });
posted @ 2021-08-03 14:27  薄心之心  阅读(3729)  评论(0编辑  收藏  举报