使用原生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 按回车键相应按钮提交事件
Nov 02 Javascript
firefox下input type="file"的size是多大
Oct 24 Javascript
location.href用法总结(最主要的)
Dec 27 Javascript
jquery带动画效果幻灯片特效代码
Aug 27 Javascript
jquery利用拖拽方式在图片上添加热链接
Nov 24 Javascript
easyui取消表单实时验证,提交时统一验证的简单实例
Nov 07 Javascript
vue高德地图之玩转周边
Jun 16 Javascript
jquery 键盘事件的使用方法详解
Sep 13 jQuery
angular中ui calendar的一些使用心得(推荐)
Nov 03 Javascript
微信小程序顶部导航栏滑动tab效果
Jan 28 Javascript
详解小程序之简单登录注册表单验证
May 13 Javascript
jquery实现购物车基本功能
Oct 25 jQuery
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与java通过socket通信的实现代码
2013/10/21 PHP
浅谈php提交form表单
2015/07/01 PHP
PHP之预定义接口详解
2015/07/29 PHP
学习PHP的数组总结【经验】
2016/05/05 PHP
Laravel中GraphQL接口请求频率实战记录
2020/09/01 PHP
Google Map Api和GOOGLE Search Api整合实现代码
2009/07/18 Javascript
jQuery温习篇 强大的JQuery选择器
2010/04/24 Javascript
javascript动画之圆形运动,环绕鼠标运动作小球
2010/07/20 Javascript
js和css写一个可以自动隐藏的悬浮框
2014/03/05 Javascript
javascript实现禁止复制网页内容
2014/12/16 Javascript
nodejs中实现阻塞实例
2015/03/24 NodeJs
js实现表单Radio切换效果的方法
2015/08/17 Javascript
JS实现的N多简单无缝滚动代码(包含图文效果)
2015/11/06 Javascript
JavaScript 基础函数_深入剖析变量和作用域
2016/05/18 Javascript
Javascript highcharts 饼图显示数量和百分比实例代码
2016/12/06 Javascript
Angular2使用jQuery的方法教程
2017/05/28 jQuery
Router解决跨模块下的页面跳转示例
2018/01/11 Javascript
浅谈webpack 四个核心概念之Entry
2019/06/12 Javascript
vue+elementUI实现图片上传功能
2019/08/20 Javascript
vue实现自定义多选按钮
2020/07/16 Javascript
Vue使用Ref跨层级获取组件的步骤
2021/01/25 Vue.js
Python实例之wxpython中Frame使用方法
2014/06/09 Python
python实现周期方波信号频谱图
2018/07/21 Python
python实现旋转和水平翻转的方法
2018/10/25 Python
Python3.5装饰器典型案例分析
2019/04/30 Python
Python使用pymysql模块操作mysql增删改查实例分析
2019/12/19 Python
Matplotlib scatter绘制散点图的方法实现
2020/01/02 Python
有趣的Python图片制作之如何用QQ好友头像拼接出里昂
2020/04/22 Python
matplotlib jupyter notebook 图像可视化 plt show操作
2020/04/24 Python
解决python便携版无法直接运行py文件的问题
2020/09/01 Python
ALDO加拿大官网:加拿大女鞋品牌
2018/12/22 全球购物
房产公证书范本
2014/04/10 职场文书
机关作风整顿个人整改措施2014
2014/09/17 职场文书
总经理岗位职责
2015/02/04 职场文书
公司庆典主持词
2015/07/04 职场文书
使用CSS实现小三角边框原理解析
2021/11/07 HTML / CSS