首頁 > web前端 > js教程 > 使用原生JS實作彈出層特效_javascript技巧

使用原生JS實作彈出層特效_javascript技巧

WBOY
發布: 2016-05-16 16:25:08
原創
1250 人瀏覽過

建立遮罩層

複製程式碼以下程式碼:

  _createCover: 函數() {
      var newMask = document.createElement("div");
      newMask.id = this._mark;
      newMask.style.position = "絕對";
      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: 函數(html) {
      var newDiv = document.createElement("div");
      newDiv.id = this._id;
      newDiv.style.position = "絕對";
      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 = '無';
      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 = {
     初始化:函數(選項){
         this._fS = BindAsEventListener(this, this.addjustPosition);
         this.setOptions(選項);
     },
     setOptions: 函數(選項) {
         this.options = 選項 || {};
         this._id = options.id;
         this._mark = '標記';
     },
     顯示:函數(html,選項){
         選項=選項|| {};
         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;
     },
     插入:函數(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 ']');
         若(_t){
             _t.appendChild(_e);
         }
     },
     getFloater: 函數(){
         if(this._floater){
             回此._floater;
         }
     },
     //遮罩層
     _createCover: 函數() {
         var newMask = document.createElement("div");
         newMask.id = this._mark;
         newMask.style.position = "絕對";
         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: 函數(html) {
         var newDiv = document.createElement("div");
         newDiv.id = this._id;
         newDiv.style.position = "絕對";
         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 = '無';
         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._saveElem = this._floater.querySelector('[' this._saveOption.elem ']');
         if(this._saveElem){
             addEventHandler(this._saveElem, "click", this._saveOption.handler);
         }
     },
     _bindScrollEvent: 函數() {
         addEventHandler(window, "scroll", this._fS);
     },
     隱藏:函數(){
         this.isShow = false;
         this.destory();
     },
     裡面:函數(){
         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;
     }
 };
 返回我;
 })();
相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板