创建遮罩层
_createCover: function() {
var newMask = document.createElement("div");
newMask.id = this._mark;
newMask.style.position = "mutlak";
newMask.style.zIndex = "100";
_scrollWidth = Math.max(document.body.scrollWidth,document.documentElement.scrollWidth);
_scrollHeight = Math.max(document.body.scrollHeight,document.documentElement.scrollHeight);
newMask.style.width = _scrollWidth "px";
newMask.style.height = _scrollHeight "px";
newMask.style.top = "0px";
newMask.style.left = "0px";
newMask.style.background = "#000";
newMask.style.filter = "alpha(opacity=50)";
newMask.style.opacity = "0.50";
newMask.style.display = 'tiada';
document.body.appendChild(newMask);
ini._cover = newMask;
}
新建弹出层
_createFloater: function(html) {
var newDiv = document.createElement("div");
newDiv.id = this._id;
newDiv.style.position = "mutlak";
newDiv.style.zIndex = "9999";
newDivWidth = 400;
newDivHeight = 200;
newDiv.style.width = newDivWidth "px";
newDiv.style.height = newDivHeight "px";
newDiv.style.top = (document.body.scrollTop document.body.clientHeight/2 - newDivHeight/2) "px";
newDiv.style.left = (document.body.scrollLeft document.body.clientWidth/2 - newDivWidth/2) "px";
newDiv.style.padding = "5px";
newDiv.style.display = 'tiada';
newDiv.innerHTML = html;
document.body.appendChild(newDiv);
this._floater = newDiv;
}
调节弹层位置
addjustPosition: function() {
this._floater.style.top = (document.body.scrollTop document.body.clientHeight/2 - newDivHeight/2) "px";
this._floater.style.left = (document.body.scrollLeft document.body.clientWidth/2 - newDivWidth/2) "px";
}
屏幕滚动事件时调整位置
this._fS = BindAsEventListener(this, this.addjustPosition);
addEventHandler(tetingkap, "scroll", this._fS);
// 隐藏后需
removeEventHandler(tetingkap, "tatal", ini._fS);
完整代码
var Floater = (fungsi(){
var me = Class.create();
me.prototaip = {
mulakan: fungsi(pilihan) {
this._fS = BindAsEventListener(this, this.addjustPosition);
this.setOptions(options);
},
setOptions: function(options) {
this.options = pilihan || {};
this._id = options.id;
ini._mark = 'tanda';
},
tunjukkan: fungsi(html, pilihan) {
pilihan = pilihan || {};
jika(!ini._cover){
this._createCover();
}
jika(!ini._floater){
this._createFloater(html);
}
if(options.saveOpt){
this._saveOption = options.saveOpt;
this.bindSaveEvent();
}
this._bindScrollEvent();
this.adjustPosition();
this._floater.style.display = '';
this._cover.style.display = '';
this.isShow = benar;
},
sisipkan: function(html,opts,att){
var _e = document.createElement("div"), _t;
_e.innerHTML = html;
untuk(var k dalam opts){
_e[k] = opts[k];
}
_t = this._floater.querySelector('[' att ']');
jika(_t){
_t.appendChild(_e);
}
},
getFloater: function(){
jika(ini._floater){
pulangkan ini._floater;
}
},
//遮罩层
_createCover: function() {
var newMask = document.createElement("div");
newMask.id = this._mark;
newMask.style.position = "mutlak";
newMask.style.zIndex = "100";
_scrollWidth = Math.max(document.body.scrollWidth,document.documentElement.scrollWidth);
_scrollHeight = Math.max(document.body.scrollHeight,document.documentElement.scrollHeight);
newMask.style.width = _scrollWidth "px";
newMask.style.height = _scrollHeight "px";
newMask.style.top = "0px";
newMask.style.left = "0px";
newMask.style.background = "#000";
newMask.style.filter = "alpha(opacity=50)";
newMask.style.opacity = "0.50";
newMask.style.display = 'tiada';
document.body.appendChild(newMask);
ini._cover = newMask;
},
//新弹出层
_createFloater: function(html) {
var newDiv = document.createElement("div");
newDiv.id = this._id;
newDiv.style.position = "mutlak";
newDiv.style.zIndex = "9999";
newDivWidth = 400;
newDivHeight = 200;
newDiv.style.width = newDivWidth "px";
newDiv.style.height = newDivHeight "px";
newDiv.style.top = (document.body.scrollTop document.body.clientHeight/2 - newDivHeight/2) "px";
newDiv.style.left = (document.body.scrollLeft document.body.clientWidth/2 - newDivWidth/2) "px";
newDiv.style.padding = "5px";
newDiv.style.display = 'tiada';
newDiv.innerHTML = html;
document.body.appendChild(newDiv);
this._floater = newDiv;
},
//弹出层滚动居中
addjustPosition: function() {
this._floater.style.top = (document.body.scrollTop document.body.clientHeight/2 - newDivHeight/2) "px";
this._floater.style.left = (document.body.scrollLeft document.body.clientWidth/2 - newDivWidth/2) "px";
},
bindSaveEvent: function() {
this._saveElem = this._floater.querySelector('[' this._saveOption.elem ']');
jika(ini._saveElem){
addEventHandler(this._saveElem, "click", this._saveOption.handler);
}
},
_bindScrollEvent: function() {
addEventHandler(tetingkap, "scroll", this._fS);
},
sembunyikan: function() {
this.isShow = palsu;
this.destory();
},
destory: function() {
removeEventHandler(tetingkap, "tatal", ini._fS);
jika(ini._saveElem){
removeEventHandler(this._saveElem, "click", this._saveOption.handler);
}
jika (ini._cover){
document.body.removeChild(this._cover);
}
jika (ini._floater){
document.body.removeChild(this._floater);
}
ini._cover = null;
this._floater = null;
}
};
pulangkan saya;
})();