Jquery 弹出层插件实现代码

2020-05-23 06:14:12易采站长站整理

base.init = function() {
base.options = $.extend({}, $.layer.defaults, options);
base.$layerBox = $(base.options.template);
base.posX = base.posY = 0;
base.moveing = false;
//加载样式
if (!$.layer.hasCss && base.options.cssurl != “”) {
$(“head”, base.options.target).append(“<link href=”” + base.options.cssurl + “” rel=”stylesheet” type=”text/css” />”);
$.layer.hasCss = true;
}
//加载层对象
$.layer.maskLayerIndex += 20; //调高层位置
base.$layerBox.appendTo(“body”, base.options.target).css({ “z-index”: $.layer.maskLayerIndex }).hide();
base.$layerBox.find(“div[class=’layer-title’]”).append(base.options.title);
//判断内容是字符串还是Jquery对象
if (typeof (base.options.content) == “object”) {
base.$tempContentParent = base.options.content.parent();
base.tagHide = base.options.content.is(“:hidden”);
base.$tempContent = base.options.content.clone();
base.options.content.appendTo(base.$layerBox.find(“div[class=’layer-content’]”)).show();
}
else {
base.$layerBox.find(“div[class=’layer-content’]”).append(base.options.content);
}
//绑定关闭按钮事件
base.$layerBox.find(“span[class=’layer-close’]”).mousedown(function(event) {
//阻止事件冒泡
if (event && event.stopPropagation) {
event.stopPropagation();
} else {
window.event.cancelBubble = true;
}
base.close();
return false;
});
base.$layerBox.find(“div[class=’layer-bar’]”).mousedown(function(event) {
base.moveStart(event);
});
//base.$layerBox.show();

}
//打开层
base.open = function() {
//层显示动画
//alert($(“html”, base.options.target).height());
var mtop = ($(“html”, base.options.target).height() – base.options.height) / 2 + $(document, base.options.target).scrollTop(); //
var mleft = $(“html”, base.options.target).width() / 2 – base.options.width / 2; //
base.$layerBox.css({ top: base.$el.offset().top, left: base.$el.offset().left, width: 0, height: 0 }).animate({
left: mleft,
top: mtop,
width: base.options.width,
height: base.options.height,
opacity: “show”
}, “slow”, function() {
//加载遮罩层
if (base.options.masklayer.show) {
$.layer.maskLayerIndex -= 10; //降低层位置
base.$masklayer = $(“<div style=”display:none; z-index:” + $.layer.maskLayerIndex + “; filter: alpha(opacity:” + base.options.masklayer.alpha + “); left: 0px; width: 100%; position: absolute; top: 0px; height: ” + $(document, base.options.target).height() + “px; background-color:” + base.options.masklayer.bgcolor + “”></div>”);