jQuery弹出层插件简化版

  1. String.prototype.replaceAll  = function(s1,s2){
  2. return this.replace(new RegExp(s1,"gm"),s2);    
  3. };
  4. (function($){
  5. /*
  6.  * $-layer 0.1 - New Wave Javascript
  7.  *
  8.  * Copyright (c) 2008 King Wong
  9.  * $Date: 2008-10-09  $
  10.  */
  11. var ___id___ = "";
  12. var ___settings___ = {};
  13. var isMouseDown    = false;
  14. var currentElement = null;
  15. var dropCallbacks = {};
  16. var dragCallbacks = {};
  17. var bubblings = {};
  18. var lastMouseX;
  19. var lastMouseY;
  20. var lastElemTop;
  21. var lastElemLeft;
  22. var dragStatus = {};    
  23. var holdingHandler = false;
  24. $.getMousePosition = function(e){
  25.     var posx = 0;
  26.     var posy = 0;
  27.     if (!e) var e = window.event;
  28.     if (e.pageX || e.pageY) {
  29.         posx = e.pageX;
  30.         posy = e.pageY;
  31.     }
  32.     else if (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.     var pos = $.getMousePosition(e);
  40.     var spanX = (pos.x - lastMouseX);
  41.     var spanY = (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.ondrag = function(callback){
  48.     return this.each(function(){
  49.         dragCallbacks[this.id] = callback;
  50.     });
  51. };
  52. $.fn.ondrop = function(callback){
  53.     return this.each(function(){
  54.         dropCallbacks[this.id] = callback;
  55.     });
  56. };
  57. $.fn.dragOff = function(){
  58.     return this.each(function(){
  59.         dragStatus[this.id] = 'off';
  60.     });
  61. };
  62. $.fn.dragOn = function(){
  63.     return this.each(function(){
  64.         dragStatus[this.id] = 'on';
  65.     });
  66. };
  67. $.extend({
  68.     layerSettings:{
  69.         id:"layerdiv",
  70.         width:220,
  71.         height:220,
  72.         templete:'@title@close
',
  •         content:'',
  •         title:'',
  •         isbg:true,
  •         opacity:0.3
  •     },
  •     layerSetup: function( settings ) {
  •         $.extend( $.layerSettings, settings );
  •         ___settings___[settings.id] = settings;
  •         ___id___ = settings.id;
  •     },
  •     layershow:function(){       
  •         var __bw = $("body").width();
  •         var __bh = $("body").height() > $(window).height() ? $("body").height() : $(window).height();
  •         var _width = $.layerSettings.width;
  •         var _height = $.layerSettings.height;
  •         
  •         if(document.getElementById(___id___)) return;
  •         var _moveid = ___id___ + "_move";
  •         var _titleid = ___id___ + "_title";
  •         var _contentid = ___id___ + "_content";
  •         var _cssurl = $.layerSettings.cssurl;
  •         var opacity = $.layerSettings.opacity;
  •         __index = $.layermaxindex();
  •         var __left = (__bw - _width) > 0 ? (__bw - _width)/2 : 0;
  •         var __top = 100;
  •         var __bgDiv = '+___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;">
  • ';
  •         if($.layerSettings.isbg)
  •         {
  •             $("body").append(__bgDiv);
  •         }
  •         $("body").append('+___id___+'" style="z-index:'+__index+';position:absolute; left:'+__left+'px; top:'+__top+'px;">
  • ');
  •         var _templete = $.layerSettings.templete;
  •         var __templete = _templete.replaceAll("@width@",_width).replaceAll("@height@",_height).replaceAll("@titleid@",_titleid).replaceAll("@contentid@",_contentid).replaceAll("@title@",jQuery.layerSettings.title).replaceAll("@moveid@",_moveid);
  •         $("#"+___id___).append(__templete);
  •         $("#"+_contentid).append($.layerSettings.content);
  •         $("#"+_titleid).append($.layerSettings.title);
  •         var idd = ___id___;
  •         $(".layerclose").bind("click",function()
  •         {
  •             $.layerclose(idd);
  •         });
  •         $("#"+___id___).bind("click",function()
  •          {
  •              var id = this.id;
  •              $.layerSetup(___settings___[id]);
  •             $(this).css("z-index",$.layermaxindex()); 
  •          });
  •         $(document).bind("click",function(e)
  •          {
  •             var pos = $.getMousePosition(e);
  •             
  •          });
  •         $(document).mousemove(function(e){                                          
  •             if(isMouseDown && dragStatus[currentElement.id] != 'false'){
  •                 $.updatePosition(e);
  •                 if(dragCallbacks[currentElement.id] != undefined){
  •                     dragCallbacks[currentElement.id](e, currentElement);
  •                 }
  •                 return false;
  •             }
  •         });
  •         $(document).mouseup(function(e){
  •             if(isMouseDown && dragStatus[currentElement.id] != 'false'){
  •                 isMouseDown = false;
  •                 if(dropCallbacks[currentElement.id] != undefined){
  •                     dropCallbacks[currentElement.id](e, currentElement);
  •                 }
  •                 return false;
  •             }
  •         });
  •         (function(){
  •             bubblings[___id___] = true;
  •             dragStatus[___id___] = "on";
  •             
  •             //setHandler
  •             bubblings[this.id] = true;
  •             
  •             dragStatus[_moveid] = "handler";
  •             $("#"+_moveid).css("cursor""move");   
  •             $("#"+_moveid).mousedown(function(e){
  •                 var id = this.id.replace("_move","");
  •                 ___id___ = id;
  •                 $("#"+id).css("z-index",$.layermaxindex());
  •                 $.layerSetup(___settings___[id]);
  •                 if((dragStatus[___id___] == "off") || (dragStatus[___id___] == "handler" && !holdingHandler))
  •                     return bubblings["#"+___id___];
  •                 
  •                 isMouseDown    = true;
  •                 currentElement = $("#"+___id___);
  •                 var pos    = $.getMousePosition(e);
  •                 lastMouseX = pos.x;
  •                 lastMouseY = pos.y;
  •                 lastElemTop  = document.getElementById(___id___).offsetTop;
  •                 lastElemLeft = document.getElementById(___id___).offsetLeft;
  •                 $.updatePosition(e);
  •                 holdingHandler = true;
  •             });
  •             
  •             $("#"+_moveid).mouseup(function(e){
  •                 holdingHandler = false;
  •             });
  •             //end setHandler
  •         })();
  •     },
  •     layerclose:function(__id)
  •     {
  •         $("#"+__id+"_background").remove();
  •         $("#"+__id).remove();
  •     },
  •     layermaxindex:function()
  •     {
  •         var ___index = 0;
  •         $.each($("*"),function(i,n){
  •              var __tem = $(n).css("z-index");
  •              if(__tem>0)
  •              {
  •                 if(__tem > ___index)
  •                 {
  •                     ___index = __tem + 1;   
  •                 }
  •              }
  •          });
  •         return ___index;
  •     }
  • });
  • })(jQuery);
  •  

    使用方法:

    (1)显示层:

    1. function show()
    2. {
    3. $.layerSetup({
    4.                  id:"abc",//弹出层的ID
    5.                  title:"test",//标题
    6.                  content:'test',//内容
    7.                  isbg:false,//是否显示背景遮照层
    8.                  opacity:0.3,//背景遮照层的透明度,值越大透明度越低,0为完全透明,1为不透明
    9.                  templete:'
    '//模板
  •                  });
  • $.layershow();
  • }
  • (2)关闭层:

     

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

     

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

    你可能感兴趣的:(JAVASCRIPT)