歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> 一個輕量級的網頁遮罩層jQuery插件

一個輕量級的網頁遮罩層jQuery插件

日期:2017/3/1 10:34:18   编辑:Linux編程

使用jQuery的好處是很多人為它寫一些組件,而在項目所需用到功能也都可以找到一些組件去完成。

現在又這樣一個需求當用戶點擊一個按鈕後不允許用戶進行任何的操作,取而代之的是彈出一個遮罩層顯示一個loading提示框,效果如下。

其實這個需求很簡單,但很多組件體積相對這個需求來說太大了,在網上瞎溜達了找到了一個還不錯的組件,作者是上面也沒有寫。現在就來分析一下這個組件的源碼和使用

[javascript]

  1. /**
  2. * @部分參數說明
  3. */
  4. (function($){
  5. $.fn.extend({
  6. //主函數
  7. toggleLoading: function(options){
  8. // 找到遮罩層
  9. var crust = this.children(".x-loading-wanghe");
  10. // 當前操作的元素
  11. var thisjQuery = this;
  12. // 實現toogle(切換遮罩層出現與消失)效果的判斷方法
  13. if(crust.length>0){
  14. if(crust.is(":visible")){
  15. crust.fadeOut(500);
  16. }else{
  17. crust.fadeIn(500);
  18. }
  19. return this;
  20. }
  21. // 擴展參數
  22. var op = $.extend({
  23. z: 9999,
  24. msg:'數據加載中...',
  25. iconUrl:'images/loading.gif',
  26. width:18,
  27. height:18,
  28. borderColor:'#6bc4f5',
  29. opacity:0.5,
  30. agentW:thisjQuery.outerWidth(),
  31. agentH:thisjQuery.outerHeight()
  32. },options);
  33. if(thisjQuery.css("position")=="static")
  34. thisjQuery.css("position","relative");
  35. //var w = thisjQuery.outerWidth(),h = thisjQuery.outerHeight();
  36. var w = op.agentW,h = op.agentH;
  37. crust = $("<div></div>").css({//外殼
  38. 'position': 'absolute',
  39. 'z-index': op.z,
  40. 'display':'none',
  41. 'width':w+'px',
  42. 'height':h+'px',
  43. 'text-align':'center',
  44. 'top': '0px',
  45. 'left': '0px',
  46. 'font-family':'arial',
  47. 'font-size':'12px',
  48. 'font-weight':'500'
  49. }).attr("class","x-loading-wanghe");
  50. var mask = $("<div></div>").css({//蒙版
  51. 'position': 'absolute',
  52. 'z-index': op.z+1,
  53. 'width':'100%',
  54. 'height':'100%',
  55. 'background-color':'#333333',
  56. 'top': '0px',
  57. 'left': '0px',
  58. 'opacity':op.opacity
  59. });
  60. //71abc6,89d3f8,6bc4f5
  61. var msgCrust = $("<span></span>").css({//消息外殼
  62. 'position': 'relative',
  63. 'top': (h-30)/2+'px',
  64. 'z-index': op.z+2,
  65. 'height':'24px',
  66. 'display':'inline-block',
  67. 'background-color':'#cadbe6',
  68. 'padding':'2px',
  69. 'color':'#000000',
  70. 'border':'1px solid '+op.borderColor,
  71. 'text-align':'left',
  72. 'opacity':0.9
  73. });
  74. var msg = $("<span>"+op.msg+"</span>").css({//消息主體
  75. 'position': 'relative',
  76. 'margin': '0px',
  77. 'z-index': op.z+3,
  78. 'line-height':'22px',
  79. 'height':'22px',
  80. 'display':'inline-block',
  81. 'background-color':'#efefef',
  82. 'padding-left':'25px',
  83. 'padding-right':'5px',
  84. 'border':'1px solid '+op.borderColor,
  85. 'text-align':'left',
  86. 'text-indent':'0'
  87. });
  88. var msgIcon = $("<img src="+op.iconUrl+" />").css({//圖標
  89. 'position': 'absolute',
  90. 'top': '3px',
  91. 'left':'3px',
  92. 'z-index': op.z+4,
  93. 'width':'18px',
  94. 'height':'18px'
  95. });
  96. // 拼裝遮罩層
  97. msg.prepend(msgIcon);
  98. msgCrust.prepend(msg);
  99. crust.prepend(mask);
  100. crust.prepend(msgCrust);
  101. thisjQuery.prepend(crust);
  102. // alert(thisjQuery.html());
  103. crust.fadeIn(500);
  104. //模態設置
  105. return this;
  106. }
  107. });
  108. })(jQuery);

相關配置

配置&configure 全部配置 默認值 說明 z: 9999 圖層z-index,當蒙版遮罩不住時候適當增大其值 msg: 數據加載中... 提示信息 iconUrl: images/loading.gif 提示圖片url height: 18 圖標默認高(px) width: 18 圖標默認寬(px) borderColor #6bc4f5 提示的邊框顏色 opacity: 0.5 蒙版的透明度 agentW: 當前元素的寬度 蒙版的寬度 agentH: 當前元素的高度 蒙版的高度
Copyright © Linux教程網 All Rights Reserved