使用原生JS實現彈出層特效,js彈出特效

來源:互聯網
上載者:User

使用原生JS實現彈出層特效,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;
 })();

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.