大牛的部落格連結:https://www.cnblogs.com/daixinyu/p/6715398.html 提供給大家學習
我優化了幾點
1,我把水印的樣式單獨提出來,這樣會提高渲染水印的效能
2,他是給整個頁面新增水印,我修改一下是給需要新增水印的容器來加水印
function watermark(settings) { //預設設定 var 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_width:210,//水印寬度 watermark_height:80,//水印長度 }; //採用配置項替換預設值,作用類似jquery.extend if(arguments.length===1&&typeof arguments[0] ==="object" ){ var src=arguments[0]||{}; for(key in src){ if(src[key]&&defaultSettings[key]&&src[key]===defaultSettings[key]) continue; else if(src[key]) defaultSettings[key]=src[key]; } } // 獲取需要新增水印的容器 var box = document.getElementById("box"); // 建立水印容器 mark-box var markBox= document.createElement('div'); markBox.className="mark-box"; //獲取容器寬度 var page_width = box.offsetWidth; //獲取頁面最大高度 var page_height = box.offsetHeight; //如果將水印列數設定為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)); } 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 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)); //給水印定位 mask_div.style.left = x + 'px'; mask_div.style.top = y + 'px'; markBox.appendChild(mask_div); }; }; box.appendChild(markBox); }
下面是水印樣式
.mask_div { position: absolute; overflow: hidden; z-index: 9999; opacity: 0.2; pointer-events: none; filter: alpha(opacity=20); font-size: 18px; font-family: 微軟雅黑; color: rgb(170, 170, 170); text-align: center; width: 210px; height: 80px; display: block; -webkit-transform: rotate(-15deg); -moz-transform: rotate(-15deg); -ms-transform: rotate(-15deg); -o-transform: rotate(-15deg); transform: rotate(-15deg); }
呼叫方法watermark({options})