-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpop.js
More file actions
140 lines (129 loc) · 4.33 KB
/
Copy pathpop.js
File metadata and controls
140 lines (129 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*!
* Copyright 2014, 2014 ywang1724 and other contributors
* Released under the MIT license
* http://ywang1724.com
*
* Date: 2014-09-02
*/
/**
* 弹出层类 PopLayer
* 参数说明:args obj
* @param title 弹出层标题
* @param content 弹出层内容html
* @param isModal 弹出层是否模态
* @param moveable 弹出层可否移动
* @param document 上下文文档对象
*/
(function() {
function PopLayer(args) {
//初始化参数
this.title = args.title || "";
this.content = args.content || "";
this.isModal = (typeof args.isModal === "boolean") ? args.isModal : true;
this.moveable = (typeof args.moveable === "boolean") ? args.moveable : true;
this.document = args.document || document;
//辅助参数
this.isDown = false; //鼠标是否在弹层标题栏按下
this.offset = {
"width": 0,
"height": 0
};
this.id = ++top.PopLayer.id;
//模态加遮罩层
var modal = this.getElement();
if (this.isModal) {
this.myModal = modal.myModal;
}
this.myPop = modal.myPop;
top.PopLayer.instances[this.id] = this;
//初始化
this.init();
};
PopLayer.prototype = {
init: function() {
this.initContent();//初始化内容
this.initEvent();//初始化行为
},
initContent: function() {
if (this.isModal) {
$("body", this.document).append(this.myModal);
this.myModal.show();
}
$("body", this.document).append(this.myPop);
$(".myPop-title-value", this.myPop).html(this.title);//设置标题
this.myPop.css("top", (this.document.documentElement.clientHeight - this.myPop.height()) / 2 + "px");
this.myPop.css("left", (this.document.documentElement.clientWidth - this.myPop.width()) / 2 + "px");
this.myPop.show();
},
initEvent: function() {
var $this = this;
//鼠标按下事件
$(".myPop-title", this.myPop).on("mousedown", function(e) {
$this.isDown = true;
var event = window.event || e;
//记录按下时鼠标距离弹出层位置
$this.offset.height = event.clientY - $this.myPop.offset().top;
$this.offset.width = event.clientX - $this.myPop.offset().left;
return false;
});
//鼠标拖动事件
$(this.document).mousemove(function(e) {
if ($this.isDown && $this.moveable) {
var event = window.event || e;
//偏移位置
var top = event.clientY - $this.offset.height,
left = event.clientX - $this.offset.width,
maxL = $this.document.documentElement.clientWidth - $this.myPop.width(),
maxT = $this.document.documentElement.clientHeight - $this.myPop.height();
left = left < 0 ? 0 : left;
left = left > maxL ? maxL : left;
top = top < 0 ? 0 : top;
top = top > maxT ? maxT : top;
$this.myPop.css("top", top + "px");
$this.myPop.css("left", left + "px");
}
return false;
}).mouseup(function(e) {
if ($this.isDown) {
$this.isDown = false;
}
return false;
});
//关闭事件
$(".myPop-close", this.myPop).on('click', function() {
$this.destroy();
return false;
});
},
getElement: function() {
return {
"myModal": $("<div class='myModal'></div>", this.document),
"myPop": $("<div class='myPop'>" +
"<h2 class='myPop-title'>" +
"<span class='myPop-title-value'></span>" +
"<span class='myPop-close'>×</span>" +
"</h2>" +
"<div class='myPop-content'>" + this.content + "</div>" +
"</div>", this.document)
};
},
destroy: function() {
//清除显示层
this.myPop.remove();
//清除存在的遮罩层
if(this.isModal){
this.myModal.remove();
}
//销毁池中对象
delete top.PopLayer.instances[this.id];
//计数器退栈
top.PopLayer.id--;
}
};
if (!top.PopLayer) {
PopLayer.zIndexCounter = 1000;//z-index计数器
PopLayer.id = 0;//层对象计数
PopLayer.instances = {};//层对象池
top.PopLayer = PopLayer;
}
})();