使用jQuery制作Web頁面遮罩層插件的實例教程
來源:易賢網(wǎng) 閱讀:1368 次 日期:2016-06-20 16:21:21
溫馨提示:易賢網(wǎng)小編為您整理了“使用jQuery制作Web頁面遮罩層插件的實例教程”,方便廣大網(wǎng)友查閱!

GitHub上人們分享的遮罩層插件也是玲瑯滿目,不過自己動手做一個的話肯定更復(fù)合自己的需求,這里就帶大家來看使用jQuery制作Web頁面遮罩層插件的實例教程,需要的朋友可以參考下

在網(wǎng)頁上經(jīng)常遇到需要等待很久的操作,比如導(dǎo)出報表等。為了預(yù)防用戶點(diǎn)擊其他操作或者多次點(diǎn)擊同個功能,需要用遮罩層把頁面或者操作區(qū)蓋住,防止用戶進(jìn)行下一步操作,同時可以提高界面友好度,讓用戶知道操作正在執(zhí)行。

$.fn.extend({ 

  /** 

   * 給元素添加遮罩層 

   * @param message {String} [可選]遮罩層顯示內(nèi)容 

   */

  mask: function (message) { 

    var $target = this, 

      fixed = false, 

      targetStatic = true; 

    if (Object.prototype.toString.call(message) !== '[object String]' || !message) { 

      //如果message為空或者不是字符串,則用默認(rèn)的消息提示。 

      message = '請稍候。。。'; 

    } 

    if ($target.length === 0) { 

      $target = $('body'); 

    } else { 

      if ($target.length > 1) { 

        $target = $target.first(); 

      } 

      if ($target[0] === window || $target[0] === document) { 

        $target = $('body'); 

      } 

    } 

      

    if($target[0] === document.body){ 

      fixed = true; 

    } 

    //如果目標(biāo)元素已經(jīng)有遮罩層,獲取遮罩層 

    var old = $target.data('rhui.mask'); 

    if (old) { 

      old.$content.html(message); 

      center($target, old.$content, fixed); 

      return; 

    } 

    //如果被遮蓋的元素是static,把元素改成relative 

    if ($target.css('position') === 'static') { 

      targetStatic = true; 

      $target.css('position', 'relative'); 

    } 

    var $content, $overlay; 

    if (fixed) { 

      $overlay = $('<div class="rhui-mask" style="position:fixed;"></div>'); 

      $content = $('<div class="rhui-mask-content" style="position:fixed;">' + message + '</div>'); 

    } else { 

      $overlay = $('<div class="rhui-mask"></div>'); 

      $content = $('<div class="rhui-mask-content">' + message + '</div>'); 

    } 

    $overlay.appendTo($target); 

    $content.appendTo($target); 

    //顯示遮罩層 

    $overlay.show(); 

    $content.show(); 

    //讓遮罩層居中 

    center($target, $content, fixed); 

    //把遮罩層信息添加到$target 

    $target.data('rhui.mask', { 

      fixed: fixed, 

      $overlay: $overlay, 

      $content: $content, 

      targetStatic: targetStatic 

    }); 

    /** 

     * 讓遮罩層內(nèi)容居中顯示 

     * @param $target  被遮蓋的元素 

     * @param $content 遮罩層內(nèi)容元素 

     * @param fixed   遮罩層是否固定顯示 

     */

    function center($target, $content, fixed) { 

      var $window, 

        height = $content.outerHeight(true), 

        width = $content.outerWidth(true); 

      if (fixed) { 

        //如果遮罩層固定顯示,讓遮罩層在window居中 

        $window = $(window); 

        $content.css({ 

          left: ($window.width() - width) / 2, 

          top: ($window.height() - height) / 2 

        }); 

      } else { 

        //讓遮罩層在$target中居中 

        $content.css({ 

          left: ($target.width() - width) / 2, 

          top: ($target.height() - height) / 2 

        }); 

      } 

    } 

  }, 

  /** 

   * 取消遮罩層 

   */

  unmask: function () { 

    var $target; 

    if (this.length === 0) { 

      $target = $('body'); 

    } else { 

      $target = this.first(); 

      if ($target[0] === window || $target[0] === document) { 

        $target = $('body'); 

      } 

    } 

    var data = $target.data('rhui.mask'); 

    if (!data) { 

      return; 

    } 

    //還原目標(biāo)元素的position屬性 

    if (data.targetStatic) { 

      $target.css('position', 'static'); 

    } 

    data.$overlay.remove(); 

    data.$content.remove(); 

    $target.removeData('rhui.mask'); 

  } 

}); 

插件樣式由rhui-mask和rhui-mask-content類控制,rhui-mask是遮罩層樣式,rhui-mask-content是遮罩層的提示內(nèi)容樣式。

/* 遮罩層樣式 */

.rhui-mask { 

  position: absolute; 

  top: 0; 

  right: 0; 

  bottom: 0; 

  left: 0; 

  z-index: 9000; 

  display: block; 

  margin: 0; 

  padding: 0; 

  border-style: none; 

  background-color: #777; 

  opacity: 0.3; 

  zoom: 1; 

  filter: alpha(opacity=30); 

/* 遮罩層顯示內(nèi)容樣式 */

.rhui-mask-content { 

  position: absolute; 

  z-index: 9999; 

  display: block; 

  margin: 0; 

  padding: 15px 20px; 

  border: 2px solid rgb(109, 157, 215); 

  background-color: #fff; 

  color: blue; 

  letter-spacing: 2px; 

  font-weight: bold; 

  font-size: 15px; 

  cursor: wait; 

效果如圖所示

名單

頁面調(diào)用完整代碼

<!DOCTYPE html> 

<html> 

<head> 

  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 

  <title>網(wǎng)頁遮罩層的實現(xiàn)</title> 

  <style type="text/css"> 

    /* 遮罩層樣式 */   

    .rhui-mask { 

      position: absolute; 

      top: 0; 

      right: 0; 

      bottom: 0; 

      left: 0; 

      z-index: 9000; 

      display: block; 

      margin: 0; 

      padding: 0; 

      border-style: none; 

      background-color: #777; 

      opacity: 0.3; 

      zoom: 1; 

      filter: alpha(opacity=30); 

    } 

      

    /* 遮罩層顯示內(nèi)容樣式 */   

    .rhui-mask-content { 

      position: absolute; 

      z-index: 9999; 

      display: block; 

      margin: 0; 

      padding: 15px 20px; 

      border: 2px solid rgb(109, 157, 215); 

      background-color: #fff; 

      color: blue; 

      letter-spacing: 2px; 

      font-weight: bold; 

      font-size: 15px; 

      cursor: wait; 

    } 

  </style> 

  <script type="text/javascript" src="http://cdn.bootcss.com/jquery/1.11.3/jquery.js"></script> 

  <script type="text/javascript"> 

    $.fn.extend({ 

      /** 

       * 給元素添加遮罩層 

       * @param message {String} [可選]遮罩層顯示內(nèi)容 

       */

      mask: function (message) { 

        var $target = this, 

          fixed = false, 

          targetStatic = true; 

        if (Object.prototype.toString.call(message) !== '[object String]' || !message) { 

          //如果message為空或者不是字符串,則用默認(rèn)的消息提示。 

          message = '請稍候。。。'; 

        } 

        if ($target.length === 0) { 

          $target = $('body'); 

        } else { 

          if ($target.length > 1) { 

            $target = $target.first(); 

          } 

          if ($target[0] === window || $target[0] === document) { 

            $target = $('body'); 

          } 

        } 

        if ($target[0] === document.body) { 

          fixed = true; 

        } 

        //如果目標(biāo)元素已經(jīng)有遮罩層,獲取遮罩層 

        var old = $target.data('rhui.mask'); 

        if (old) { 

          old.$content.html(message); 

          center($target, old.$content, fixed); 

          return; 

        } 

        //如果被遮蓋的元素是static,把元素改成relative 

        if ($target.css('position') === 'static') { 

          targetStatic = true; 

          $target.css('position', 'relative'); 

        } 

        var $content, $overlay; 

        if (fixed) { 

          $overlay = $('<div class="rhui-mask" style="position:fixed;"></div>'); 

          $content = $('<div class="rhui-mask-content" style="position:fixed;">' + message + '</div>'); 

        } else { 

          $overlay = $('<div class="rhui-mask"></div>'); 

          $content = $('<div class="rhui-mask-content">' + message + '</div>'); 

        } 

        $overlay.appendTo($target); 

        $content.appendTo($target); 

        //顯示遮罩層 

        $overlay.show(); 

        $content.show(); 

        //讓遮罩層居中 

        center($target, $content, fixed); 

        //把遮罩層信息添加到$target 

        $target.data('rhui.mask', { 

          fixed: fixed, 

          $overlay: $overlay, 

          $content: $content, 

          targetStatic: targetStatic 

        }); 

        /** 

         * 讓遮罩層內(nèi)容居中顯示 

         * @param $target  被遮蓋的元素 

         * @param $content 遮罩層內(nèi)容元素 

         * @param fixed   遮罩層是否固定顯示 

         */

        function center($target, $content, fixed) { 

          var $window, 

            height = $content.outerHeight(true), 

            width = $content.outerWidth(true); 

          if (fixed) { 

            //如果遮罩層固定顯示,讓遮罩層在window居中 

            $window = $(window); 

            $content.css({ 

              left: ($window.width() - width) / 2, 

              top: ($window.height() - height) / 2 

            }); 

          } else { 

            //讓遮罩層在$target中居中 

            $content.css({ 

              left: ($target.width() - width) / 2, 

              top: ($target.height() - height) / 2 

            }); 

          } 

        } 

      }, 

      /** 

       * 取消遮罩層 

       */

      unmask: function () { 

        var $target; 

        if (this.length === 0) { 

          $target = $('body'); 

        } else { 

          $target = this.first(); 

          if ($target[0] === window || $target[0] === document) { 

            $target = $('body'); 

          } 

        } 

        var data = $target.data('rhui.mask'); 

        if (!data) { 

          return; 

        } 

        //還原目標(biāo)元素的position屬性 

        if (data.targetStatic) { 

          $target.css('position', 'static'); 

        } 

        data.$overlay.remove(); 

        data.$content.remove(); 

        $target.removeData('rhui.mask'); 

      } 

    }); 

  </script> 

</head> 

<body> 

  <div id="div" style="width:600px;height:300px;margin:10px;border:1px solid red;"></div> 

  <script type="text/javascript"> 

    $(function () { 

      //遮蓋整個頁面 

      //只要對window、document和body使用遮罩層,都會遮蓋整個頁面 

      //$(window).mask();      

      //$(window).unmask(); 取消遮罩 

      //遮蓋div 

      $('#div').mask('加載中,請稍候。。。'); 

    }); 

  </script> 

</body> 

</html> 

更多信息請查看網(wǎng)絡(luò)編程
易賢網(wǎng)手機(jī)網(wǎng)站地址:使用jQuery制作Web頁面遮罩層插件的實例教程
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

2025國考·省考課程試聽報名

  • 報班類型
  • 姓名
  • 手機(jī)號
  • 驗證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺 | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號:hfpxwx
咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報警專用圖標(biāo)