watermark.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. function watermark(settings) {
  2. //默认设置
  3. var defaultSettings={
  4. watermarl_element:"body",
  5. watermark_txt:"",
  6. watermark_x:20,//水印起始位置x轴坐标
  7. watermark_y:20,//水印起始位置Y轴坐标
  8. watermark_rows:2000,//水印行数
  9. watermark_cols:2000,//水印列数
  10. watermark_x_space:70,//水印x轴间隔
  11. watermark_y_space:30,//水印y轴间隔
  12. watermark_color:'#aaa',//水印字体颜色
  13. watermark_alpha:0.4,//水印透明度
  14. watermark_fontsize:'15px',//水印字体大小
  15. watermark_font:'微软雅黑',//水印字体
  16. watermark_width:300,//水印宽度
  17. watermark_height:200,//水印长度
  18. watermark_angle:0,//水印倾斜度数
  19. watermark_num:0
  20. };
  21. //采用配置项替换默认值,作用类似jquery.extend
  22. if(arguments.length===1&&typeof arguments[0] ==="object" )
  23. {
  24. var src=arguments[0]||{};
  25. for(key in src)
  26. {
  27. if(src[key]&&defaultSettings[key]&&src[key]===defaultSettings[key])
  28. continue;
  29. else if(src[key])
  30. defaultSettings[key]=src[key];
  31. }
  32. }
  33. var oTemp = document.createDocumentFragment();
  34. var maskElement = document.getElementById(defaultSettings.watermarl_element) || document.body;
  35. //获取页面最大宽度
  36. //var page_width = Math.max(maskElement.scrollWidth,maskElement.clientWidth);
  37. var page_width = $(window).width();
  38. //获取页面最大高度
  39. //var page_height = Math.max(maskElement.scrollHeight, maskElement.clientHeight);
  40. var page_height = $(window).height();
  41. //角度
  42. var watermark_angle = 360 * Math.atan(page_height / page_width) / (2 * Math.PI)
  43. //水印数量自适应元素区域尺寸
  44. defaultSettings.watermark_cols=Math.ceil(page_width/(defaultSettings.watermark_x_space+defaultSettings.watermark_width));
  45. defaultSettings.watermark_rows = Math.ceil(page_height / (defaultSettings.watermark_y_space + defaultSettings.watermark_height));
  46. var x;
  47. var y;
  48. if (defaultSettings.watermark_num == 0) {
  49. for (var i = 0; i < defaultSettings.watermark_cols - 2; i++) {
  50. y = defaultSettings.watermark_y + (defaultSettings.watermark_y_space + defaultSettings.watermark_height) * i;
  51. for (var j = 0; j < defaultSettings.watermark_cols - 1; j++) {
  52. x = defaultSettings.watermark_x + (defaultSettings.watermark_width + defaultSettings.watermark_x_space) * j;
  53. var mask_div = document.createElement('div');
  54. mask_div.id = 'mask_div' + i + j;
  55. mask_div.className = 'mask_div';
  56. //mask_div.appendChild(document.createTextNode(defaultSettings.watermark_txt));
  57. mask_div.innerHTML = (defaultSettings.watermark_txt);
  58. //设置水印div倾斜显示
  59. mask_div.style.webkitTransform = "rotate(-" + watermark_angle + "deg)";
  60. mask_div.style.MozTransform = "rotate(-" + watermark_angle + "deg)";
  61. mask_div.style.msTransform = "rotate(-" + watermark_angle + "deg)";
  62. mask_div.style.OTransform = "rotate(-" + watermark_angle + "deg)";
  63. mask_div.style.transform = "rotate(-" + watermark_angle + "deg)";
  64. mask_div.style.visibility = "";
  65. mask_div.style.position = "absolute";
  66. mask_div.style.left = (x + 50) + 'px';
  67. mask_div.style.top = (y + 50) + 'px';
  68. mask_div.style.overflow = "hidden";
  69. mask_div.style.zIndex = "9999";
  70. mask_div.style.pointerEvents = 'none';//pointer-events:none 让水印不遮挡页面的点击事件
  71. //mask_div.style.border="solid #eee 1px";          //兼容IE9以下的透明度设置 mask_div.style.filter="alpha(opacity=50)";
  72. mask_div.style.opacity = defaultSettings.watermark_alpha;
  73. mask_div.style.fontSize = defaultSettings.watermark_fontsize;
  74. mask_div.style.fontFamily = defaultSettings.watermark_font;
  75. mask_div.style.color = defaultSettings.watermark_color;
  76. mask_div.style.textAlign = "center";
  77. mask_div.style.width = defaultSettings.watermark_width + 'px';
  78. mask_div.style.height = defaultSettings.watermark_height + 'px';
  79. mask_div.style.paddingTop = "10px";
  80. mask_div.style.display = "block";
  81. mask_div.style["word-break"] = "break-all";
  82. mask_div.style["word-wrap"] = "break-word";
  83. oTemp.appendChild(mask_div);
  84. };
  85. }
  86. } else {
  87. var xi = 0, yi = -1, xt = Math.round(window.innerWidth / (defaultSettings.watermark_x + (defaultSettings.watermark_width + defaultSettings.watermark_x_space)))
  88. for (var i = 0; i < defaultSettings.watermark_num; i++) {
  89. xi = i % xt;
  90. if (xi == 0) {
  91. yi = yi + 1;
  92. }
  93. x = defaultSettings.watermark_x + (defaultSettings.watermark_width + defaultSettings.watermark_x_space) * xi;
  94. y = defaultSettings.watermark_y + (defaultSettings.watermark_y_space + defaultSettings.watermark_height) * yi;
  95. var mask_div = document.createElement('div');
  96. mask_div.id = 'mask_div' + i;
  97. mask_div.className = 'mask_div';
  98. //mask_div.appendChild(document.createTextNode(defaultSettings.watermark_txt));
  99. mask_div.innerHTML = (defaultSettings.watermark_txt);
  100. //设置水印div倾斜显示
  101. mask_div.style.webkitTransform = "rotate(-" + watermark_angle + "deg)";
  102. mask_div.style.MozTransform = "rotate(-" + watermark_angle + "deg)";
  103. mask_div.style.msTransform = "rotate(-" + watermark_angle + "deg)";
  104. mask_div.style.OTransform = "rotate(-" + watermark_angle + "deg)";
  105. mask_div.style.transform = "rotate(-" + watermark_angle + "deg)";
  106. mask_div.style.visibility = "";
  107. mask_div.style.position = "absolute";
  108. mask_div.style.left = (x + 50) + 'px';
  109. mask_div.style.top = (y + 50) + 'px';
  110. mask_div.style.overflow = "hidden";
  111. mask_div.style.zIndex = "9999";
  112. mask_div.style.pointerEvents = 'none';//pointer-events:none 让水印不遮挡页面的点击事件
  113. //mask_div.style.border="solid #eee 1px";          //兼容IE9以下的透明度设置 mask_div.style.filter="alpha(opacity=50)";
  114. mask_div.style.opacity = defaultSettings.watermark_alpha;
  115. mask_div.style.fontSize = defaultSettings.watermark_fontsize;
  116. mask_div.style.fontFamily = defaultSettings.watermark_font;
  117. mask_div.style.color = defaultSettings.watermark_color;
  118. mask_div.style.textAlign = "center";
  119. mask_div.style.width = defaultSettings.watermark_width + 'px';
  120. mask_div.style.height = defaultSettings.watermark_height + 'px';
  121. mask_div.style.paddingTop = "10px";
  122. mask_div.style.display = "block";
  123. mask_div.style["word-break"] = "break-all";
  124. mask_div.style["word-wrap"] = "break-word";
  125. oTemp.appendChild(mask_div);
  126. }
  127. }
  128. maskElement.appendChild(oTemp);
  129. }
  130. $.ajax({
  131. type: "post",
  132. url: apiurljs.login + "g2app/dataabase/queryDataByCol",
  133. contentType: "application/json",
  134. headers: {
  135. Authorization: "Bearer " + localStorage.getItem("GlWorkPlatform-AccessToken"),
  136. },
  137. dataType: "json",
  138. data: JSON.stringify({
  139. data: s4.encryptData_CBC(JSON.stringify({
  140. tablename: "SHUIYIN",
  141. colums: "*",
  142. order: "ID",
  143. sqlwhere: { SORTID: 1 },
  144. sqlinwhere: "",
  145. sqllikewhere: ""
  146. }))
  147. }),
  148. success: (function (obj) {
  149. var res = strToJson(s4.decryptData_CBC(obj.data));
  150. console.log(res)
  151. if (res.data.length != 0) {
  152. if (res.data[0].STATUS == "true") {
  153. watermark({
  154. "watermarl_element": "app",
  155. "watermark_txt": s4.encryptData_CBC(res.data[0].CNAME + ($.cookie("GlWorkPlatform-chineseName") != null ? " " + $.cookie("GlWorkPlatform-chineseName") + " " : " ") + sp.myIP()),
  156. "watermark_width": 300,
  157. "watermark_height": 200,
  158. "watermark_num": parseInt(res.data[0].SHULIANG)
  159. })
  160. }
  161. $("head").append("<style> html,body{overflow:hidden !important;}</style>")
  162. }
  163. })
  164. })