JQuery pop-up layer plug-in simplified version code download
The Code is as follows:
String. prototype. replaceAll = function (s1, s2 ){
Return this. replace (new RegExp (s1, "gm"), s2 );
};
(Function ($ ){
/*
* $-Layer 0.1-New Wave Javascript
*
* Copyright (c) 2008 King Wong
* $ Date: 2008-10-09 $
*/
Var ___ id ___ = "";
Var ___ settings ___ = {};
Var isMouseDown = false;
Var currentElement = null;
Var dropCallbacks = {};
Var dragCallbacks = {};
Var bubblings = {};
Var lastMouseX;
Var lastMouseY;
Var lastElemTop;
Var lastElemLeft;
Var dragStatus = {};
Var holdingHandler = false;
$. GetMousePosition = function (e ){
Var posx = 0;
Var posy = 0;
If (! E) var e = window. event;
If (e. pageX | e. pageY ){
Posx = e. pageX;
Posy = e. pageY;
}
Else if (e. clientX | e. clientY ){
Posx = e. clientX + document. body. scrollLeft + document.doc umentElement. scrollLeft;
Posy = e. clientY + document. body. scrollTop + document.doc umentElement. scrollTop;
}
Return {'X': posx, 'y': posy };
};
$. UpdatePosition = function (e ){
Var pos = $. getMousePosition (e );
Var spanX = (pos. x-lastMouseX );
Var spanY = (pos. y-lastMouseY );
Var _ top = (lastElemTop + spanY)> 0? (LastElemTop + spanY): 0;
Var _ left = (lastElemLeft + spanX)> 0? (LastElemLeft + spanX): 0;
$ ("#" +___Id___).css ("top", _ top );
$ ("#" +___Id1__).css ("left", _ left );
};
$. Fn. ondrag = function (callback ){
Return this. each (function (){
DragCallbacks [this. id] = callback;
});
};
$. Fn. ondrop = function (callback ){
Return this. each (function (){
DropCallbacks [this. id] = callback;
});
};
$. Fn. dragOff = function (){
Return this. each (function (){
DragStatus [this. id] = 'off ';
});
};
$. Fn. dragOn = function (){
Return this. each (function (){
DragStatus [this. id] = 'on ';
});
};
$. Extend ({
LayerSettings :{
Id: "layerp ",
Width: 220,
Height: 220,
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 = pai.layersettings.css url;
Var opacity = $. layerSettings. opacity;
_ Index = $. layermaxindex ();
Var _ left = (_ bw-_ width)> 0? (_ Bw-_ width)/2: 0;
Var _ top = 100;
Var _ bgDiv ='
';
If ($. layerSettings. isbg)
{
$ ("Body"). append (_ bgDiv );
}
$ ("Body"). append ('
');
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 );
Usage:
(1) display layer:
The Code is as follows:
Function show ()
{
$. LayerSetup ({
Id: "abc", // the ID of the pop-up layer
Title: "test", // title
Content: 'test', // content
Isbg: false, // whether to display the background shadow Layer
Opacity: 0.3, // the transparency of the background shadow layer. The greater the value, the lower the transparency. 0 indicates completely transparent, and 1 indicates opacity.
Templete :'
'// Template
});
$. Layershow ();
}
(2) Close the layer:
The Code is as follows:
$. Layerclose ("pop-up layer ID ");
NOTE: If your template has a CSS file, you need to directly introduce it to this page. This simplified version cannot be displayed on other frameworks, so there is no such cssurl attribute and no target attribute.
Source code download http://xiazai.jb51.net/jslib/jquery-layer.rar