(三)原生JS实现 - 插件 - 弹出层

创建遮罩层

 1     _createCover: function() {
 2         var newMask = document.createElement("div");
 3         newMask.id = this._mark;
 4         newMask.style.position = "absolute";
 5         newMask.style.zIndex = "100";
 6         _scrollWidth = Math.max(document.body.scrollWidth,document.documentElement.scrollWidth);
 7         _scrollHeight = Math.max(document.body.scrollHeight,document.documentElement.scrollHeight);
 8         newMask.style.width = _scrollWidth + "px";
 9         newMask.style.height = _scrollHeight + "px";
10         newMask.style.top = "0px";
11         newMask.style.left = "0px";
12         newMask.style.background = "#000";
13         newMask.style.filter = "alpha(opacity=50)";
14         newMask.style.opacity = "0.50";
15         newMask.style.display = ‘none‘;
16         document.body.appendChild(newMask);
17         this._cover = newMask;
18     }

新建弹出层

 1     _createFloater: function(html) {
 2         var newDiv = document.createElement("div");
 3         newDiv.id = this._id;
 4         newDiv.style.position = "absolute";
 5         newDiv.style.zIndex = "9999";
 6         newDivWidth = 400;
 7         newDivHeight = 200;
 8         newDiv.style.width = newDivWidth + "px";
 9         newDiv.style.height = newDivHeight + "px";
10         newDiv.style.top = (document.body.scrollTop + document.body.clientHeight/2 - newDivHeight/2) + "px";
11         newDiv.style.left = (document.body.scrollLeft + document.body.clientWidth/2 - newDivWidth/2) + "px";
12         newDiv.style.padding = "5px";
13         newDiv.style.display = ‘none‘;
14         newDiv.innerHTML = html;
15         document.body.appendChild(newDiv);
16         this._floater = newDiv;
17     }

调节弹层位置

1     addjustPosition: function() {
2         this._floater.style.top = (document.body.scrollTop + document.body.clientHeight/2 - newDivHeight/2) + "px";
3         this._floater.style.left = (document.body.scrollLeft + document.body.clientWidth/2 - newDivWidth/2) + "px";
4     }

屏幕滚动事件时调整位置

this._fS = BindAsEventListener(this, this.addjustPosition);
addEventHandler(window, "scroll", this._fS);

// 隐藏后需
removeEventHandler(window, "scroll", this._fS);

完整代码

  1 var Floater = (function(){
  2 var me = Class.create();
  3 me.prototype = {
  4     initialize: function(options) {
  5         this._fS = BindAsEventListener(this, this.addjustPosition);
  6         this.setOptions(options);
  7     },
  8     setOptions: function(options) {
  9         this.options = options || {};
 10         this._id = options.id;
 11         this._mark = ‘mark‘;
 12     },
 13     show: function(html,options) {
 14         options = options || {};
 15         if(!this._cover){
 16             this._createCover();
 17         }
 18         if(!this._floater){
 19             this._createFloater(html);
 20         }
 21         if(options.saveOpt){
 22             this._saveOption = options.saveOpt;
 23             this.bindSaveEvent();
 24         }
 25         this._bindScrollEvent();
 26         this.addjustPosition();
 27         this._floater.style.display = ‘‘;
 28         this._cover.style.display = ‘‘;
 29         this.isShow = true;
 30     },
 31     insert: function(html,opts,att){
 32         var _e = document.createElement("div"), _t;
 33         _e.innerHTML = html;
 34         for(var k in opts){
 35             _e[k] = opts[k];
 36         }
 37         _t = this._floater.querySelector(‘[‘+att+‘]‘);
 38         if(_t){
 39             _t.appendChild(_e);
 40         }
 41     },
 42     getFloater: function(){
 43         if(this._floater){
 44             return this._floater;
 45         }
 46     },
 47     //遮罩层
 48     _createCover: function() {
 49         var newMask = document.createElement("div");
 50         newMask.id = this._mark;
 51         newMask.style.position = "absolute";
 52         newMask.style.zIndex = "100";
 53         _scrollWidth = Math.max(document.body.scrollWidth,document.documentElement.scrollWidth);
 54         _scrollHeight = Math.max(document.body.scrollHeight,document.documentElement.scrollHeight);
 55         newMask.style.width = _scrollWidth + "px";
 56         newMask.style.height = _scrollHeight + "px";
 57         newMask.style.top = "0px";
 58         newMask.style.left = "0px";
 59         newMask.style.background = "#000";
 60         newMask.style.filter = "alpha(opacity=50)";
 61         newMask.style.opacity = "0.50";
 62         newMask.style.display = ‘none‘;
 63         document.body.appendChild(newMask);
 64         this._cover = newMask;
 65     },
 66     //新弹出层
 67     _createFloater: function(html) {
 68         var newDiv = document.createElement("div");
 69         newDiv.id = this._id;
 70         newDiv.style.position = "absolute";
 71         newDiv.style.zIndex = "9999";
 72         newDivWidth = 400;
 73         newDivHeight = 200;
 74         newDiv.style.width = newDivWidth + "px";
 75         newDiv.style.height = newDivHeight + "px";
 76         newDiv.style.top = (document.body.scrollTop + document.body.clientHeight/2 - newDivHeight/2) + "px";
 77         newDiv.style.left = (document.body.scrollLeft + document.body.clientWidth/2 - newDivWidth/2) + "px";
 78         newDiv.style.padding = "5px";
 79         newDiv.style.display = ‘none‘;
 80         newDiv.innerHTML = html;
 81         document.body.appendChild(newDiv);
 82         this._floater = newDiv;
 83     },
 84     //弹出层滚动居中
 85     addjustPosition: function() {
 86         this._floater.style.top = (document.body.scrollTop + document.body.clientHeight/2 - newDivHeight/2) + "px";
 87         this._floater.style.left = (document.body.scrollLeft + document.body.clientWidth/2 - newDivWidth/2) + "px";
 88     },
 89     bindSaveEvent: function() {
 90         this._saveElem = this._floater.querySelector(‘[‘+this._saveOption.elem+‘]‘);
 91         if(this._saveElem){
 92             addEventHandler(this._saveElem, "click", this._saveOption.handler);
 93         }
 94     },
 95     _bindScrollEvent: function() {
 96         addEventHandler(window, "scroll", this._fS);
 97     },
 98     hide: function() {
 99         this.isShow = false;
100         this.destory();
101     },
102     destory: function() {
103         removeEventHandler(window, "scroll", this._fS);
104         if(this._saveElem){
105             removeEventHandler(this._saveElem, "click", this._saveOption.handler);
106         }
107         if (this._cover){
108             document.body.removeChild(this._cover);
109         }
110         if (this._floater){
111             document.body.removeChild(this._floater);
112         }
113         this._cover = null;
114         this._floater = null;
115     }
116 };
117 return me;
118 })();

时间: 2024-10-17 08:52:48

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

原生Js封装的弹出框

<!DOCTYPE HTML> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>原生Js封装的弹出框-弹出窗口-页面居中-多状态可选</title> <style type="text/css"&

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

layer.js是一个方便的弹出层插件,切图网专注于PSD2HTML等前端切图多年,后转向Vue开发.在vue开发过程中引入layer.js的时候遇到了麻烦.原因是layer.js不支持import导入,这时就需要修改一下它的源码.在看过它的源码后,发现需要修改的地方只有两处, 源码中已经暴露了一个全局变量layer,故只需在脚本末尾处添加 export default layer; 这表示将这个全局变量导出. 然后在文件中找到下面的代码注释掉,这段代码是为layer添加样式的,但使用的路径不对

原生小程序底部弹出层动画过渡

1.wxml <view class="my-dialog" > <view class="my-mask" bindtap="confirmDialog" hidden="{{!showDialog}}" ></view> <view class="my-container" animation="{{animatheightadd}}">

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Typ

js漂亮的弹出层

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

Js面试题(三)--js点击弹出对应序号

<!-- 点击ul的五个li元素,分别弹出序号 --> <ul> <li>1</li> <li>2</li> <li>3</li> <li>4</li> <li>5</li> </ul> 第一种方法,当然也是最容易想到 第二种方法,采用外部参数函数调用 第三种方法,采用设置属性,点击事件,然后对应方法 第四种方法,分别让每个li对象设置index关联到

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

layer.js包含了所有的层级情形,并且附加的有:tab层,相册层.webIM层. 适用于移动版本的layer.js   为layer for mobile 配套的layui 非常适合用于后台系统的ui框架.用法在官网都有说明:参见http://layer.layui.com/   文档手册 另外它的fly社区有许多demo案例提供给我们参考

工作日志WebRoot--时间插件弹出层被遮挡

解决:通过chrome找出该插件的css文件bootstrap-timepicker.css 及该css( .bootstrap-timepicker-widget.dropdown-menu.open), 在该css中插入:z-index:11000; .bootstrap-timepicker-widget.dropdown-menu.open { z-index:11000; display: inline-block; }

js点击任意区域弹出层消失,js点击弹出层之外的任意区域弹出层消失,常见弹层的封装

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> </head> <body> <style type="text/css"> *{margin:0;padding:0;} .warp{overflow:hidden;} .div1,.div2,.div3,.div4{wi