源码网,源码论坛,源码之家,商业源码,游戏源码下载,discuz插件,棋牌源码下载,精品源码论坛

 找回密码
 立即注册
楼主: ttx9n

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

[复制链接]

7万

主题

861

回帖

32万

积分

论坛元老

Rank: 8Rank: 8

积分
329525
发表于 2018-12-25 05:42:48 | 显示全部楼层 |阅读模式
这篇文章主要介绍了使用原生JS实现弹出层特效,需要的朋友可以参考下

创建遮罩层
复制代码 代码如下:
  _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;
 })();

回复

使用道具 举报

1

主题

2万

回帖

207

积分

中级会员

Rank: 3Rank: 3

积分
207
发表于 2022-8-26 13:55:48 | 显示全部楼层
女生看了弄丢了卡萨诺的卡洛斯
回复 支持 反对

使用道具 举报

6

主题

2万

回帖

425

积分

中级会员

Rank: 3Rank: 3

积分
425
发表于 2022-8-28 03:28:25 | 显示全部楼层
天天源码社区论坛
回复 支持 反对

使用道具 举报

0

主题

2万

回帖

66

积分

注册会员

Rank: 2

积分
66
发表于 2024-1-11 07:16:39 | 显示全部楼层
1312315458748777
回复 支持 反对

使用道具 举报

1

主题

2万

回帖

69

积分

注册会员

Rank: 2

积分
69
发表于 2024-3-21 13:35:42 | 显示全部楼层
挺不错的东西
回复 支持 反对

使用道具 举报

2

主题

2万

回帖

499

积分

中级会员

Rank: 3Rank: 3

积分
499
发表于 2024-5-1 00:19:48 | 显示全部楼层
这个源码还可以
回复 支持 反对

使用道具 举报

0

主题

2万

回帖

115

积分

注册会员

Rank: 2

积分
115
发表于 2024-6-22 12:35:51 | 显示全部楼层
还有什么好东西没
回复 支持 反对

使用道具 举报

2

主题

2万

回帖

73

积分

注册会员

Rank: 2

积分
73
发表于 2024-8-12 01:57:40 | 显示全部楼层
大家都不容易!
回复 支持 反对

使用道具 举报

0

主题

2万

回帖

61

积分

注册会员

Rank: 2

积分
61
发表于 2024-9-2 01:30:58 | 显示全部楼层
额头额定法国队是范德萨
回复 支持 反对

使用道具 举报

1

主题

2万

回帖

321

积分

中级会员

Rank: 3Rank: 3

积分
321
发表于 2024-9-21 03:32:54 | 显示全部楼层
还不错啊
回复 支持 反对

使用道具 举报

高级模式
B Color Image Link Quote Code Smilies

本版积分规则

手机版|小黑屋|网站地图|源码论坛 ( 海外版 )

GMT+8, 2025-2-2 08:42 , Processed in 0.072903 second(s), 23 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表