使用原生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中关于执行环境的杂谈
Aug 14 Javascript
js函数名与form表单元素同名冲突的问题
Mar 07 Javascript
jQuery操作元素css样式的三种方法
Jun 04 Javascript
实例详解angularjs和ajax的结合使用
Oct 22 Javascript
jQuery模拟Marquee实现无缝滚动效果完整实例
Sep 29 Javascript
通过原生JS实现为元素添加事件的方法
Nov 23 Javascript
Vue响应式原理详解
Apr 18 Javascript
基于JavaScript实现无缝滚动效果
Jul 21 Javascript
jQuery条件分页 代替离线查询(附代码)
Aug 17 jQuery
浅谈Vue数据绑定的原理
Jan 08 Javascript
解决vue scoped scss 无效的问题
Sep 04 Javascript
解决新建一个vue项目过程中遇到的问题
Oct 22 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
用定制的PHP应用程序来获取Web服务器的状态信息
2006/10/09 PHP
php zend解密软件绿色版测试可用
2008/04/14 PHP
thinkPHP中配置的读取与C方法详解
2016/12/05 PHP
YII2框架中使用yii.js实现的post请求
2017/04/09 PHP
thinkPHP5框架分页样式类完整示例
2018/09/01 PHP
JavaScript 模拟用户单击事件
2009/12/31 Javascript
jquery 单引号和双引号的区别及使用注意
2013/07/31 Javascript
使用js的replace()方法查找字符示例代码
2013/10/28 Javascript
jQuery中hover方法和toggle方法使用指南
2015/02/27 Javascript
分享12个实用的jQuery代码片段
2016/03/09 Javascript
js时间戳和c#时间戳互转方法(推荐)
2017/02/15 Javascript
在 Angular 中实现搜索关键字高亮示例
2017/03/21 Javascript
Vue单页及多页应用全局配置404页面实践记录
2018/05/22 Javascript
vue源码学习之Object.defineProperty 对数组监听
2018/05/30 Javascript
JavaScript中filter的用法实例分析
2019/02/27 Javascript
微信小程序 多行文本显示...+显示更多按钮和收起更多按钮功能
2019/09/26 Javascript
ES6使用新特性Proxy实现的数据绑定功能实例
2020/05/11 Javascript
[05:09]2016国际邀请赛中国区预选赛淘汰赛首日精彩回顾
2016/06/29 DOTA
python实现最大优先队列
2019/08/29 Python
Python一行代码解决矩阵旋转的问题
2019/11/30 Python
python调用c++返回带成员指针的类指针实例
2019/12/12 Python
Python3标准库之dbm UNIX键-值数据库问题
2020/03/24 Python
用python写一个带有gui界面的密码生成器
2020/11/06 Python
原生canvas制作画图小工具的踩坑和爬坑
2020/06/09 HTML / CSS
ET Mall东森购物网:东森严选
2017/03/06 全球购物
Blue Nile台湾:钻石珠宝商,订婚首饰、结婚戒指和精品首饰
2017/11/24 全球购物
XD健身器材:Kevlar球、Crossfit健身球
2019/03/26 全球购物
舞蹈毕业生的自我评价
2014/03/05 职场文书
农村结婚典礼司仪主持词
2014/03/14 职场文书
新闻专业毕业生英文求职信
2014/03/19 职场文书
党的群众路线教育实践方案
2014/05/11 职场文书
工会工作个人总结
2015/03/03 职场文书
毕业论文致谢部分怎么写
2015/05/14 职场文书
nginx配置ssl实现https的方法示例
2021/03/31 Servers
MySQL中CURRENT_TIMESTAMP的使用方式
2021/11/27 MySQL
Go并发4种方法简明讲解
2022/04/06 Golang