创建遮罩层

     _createCover: function() {
var newMask = document.createElement("div");
newMask.id = this._mark;
newMask.style.position = "absolute";
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 = 'none';
document.body.appendChild(newMask);
this._cover = newMask;
}

新建弹出层

     _createFloater: function(html) {
var newDiv = document.createElement("div");
newDiv.id = this._id;
newDiv.style.position = "absolute";
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 = 'none';
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(window, "scroll", this._fS); // 隐藏后需
removeEventHandler(window, "scroll", this._fS);

完整代码

 var Floater = (function(){
var me = Class.create();
me.prototype = {
initialize: function(options) {
this._fS = BindAsEventListener(this, this.addjustPosition);
this.setOptions(options);
},
setOptions: function(options) {
this.options = options || {};
this._id = options.id;
this._mark = 'mark';
},
show: function(html,options) {
options = options || {};
if(!this._cover){
this._createCover();
}
if(!this._floater){
this._createFloater(html);
}
if(options.saveOpt){
this._saveOption = options.saveOpt;
this.bindSaveEvent();
}
this._bindScrollEvent();
this.addjustPosition();
this._floater.style.display = '';
this._cover.style.display = '';
this.isShow = true;
},
insert: function(html,opts,att){
var _e = document.createElement("div"), _t;
_e.innerHTML = html;
for(var k in opts){
_e[k] = opts[k];
}
_t = this._floater.querySelector('['+att+']');
if(_t){
_t.appendChild(_e);
}
},
getFloater: function(){
if(this._floater){
return this._floater;
}
},
//遮罩层
_createCover: function() {
var newMask = document.createElement("div");
newMask.id = this._mark;
newMask.style.position = "absolute";
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 = 'none';
document.body.appendChild(newMask);
this._cover = newMask;
},
//新弹出层
_createFloater: function(html) {
var newDiv = document.createElement("div");
newDiv.id = this._id;
newDiv.style.position = "absolute";
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 = 'none';
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+']');
if(this._saveElem){
addEventHandler(this._saveElem, "click", this._saveOption.handler);
}
},
_bindScrollEvent: function() {
addEventHandler(window, "scroll", this._fS);
},
hide: function() {
this.isShow = false;
this.destory();
},
destory: function() {
removeEventHandler(window, "scroll", this._fS);
if(this._saveElem){
removeEventHandler(this._saveElem, "click", this._saveOption.handler);
}
if (this._cover){
document.body.removeChild(this._cover);
}
if (this._floater){
document.body.removeChild(this._floater);
}
this._cover = null;
this._floater = null;
}
};
return me;
})();

(三)原生JS实现 - 插件 - 弹出层的更多相关文章

  1. js插件---弹出层sweetalert2(总结)

    js插件---弹出层sweetalert2(总结) 一.总结 一句话总结: sweetalert2的效果非常好,效果比较Q萌,移动端适配也比较好,感觉比layer.js效果好点 1.SweetAler ...

  2. 原生Js封装的弹出框-弹出窗口-页面居中-多状态可选

    原生Js封装的弹出框-弹出窗口-页面居中-多状态可选   实现了一下功能: 1.title可自定义 可拖拽 2.width height可以自定义 3.背景遮罩和透明度可以自定义 4.可以自己编辑弹出 ...

  3. js进阶 11-20 弹出层如何制作

    js进阶 11-20 弹出层如何制作 一.总结 一句话总结:其实就是一个div,控制显示和隐藏即可.设置成绝对定位更好,就可以控制弹出层出现的位置.关闭的画质需要将display重新设置为none就好 ...

  4. 在vue中继续使用layer.js来做弹出层---切图网

    layer.js是一个方便的弹出层插件,切图网专注于PSD2HTML等前端切图多年,后转向Vue开发.在vue开发过程中引入layer.js的时候遇到了麻烦.原因是layer.js不支持import导 ...

  5. JS来添加弹出层,并且完成锁屏

    上图 <html> <head> <title>弹出层</title> <style type="text/css"> ...

  6. js漂亮的弹出层

    1.漂亮的弹出层----artDialog http://aui.github.io/artDialog/ 2.弹出层 ------layer http://sentsin.com/jquery/la ...

  7. DIV JS CSS 轻量级弹出层 兼容各浏览器

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. jQuery第三课 点击按钮 弹出层div效果

    jQuery 事件方法 事件方法会触发匹配元素的事件,或将函数绑定到所有匹配元素的某个事件. 触发实例: $("button#demo").click() 上面的例子将触发 id= ...

  9. 【jq】插件—弹出层layer.js

    layer.js包含了所有的层级情形,并且附加的有:tab层,相册层.webIM层. 适用于移动版本的layer.js   为layer for mobile 配套的layui 非常适合用于后台系统的 ...

随机推荐

  1. Linux 编译安装httpsqs

    wget http://httpsqs.googlecode.com/files/libevent-2.0.12-stable.tar.gz tar zxvf libevent-2.0.12-stab ...

  2. LBS配置

    js: <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak= ...

  3. Java学习笔记--注解

    注解的使用与实例:http://www.cnblogs.com/pepcod/archive/2013/02/16/2913474.html 注解的作用及使用方法:http://wenku.baidu ...

  4. 在类库中使用log4net

    最近在做一个类库,用的C#写的,为了DEBUG方便需要日志输出,于是找了log4net这个工具进行日志输出; 因为调用这个类库的是C++,而且本人对C++不是很熟悉,于是无法在app.config或者 ...

  5. Rufus-Create bootable USB drives the easy way

    Rufus Create bootable USB drives the easy way Rufus is a utility that helps format and create bootab ...

  6. 郁闷~win7无法进行局域网访问解决

    win7无法进行局域网访问解决 公司里经常会使用网络共享的文件服务器,但是用win7的那伙计,在输入帐号和密码后却提示密码错误,试验了多次都是如此. 经过网上搜索文章及自己的研究最后发现这是由于win ...

  7. 11G在线重建索引

    SQL> select count(*) from test_idx; COUNT(*) ---------- 19087751 SQL> select segment_name,segm ...

  8. 使IE6同样支持圆角效果

    之前写到过,IE6不支持:hover效果的解决办法,其它这个跟它一样.IE6(7/8)不支持border-radius属性,所以其中的圆角效果显示不出来,可以通过引用ie-css3.htc的方法解决. ...

  9. php_mysql、php_mysqli 与 pdo_mysql 的区别与选择

    php与mysql的连接有三种API接口,分别是:PHP的MySQL扩展 .PHP的mysqli扩展 .PHP数据对象(PDO) ,下面针对以上三种连接方式做下总结,以备在不同场景下选出最优方案.   ...

  10. MySQL具体解释(7)-----------MySQL线程池总结(一)

    线程池是Mysql5.6的一个核心功能.对于server应用而言,不管是web应用服务还是DB服务,高并发请求始终是一个绕不开的话题.当有大量请求并发訪问时,一定伴随着资源的不断创建和释放.导致资源利 ...