Original jQuery pop up layer plugin to share

  • 2020-05-26 07:47:07
  • OfStack

jquery, IE6\7\8 firefox chrom and other major browsers (absolutely original), but please help me improve my efficiency.


/**
 * jQuery Pop-up layer plug-in 
 * 
 * @author lanhaoooo@163.com
 * @since 2014-08-23
 */
(function($) {
  var methods = {
    o : {
      isIe : !-[ 1, ] || document.documentMode >= 9,// And then judgment IE9 , IE10 the 
      ie6 : !-[ 1, ] && !window.XMLHttpRequest,
      ie9_10 : document.documentMode >= 9,
      bgLayer : "layerModel_mask",
      dataId : "layerModel_main",
      wrapper : "layerModel_wrapper",
      warpperContent : "layerModel_content",
      warpperTitle : "layerModel_title",
      warpperCloseBtn : "layerModel_closeBtn",
      warpperOwnContent : "layerModel_ownContent",
      replaceClose : "replaceClose",
      dragableClass : "dragable",
      defaultWidth : 300
    },
    generateId : function(){
      return "_" + new Date().getTime();
    },
    init : function(data, options) {
      //left and top Please set it when you want to use it center for false That would only take effect if center = true This setting does not take effect  #666,#999,#e5dfda ,#e5e5e5,#ff8800
      var defaults = {center:true,locked:true,fixed:true,drag:true,zIndex:9999,opacity:"0.5",title:" The system prompt ",staySame:false,width:0,height:0,timer:0,bgColor:"#fffaf6",left:350,top:100,head:true,isClose:true,shake:false,
          close : function(){
            return true;
          }
        };
      options = $.extend(defaults, options);
      var s = this;
      var generateId = s.generateId();
      if (typeof data === 'object') {
        data = data instanceof $ ? data : $(data);
        if(options.staySame) {
          data = s.createRender(data, options, generateId).hide();
        } else {
          data = s.createContainer(data, options, generateId).hide();
        }
      } else if (typeof data === 'string' || typeof data === 'number') {
        data = $("<div id='"+s.o.dataId + generateId +"'></div>").html(data).appendTo(document.body).hide();
      } else {
        alert("Layer Error : Unsupporte data type :" + typeof data);
        return;
      }
      if (options.locked && !s.hasBgLayer()) {
        $("<div class='"+s.o.bgLayer+"' id='" + s.o.bgLayer + "'></div>").appendTo(document.body).css({"background" : options.bgColor,"opacity" : options.opacity});
      }
      data.css({"position" : options.fixed ? s.o.ie6 ? "absolute" : "fixed" : "absolute","z-index" : options.zIndex,"left" : options.left,"top" : options.top}).show();
      if (options.center) {
        s.fixLayer(data);
        $(window).bind("resize scroll", function() {
          s.fixLayer(data);
        });
      }
      if (options.drag) {
        s.dragLayer(data, options);
      }
      if(options.shake) {
        s.shakeLayer(data);
      }
      var timeOut;
      clearTimeout(timeOut);
      if(options.timer > 0){
        timeOut = window.setTimeout(function(){
          $("#"+s.o.replaceClose + generateId).trigger("click");
        }, options.timer);
      }
      return data;
    },
    createContainer : function(data, options,generateId) {
      var s = this;
      // if context If it is not defined, it is passed html Otherwise, it will be added in the way of splicing. After closing, it needs to return to the original place 
      var isHtmlSlice = data.context == undefined ? true : false;
      var wrapperHtml = "<div class='"+s.o.wrapper+"' id='"+s.o.wrapper + generateId+"'>"
                + "<div class='"+s.o.warpperContent+"' id='"+s.o.warpperContent + generateId+"'>"
                + "<a class='"+s.o.replaceClose+"' id='"+s.o.replaceClose + generateId+"'></a>";
                if(options.head){
                  wrapperHtml+= "<h4 class='"+s.o.warpperTitle+" "+s.o.dragableClass+"' id='"+s.o.warpperTitle + generateId+"'>";
                  if(options.isClose){
                    wrapperHtml+= "<a href='javascript:void(0);' title=' Shut down ' class='"+s.o.warpperCloseBtn+"' id='"+s.o.warpperCloseBtn + generateId+"'>&times;</a>";
                  }
                  wrapperHtml+= options.title + "</h4>";
                }
                wrapperHtml+= "<div id='"+s.o.warpperOwnContent + generateId+"' class='"+s.o.warpperOwnContent+"'></div>"
                + "</div>"
              + "</div>";
      s.container = $(wrapperHtml);
      s.container.appendTo(document.body);
      data.clone(true).appendTo("#"+s.o.warpperOwnContent + generateId).show().attr('id',data.attr('id') || s.o.dataId + generateId);
      //div Default width is 100% , so it is recommended to set the width of all the pop-up elements, otherwise the width of the pop-up layer is 100%
      var w = $("#"+data.attr('id')).width() || $("#"+s.o.dataId + generateId).width() || s.o.defaultWidth;
      // You specify the height 
      var tempWidth = w;
      if(options.height > 0 ) {
        if(options.width > 0){
          tempWidth = options.width;
          if(options.width <= w){
            $("#"+s.o.warpperOwnContent + generateId).css({"width":options.width,"overflow-x":"auto"});
          } else {
            // If the specified width is greater than the width of the element itself, you need to center the element 
            // Always center the element 
            var xPadding = (options.width - w) / 2 + 8;
            $("#"+s.o.warpperOwnContent + generateId).css({"padding" : "4px " + xPadding + "px"});
          }
        }
        s.container.width(tempWidth + 32);
        $("#"+s.o.warpperContent + generateId).width(tempWidth + 30);
        $("#"+s.o.warpperOwnContent + generateId).css({"height":options.height,"overflow-y":"auto"});
      } else {
        if(options.width > 0) {
          tempWidth = options.width;
          if(options.width <= w) {
            $("#"+s.o.warpperOwnContent + generateId).css({"width":options.width,"overflow-x":"auto"});
          }
        }
        s.container.width(tempWidth + 22);
        $("#"+s.o.warpperContent + generateId).width(tempWidth + 20);
      }
      $("#"+s.o.warpperCloseBtn + generateId).click(function(e) {
        $("#"+s.o.replaceClose + generateId).trigger("click");
      });
      $("#"+s.o.replaceClose + generateId).click(function(e) {
        s.close($("#"+s.o.wrapper + generateId), options, generateId);
        e.stopPropagation();
      });
      if(!isHtmlSlice){
        //  Let the incoming element return to its original place when the dialog box is closed 
        var display = data.css("display");
        var obj = data[0];
        var prev = obj.previousSibling;
        var next = obj.nextSibling;
        var parent = obj.parentNode;
        this["elemBack_" + generateId] = function(){
          if (prev && prev.parentNode) {
            prev.parentNode.insertBefore(obj, prev.nextSibling);
          } else if (next && next.parentNode) {
            next.parentNode.insertBefore(obj, next);
          } else if (parent) {
            parent.appendChild(obj);
          };
          data.css({"display" : display});
        };
      }
      data.detach();
      return s.container;
    },
    createRender : function(data, options,generateId){
      var s = this;
      // if context If it is not defined, it is passed html Otherwise, it will be added in the way of splicing. After closing, it needs to return to the original place 
      var isHtmlSlice = data.context == undefined ? true : false;
      s.container = $("<div class='"+s.o.wrapper+"' style='border:none;' id='"+s.o.wrapper + generateId+"'><a class='"+s.o.replaceClose+"' id='"+s.o.replaceClose + generateId+"'></a></div>");
      s.container.appendTo(document.body);
      data.clone(true).appendTo(s.container).show().attr('id',data.attr('id') || s.o.dataId + generateId);
      $("#"+s.o.replaceClose + generateId).click(function(e) {
        s.close($("#"+s.o.wrapper + generateId), options, generateId);
        e.stopPropagation();
      });
      if(!isHtmlSlice){
        //  Let the incoming element return to its original place when the dialog box is closed 
        var display = data.css("display");
        var obj = data[0];
        var prev = obj.previousSibling;
        var next = obj.nextSibling;
        var parent = obj.parentNode;
        s["elemBack_" + generateId] = function(){
          if (prev && prev.parentNode) {
            prev.parentNode.insertBefore(obj, prev.nextSibling);
          } else if (next && next.parentNode) {
            next.parentNode.insertBefore(obj, next);
          } else if (parent) {
            parent.appendChild(obj);
          };
          data.css({"display" : display});
        };
      }
      data.detach();
      return s.container;
    },
    isLastLayer : function(){
      var s = this;
      return $("." + s.o.wrapper).length <= 0;
    },
    hasBgLayer : function(){
      var s = this;
      return $("." + s.o.bgLayer).length > 0;
    },
    close : function(data, options ,generateId) {
      var s = this;
      // Call the callback function 
      var result = options.close();
      if(result == undefined || result){
        data.remove();
        if(s.isLastLayer()) {
          $("#" + s.o.bgLayer).remove();
        }
      }
      if (s["elemBack_" + generateId]) {
        s["elemBack_" + generateId]();
      };
    },
    closeLayer : function(obj){
      var s = this;
      var $wapper = $(obj).parents("div."+s.o.wrapper);
      $("."+s.o.replaceClose,$wapper).trigger("click");
    },
    fixLayer : function(data) {
      var s = this;
      var T = ($(window).height() - data.innerHeight()) / 2 + (s.o.ie6 ? $(document).scrollTop() : data.scrollTop());
      var L = ($(window).width() - data.width()) / 2 + (s.o.ie6 ? $(document).scrollLeft() : data.scrollLeft());
      data.css({"left" : L,"top" : T});
    },
    dragLayer : function(data, options) {
      var s = this;
      var move = false;//  Mobile tag 
      var x = 0, y = 0;//  The position of the mouse relative to the upper left corner of the control 
      var o = data.find("." + s.o.dragableClass).css({"cursor" : "move"});
      var a = o[0];
      o.mousedown(function(e) {
        //IE9 IE10 Actually the e.button to 0 The acutely 
        var isLeftClick = (s.o.isIe && e.button == 1) || (!s.o.isIe && e.button == 0) || (s.o.ie9_10 && e.button == 0);
        if (isLeftClick) {
          data.fadeTo(20, 0.25);//  Click and drag to display transparently 
          s.o.isIe ? a.setCapture() : window.captureEvents(Event.MOUSEMOVE);
          move = true;
          x = e.pageX - parseInt(data.css("left"));
          y = e.pageY - parseInt(data.css("top"));
          $(document).mousemove(function(e) {
            if (move) {
              var sx = e.pageX - x;//  Calculates the absolute position of the upper left corner of the control based on the mouse position when moving 
              var sy = e.pageY - y;
              data.css({"top" : sy,"left" : sx});
            }
          }).mouseup(function() {
            data.fadeTo("fast", 1);//  After releasing the mouse, stop moving and return to opacity 
            move = false;
            x = 0;
            y = 0;
            s.o.isIe ? a.releaseCapture() : window.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);
          });
        } else {
          return false;
        }
      });
    },
    shakeLayer : function(data){
      var ll = ($(window).width() - data.width()) / 2;  
      var loop = 4;
      for(var i=1; i<=loop; i++){
        data.animate({left:ll - (loop * 10 - 10 * i)},50);  
        data.animate({left:ll + 2*(loop * 10 - 10 * i)},50);  
      }
    }
  };
  $.fn.layerModel = function(options) {
    methods.init(this, options);
  };
  $.fn.close = function() {
    methods.closeLayer(this);
  };
  $.fn.fix = function() {
    var mn = $(this).parents("." + methods.o.wrapper);
    methods.fixLayer($(mn[0]));
  };
})(jQuery);

That's all for this article, I hope you enjoy it.


Related articles: