jQuery弹出层插件简化版

  1. String.prototype.replaceAll=function(s1,s2){
  2. returnthis.replace(newRegExp(s1,"gm"),s2);
  3. };
  4. (function($){
  5. /*
  6. *$-layer0.1-NewWaveJavascript
  7. *
  8. *Copyright(c)2008KingWong
  9. *$Date:2008-10-09$
  10. */
  11. var___id___="";
  12. var___settings___={};
  13. varisMouseDown=false;
  14. varcurrentElement=null;
  15. vardropCallbacks={};
  16. vardragCallbacks={};
  17. varbubblings={};
  18. varlastMouseX;
  19. varlastMouseY;
  20. varlastElemTop;
  21. varlastElemLeft;
  22. vardragStatus={};
  23. varholdingHandler=false;
  24. $.getMousePosition=function(e){
  25. varposx=0;
  26. varposy=0;
  27. if(!e)vare=window.event;
  28. if(e.pageX||e.pageY){
  29. posx=e.pageX;
  30. posy=e.pageY;
  31. }
  32. elseif(e.clientX||e.clientY){
  33. posx=e.clientX+document.body.scrollLeft+document.documentElement.scrollLeft;
  34. posy=e.clientY+document.body.scrollTop+document.documentElement.scrollTop;
  35. }
  36. return{'x':posx,'y':posy};
  37. };
  38. $.updatePosition=function(e){
  39. varpos=$.getMousePosition(e);
  40. varspanX=(pos.x-lastMouseX);
  41. varspanY=(pos.y-lastMouseY);
  42. var_top=(lastElemTop+spanY)>0?(lastElemTop+spanY):0;
  43. var_left=(lastElemLeft+spanX)>0?(lastElemLeft+spanX):0;
  44. $("#"+___id___).css("top",_top);
  45. $("#"+___id___).css("left",_left);
  46. };
  47. $.fn.οndrag=function(callback){
  48. returnthis.each(function(){
  49. dragCallbacks[this.id]=callback;
  50. });
  51. };
  52. $.fn.οndrοp=function(callback){
  53. returnthis.each(function(){
  54. dropCallbacks[this.id]=callback;
  55. });
  56. };
  57. $.fn.dragOff=function(){
  58. returnthis.each(function(){
  59. dragStatus[this.id]='off';
  60. });
  61. };
  62. $.fn.dragOn=function(){
  63. returnthis.each(function(){
  64. dragStatus[this.id]='on';
  65. });
  66. };
  67. $.extend({
  68. layerSettings:{
  69. id:"layerdiv",
  70. width:220,
  71. height:220,
  72. templete:'<divstyle="height:20px;width:@width@px;background-color:#777777;"><spanid="@moveid@"style="position:relative;left:0px;top:0px;height:20px;width:100px;"><spanid="@titleid@">@title@</span></span><spanclass="layerclose"style="position:relative;top:0px;float:right;right:20px;color:red;">close</span></div><divstyle="border:solid#ff00001px;width:@width@px;height:@height@px;"><divstyle="width:100%;height:100%;background-color:#ffffff;"id="@contentid@"></div></div>',
  73. content:'',
  74. title:'',
  75. isbg:true,
  76. opacity:0.3
  77. },
  78. layerSetup:function(settings){
  79. $.extend($.layerSettings,settings);
  80. ___settings___[settings.id]=settings;
  81. ___id___=settings.id;
  82. },
  83. layershow:function(){
  84. var__bw=$("body").width();
  85. var__bh=$("body").height()>$(window).height()?$("body").height():$(window).height();
  86. var_width=$.layerSettings.width;
  87. var_height=$.layerSettings.height;
  88. if(document.getElementById(___id___))return;
  89. var_moveid=___id___+"_move";
  90. var_titleid=___id___+"_title";
  91. var_contentid=___id___+"_content";
  92. var_cssurl=$.layerSettings.cssurl;
  93. varopacity=$.layerSettings.opacity;
  94. __index=$.layermaxindex();
  95. var__left=(__bw-_width)>0?(__bw-_width)/2:0;
  96. var__top=100;
  97. var__bgDiv='<divid="'+___id___+'_background"style="background:#000000;filter:alpha(opacity='+(opacity*100)+');opacity:'+opacity+';width:'+__bw+'px;height:'+__bh+'px;z-index:'+(__index++)+';position:absolute;left:0px;top:0px;"></div>';
  98. if($.layerSettings.isbg)
  99. {
  100. $("body").append(__bgDiv);
  101. }
  102. $("body").append('<divid="'+___id___+'"style="z-index:'+__index+';position:absolute;left:'+__left+'px;top:'+__top+'px;"></div>');
  103. var_templete=$.layerSettings.templete;
  104. var__templete=_templete.replaceAll("@width@",_width).replaceAll("@height@",_height).replaceAll("@titleid@",_titleid).replaceAll("@contentid@",_contentid).replaceAll("@title@",jQuery.layerSettings.title).replaceAll("@moveid@",_moveid);
  105. $("#"+___id___).append(__templete);
  106. $("#"+_contentid).append($.layerSettings.content);
  107. $("#"+_titleid).append($.layerSettings.title);
  108. varidd=___id___;
  109. $(".layerclose").bind("click",function()
  110. {
  111. $.layerclose(idd);
  112. });
  113. $("#"+___id___).bind("click",function()
  114. {
  115. varid=this.id;
  116. $.layerSetup(___settings___[id]);
  117. $(this).css("z-index",$.layermaxindex());
  118. });
  119. $(document).bind("click",function(e)
  120. {
  121. varpos=$.getMousePosition(e);
  122. });
  123. $(document).mousemove(function(e){
  124. if(isMouseDown&&dragStatus[currentElement.id]!='false'){
  125. $.updatePosition(e);
  126. if(dragCallbacks[currentElement.id]!=undefined){
  127. dragCallbacks[currentElement.id](e,currentElement);
  128. }
  129. returnfalse;
  130. }
  131. });
  132. $(document).mouseup(function(e){
  133. if(isMouseDown&&dragStatus[currentElement.id]!='false'){
  134. isMouseDown=false;
  135. if(dropCallbacks[currentElement.id]!=undefined){
  136. dropCallbacks[currentElement.id](e,currentElement);
  137. }
  138. returnfalse;
  139. }
  140. });
  141. (function(){
  142. bubblings[___id___]=true;
  143. dragStatus[___id___]="on";
  144. //setHandler
  145. bubblings[this.id]=true;
  146. dragStatus[_moveid]="handler";
  147. $("#"+_moveid).css("cursor","move");
  148. $("#"+_moveid).mousedown(function(e){
  149. varid=this.id.replace("_move","");
  150. ___id___=id;
  151. $("#"+id).css("z-index",$.layermaxindex());
  152. $.layerSetup(___settings___[id]);
  153. if((dragStatus[___id___]=="off")||(dragStatus[___id___]=="handler"&&!holdingHandler))
  154. returnbubblings["#"+___id___];
  155. isMouseDown=true;
  156. currentElement=$("#"+___id___);
  157. varpos=$.getMousePosition(e);
  158. lastMouseX=pos.x;
  159. lastMouseY=pos.y;
  160. lastElemTop=document.getElementById(___id___).offsetTop;
  161. lastElemLeft=document.getElementById(___id___).offsetLeft;
  162. $.updatePosition(e);
  163. holdingHandler=true;
  164. });
  165. $("#"+_moveid).mouseup(function(e){
  166. holdingHandler=false;
  167. });
  168. //endsetHandler
  169. })();
  170. },
  171. layerclose:function(__id)
  172. {
  173. $("#"+__id+"_background").remove();
  174. $("#"+__id).remove();
  175. },
  176. layermaxindex:function()
  177. {
  178. var___index=0;
  179. $.each($("*"),function(i,n){
  180. var__tem=$(n).css("z-index");
  181. if(__tem>0)
  182. {
  183. if(__tem>___index)
  184. {
  185. ___index=__tem+1;
  186. }
  187. }
  188. });
  189. return___index;
  190. }
  191. });
  192. })(jQuery);

使用方法:

(1)显示层:

  1. functionshow()
  2. {
  3. $.layerSetup({
  4. id:"abc",//弹出层的ID
  5. title:"test",//标题
  6. content:'test',//内容
  7. isbg:false,//是否显示背景遮照层
  8. opacity:0.3,//背景遮照层的透明度,值越大透明度越低,0为完全透明,1为不透明
  9. templete:'<divclass="showwint_mini_title"><spanclass="showwint_mini_close_btn"><ahref="javascript:void(null);"class="layerclose"></a></span><spanclass="showwint_mini_title_content"id="@moveid@"><spanid="@titleid@"></span></span></div><divclass="showwint_mini_content"><divclass="showwint_mini_content_content"id="@contentid@"></div></div>'//模板
  10. });
  11. $.layershow();
  12. }

(2)关闭层:

  1. $.layerclose("弹出层的ID");

注意:那个如果你的模板有CSS文件的话,就要直接引入到本页面就行了,这个简化版不能在别的框架弹出层的,所以也就没有了那个cssurl属性了,也没有target 属性了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值