js vue 页面添加水印
vue 微信页面添加水印
this.$nexttick(function() {
watermark({ watermark_txt: "<div>" + '123' + "</div><div>" + '456' + "</div>"});
})
function watermark(settings) {
//默认设置
var defaultsettings={
watermark_txt:"text",
watermark_x:70,//水印起始位置x轴坐标
watermark_y:10,//水印起始位置y轴坐标
watermark_rows:100,//水印行数
watermark_cols:2,//水印列数
watermark_x_space:60,//水印x轴间隔
watermark_y_space:40,//水印y轴间隔
watermark_color:'#aaa',//水印字体颜色
watermark_alpha:0.3,//水印透明度
watermark_fontsize:'.26rem',//水印字体大小
watermark_width:60,//水印宽度
watermark_height:60,//水印高度
watermark_angle:10//水印倾斜度数
};
//采用配置项替换默认值,作用类似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 otemp = document.createdocumentfragment();
//获取页面最大宽度
var page_width = math.max(document.body.scrollwidth,document.body.clientwidth) - 30;
//获取页面最大高度
// var page_height = math.max(document.body.scrollheight,document.body.clientheight);
var page_height = math.max($("#icontainer").get(0).clientheight,document.body.clientheight,$("#icontainer").get(0).scrollheight) - 20;
// var page_height = $("#icontainer").get(0).scrollheight;
// console.log(page_height,$("#icontainer").outerheight());
//如果将水印列数设置为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.innerhtml = 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 = "9999999";
mask_div.style.pointerevents = "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.lineheight = '.32rem';
mask_div.style.display = "inline-block";
otemp.appendchild(mask_div);
};
};
console.log($('.mask_div:odd'));
document.body.appendchild(otemp);
$('.mask_div:odd').css({left:defaultsettings.watermark_x + 'px'});
$('.mask_div:even').css({right:defaultsettings.watermark_x + 'px'});
}