使用原生JS实现弹出层特效


Posted in Javascript onDecember 22, 2014

创建遮罩层

  _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;

 })();
Javascript 相关文章推荐
javascript之解决IE下不渲染的bug
Jun 29 Javascript
js获取height和width的方法说明
Jan 06 Javascript
js简单实现让文本框内容逐个字的显示出来
Oct 22 Javascript
ES6 Promise对象概念与用法分析
Apr 01 Javascript
vue组件如何被其他项目引用
Apr 13 Javascript
微信小程序 slider的简单实例
Apr 19 Javascript
浅谈Vue.js 1.x 和 2.x 实例的生命周期
Jul 25 Javascript
jQuery实现可兼容IE6的淡入淡出效果告警提示功能示例
Sep 20 jQuery
vue.js父子组件通信动态绑定的实例
Sep 28 Javascript
Vue.js数字输入框组件使用方法详解
Oct 19 Javascript
vue-cli基础配置及webpack配置修改的完整步骤
Oct 20 Javascript
基于Vue+Webpack拆分路由文件实现管理
Nov 16 Javascript
jQuery基础知识小结
Dec 22 #Javascript
jQuery异步获取json数据方法汇总
Dec 22 #Javascript
jQuery的观察者模式详解
Dec 22 #Javascript
使用jQuery和Bootstrap实现多层、自适应模态窗口
Dec 22 #Javascript
sails框架的学习指南
Dec 22 #Javascript
了不起的node.js读书笔记之mongodb数据库交互
Dec 22 #Javascript
javascript动态创建及删除元素的方法
Dec 22 #Javascript
You might like
完美实现GIF动画缩略图的php代码
2011/01/02 PHP
Laravel框架模板加载,分配变量及简单路由功能示例
2018/06/11 PHP
JQuery学习笔记 nt-child的使用
2011/01/17 Javascript
Jquery 跨域访问 Lightswitch OData Service的方法
2013/09/11 Javascript
单击和双击事件的冲突处理示例代码
2014/04/03 Javascript
jquery实现带二级菜单的导航示例
2014/04/28 Javascript
window.open()详解及浏览器兼容性问题示例探讨
2014/05/29 Javascript
AspNet中使用JQuery上传插件Uploadify详解
2015/05/20 Javascript
jQuery Mobile框架中的表单组件基础使用教程
2016/05/17 Javascript
jquery UI Datepicker时间控件冲突问题解决
2016/12/16 Javascript
深入理解javascript的getTime()方法
2017/02/16 Javascript
详解如何使用Vue2做服务端渲染
2017/03/29 Javascript
javaScript强制保留两位小数的输入数校验和小数保留问题
2018/05/09 Javascript
Angular2之二级路由详解
2018/08/31 Javascript
详解koa2学习中使用 async 、await、promise解决异步的问题
2018/11/13 Javascript
webpack自动打包和热更新的实现方法
2019/06/24 Javascript
JS中的算法与数据结构之集合(Set)实例详解
2019/08/20 Javascript
layui给下拉框、按钮状态、时间赋初始值的方法
2019/09/10 Javascript
国内常用的js类库大全(CDN公共库)
2020/06/24 Javascript
JavaScript实现复选框全选和取消全选
2020/11/20 Javascript
Python中请使用isinstance()判断变量类型
2014/08/25 Python
详解Python中的__new__()方法的使用
2015/04/09 Python
Python判断Abundant Number的方法
2015/06/15 Python
python中plot实现即时数据动态显示方法
2018/06/22 Python
解决Python获取字典dict中不存在的值时出错问题
2018/10/17 Python
Gauss-Seidel迭代算法的Python实现详解
2019/06/29 Python
Python3 执行系统命令并获取实时回显功能
2019/07/09 Python
pycharm下配置pyqt5的教程(anaconda虚拟环境下+tensorflow)
2020/03/25 Python
使用javascript和HTML5 Canvas画的四渐变色播放按钮效果
2014/04/10 HTML / CSS
英国户外玩具儿童游乐设备网站:TP Toys(蹦床、攀爬框架、秋千、滑梯和游戏屋)
2018/04/09 全球购物
乐高奥地利官方商店:LEGO Shop AT
2019/07/16 全球购物
应征英语教师求职信
2013/11/27 职场文书
护士试用期自我鉴定
2014/02/08 职场文书
英文求职信写作小建议
2014/02/16 职场文书
个人作风建设剖析材料
2014/10/11 职场文书
导游词幽默开场白
2019/06/26 职场文书