需求缘由
项目是前后端不分离开发的,很老的项目,里面很多插件只能支持ie8,业主公司浏览器都是ie8 ,这次想给所有页面添加用户水印,所以开发需要兼容ie8
水印代码
创建一个js 文件watermark.js
(function(watermark){
window.watermarkdivs = [];
var loadMark = function(settings) {
var defaultSettings={
watermark_txt:watermark.watermark_txt,
watermark_x:20,
watermark_y:20,
watermark_rows:0,
watermark_cols:0,
watermark_x_space:200,
watermark_y_space:100,
watermark_font:'微软雅黑',
watermark_color:'black',
watermark_fontsize:'28px',
watermark_alpha:0.15,
watermark_width:150,
watermark_height:100,
watermark_angle:15,
watermark_progid:'alpha(opacity=15)'
};
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];
}
}
if (window.watermarkdivs && window.watermarkdivs.length > 0) {
document.body.removeChild(document.getElementById("markid"));
window.watermarkdivs = [];
}
var page_width = Math.max(document.body.scrollWidth,document.body.clientWidth);
var page_height = Math.max(document.body.scrollHeight,document.body.clientHeight);
var oTemp = document.createDocumentFragment();
var watermarkDiv = document.getElementById("markid");
if(!watermarkDiv){
watermarkDiv =document.createElement('div');
watermarkDiv.id="markid";
watermarkDiv.style.pointerEvents = "none";
document.body.appendChild(watermarkDiv);
}
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));
}
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');
var oText=document.createTextNode(defaultSettings.watermark_txt);
mask_div.appendChild(oText);
mask_div.id = 'mask_div' + i + j;
mask_div.onselectstart="return false";
mask_div.style.filter = "progid:DXImageTransform.Microsoft.Matrix(sizingMethod='auto expand', M11=0.96593, M12=0.25882, M21=-0.25882, M22=0.96593) progid:DXImageTransform.Microsoft.Alpha(opacity=15)";
mask_div.style.webkitTransform = "rotate(-" + defaultSettings.watermark_angle + "deg)";
mask_div.style['-moz-transform'] = "rotate(-" + defaultSettings.watermark_angle + "deg)";
mask_div.style['-ms-transform'] = "rotate(-" + defaultSettings.watermark_angle + "deg)";
mask_div.style['-ot-transform'] = "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.opacity = defaultSettings.watermark_alpha;
mask_div.style['-moz-opacity']=defaultSettings.watermark_alpha;
mask_div.style['-khtml-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";
watermarkDiv.appendChild(mask_div);
watermarkDiv.style.cursor = "default";
window.watermarkdivs.push(watermarkDiv);
};
};
document.body.appendChild(oTemp);
};
watermark.init = function(settings) {
window.onload = function() {
loadMark(settings);
};
window.onresize = function() {
loadMark(settings);
};
};
watermark.load = function(settings){
loadMark(settings);
};
})(window.watermark = {} );
页面使用
<script language="javascript" src="XX/XX/watermark.js"></script>
<script type="text/javascript">
$(function(){
watermark.load(watermark_txt:"12222")
});
</script>
本地运行如图
本地运行的时候页面是没有问题的,部署测试或者生产环境时,在ie8 浏览器打开时,水印没有透明度和倾斜的
解决方式
- 打开设置选中Internet设置,如图
- 选择安全 点击受信任的站点 — 点击站点 — 添加地址 如图
这样操作后ie8的水印就生效了
|