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

时间:2022-09-09 15:49:44

创建遮罩层

     _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&period;js

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

随机推荐

  1. 自定义 Material Design风格的提示框

    关闭 自定义 Material Design风格的提示框 2016-04-24 10:55 152人阅读 评论(0) 收藏 举报 版权声明:本文为博主原创文章,未经博主允许不得转载. 其实在14年谷歌 ...

  2. Jenkins配置MSBuild时使用环境变量

    [MSBuild Plugin]插件在使用环境变量有个很奇葩的方式,比如我们通常在Windows的节点机器上,使用WORKSPACE环境变量时,批处理应该这样写%WORKSPACE%,而有时插件确不能 ...

  3. 这样leetcode简单题都更完了

    这样leetcode简单题都更完了,作为水题王的我开始要更新leetcode中等题和难题了,有些挖了很久的坑也将在在这个阶段一一揭晓,接下来的算法性更强,我就要开始分专题更新题目,而不是再以我的A题顺 ...

  4. 设置图层符号风格为用已有mxd里的同名图层风格

    //要加载的IFeatureClass IFeatureClass pFeatClass = dataset as IFeatureClass; //新建要加载到mxd文档中的图层 IFeatureL ...

  5. ubuntu环境下安装Tomcat

    tomcat 是javaweb开发的本地服务器,tomcat是目前比较流行的一款. 1.下载Tomcat:http://tomcat.apache.org 2.进入下载文件夹解压Tomcat:sudo ...

  6. javascript 之数据类型

    写在前面 国庆整理资料时,发现刚开始入门前端时学习JS 的资料,打算以一个基础入门博客记录下来,有不写不对的多多指教: 先推荐些书籍给需要的童鞋 <JavaScript 高级程序设计.pdf&g ...

  7. 【洛谷1640】&lbrack;SCOI2010&rsqb;连续攻击游戏

    题目描述 lxhgww最近迷上了一款游戏,在游戏里,他拥有很多的装备,每种装备都有2个属性,这些属性的值用[1,10000]之间的数表示.当他使用某种装备时,他只能使用该装备的某一个属性.并且每种装备 ...

  8. python学习之老男孩python全栈第九期&lowbar;day005作业

    1,有如下变量(tu是个元组),请实现要求的功能. tu = ("alex", [11, 22, {"k1": 'v1', "k2": [& ...

  9. python标准库介绍——37 signal 模块详解

    ==signal 模块== 你可以使用 ``signal`` 模块配置你自己的信号处理器 (signal handler), 如 [Example 3-11 #eg-3-11] 所示. 当解释器收到某 ...

  10. 怎么用一个ppt介绍一个项目