Jquery author JohnResig self-encapsulated javascript common function _ javascript skills

Source: Internet
Author: User
Jquery author JohnResig encapsulated common functions. For details, refer. The Code is as follows:


// Obtain the style value of an element.
Function getStyle (elem, name ){
If (elem. style [name]) {
Return elem. style [name];
} Else if (elem. currentStyle ){
Return elem. currentStyle [name];
} Else if (document. defaultView & document. defaultView. getComputedStyle ){
Name = name. replace (/([A-Z])/g, "-$1 ″);
Name = name. toLowerCase ();
Var s = document. defaultView. getComputedStyle (elem ,"");
Return s & s. getPropertyValue (name );
} Else {
Return null
}
}
// Obtain the x and y coordinates of the element relative to the page.
Function pageX (elem ){
Return elem. offsetParent? (Elem. offsetLeft + pageX (elem. offsetParent): elem. offsetLeft;
}
Function pageY (elem ){
Return elem. offsetParent? (Elem. offsetTop + pageY (elem. offsetParent): elem. offsetTop;
}
// Obtain the x and y coordinates of the element relative to the parent element.
Function parentX (elem ){
Return elem. parentNode = elem. offsetParent? Elem. offsetLeft: pageX (elem)-pageX (elem. parentNode );
}
Function parentY (elem ){
Return elem. parentNode = elem. offsetParent? Elem. offsetTop: pageY (elem)-pageY (elem. parentNode );
}
// Obtain the x and y coordinates of the elements located using css.
Function posX (elem ){
Return parseInt (getStyle (elem, "left "));
}
Function posY (elem ){
Return parseInt (getStyle (elem, "top "));
}
// Set the element position.
Function setX (elem, pos ){
Elem. style. left = pos + "px ";
}
Function setY (elem, pos ){
Elem. style. top = pos + "px ";
}
// Add the X and y coordinates of the elements.
Function addX (elem, pos ){
Set (elem, (posX (elem) + pos ));
}
Function addY (elem, pos ){
Set (elem, (posY (elem) + pos ));
}
// Obtain the height and width of the element controlled by css.
Function getHeight (elem ){
Return parseInt (getStyle (elem, "height "));
}
Function getWidth (elem ){
Return parseInt (getStyle (elem, "width "));
}
// Obtain the possible height and width of the element.
Function getFullHeight (elem ){
If (getStyle (elem, "display ")! = "None "){
Return getHeight (elem) | elem. offsetHeight;
} Else {
Var old = resetCss (elem, {display: "block", visibility: "hidden", position: "absolute "});
Var h = elem. clientHeight | getHeight (elem );
RestoreCss (elem, old );
Return h;
}
}
Function getFullWidth (elem ){
If (getStyle (elem, "display ")! = "None "){
Return getWidth (elem) | elem. offsetWidth;
} Else {
Var old = resetCss (elem, {display: "block", visibility: "hidden", position: "absolute "});
Var w = elem. clientWidth | getWidth (elem );
RestoreCss (elem, old );
Return w;
}
}
// Set css and save the old css
Function resetCss (elem, prop ){
Var old = {};
For (var I in prop ){
Old [I] = elem. style [I];
Elem. style [I] = prop [I];
}
Return old;
}
Function restoreCss (elem, prop ){
For (var I in prop ){
Elem. style [I] = prop [I];
}
}
// Display and hide
Function show (elem ){
Elem. style. display = elem. $ oldDisplay | "";
}
Function hide (elem ){
Var curDisplay = getStyle (elem, "display ");
If (curDisplay! = "None "){
Elem. $ oldDisplay = curDisplay;
Elem. style. display = "none ";
}
}
// Set transparency
Function setOpacity (elem, num ){
If (elem. filters ){
Elem. style. filter = "alpha (opacity =" + num + ")";
} Else {
Elem. style. opacity = num/100;
}
}
// Slide
Function slideDown (elem ){
Var h = getFullHeight (elem );
Elem. style. height = "0px ";
Show (elem );
For (var I = 0; I <= 100; I + = 5 ){
New function (){
Var pos = I;
SetTimeout (function () {elem. style. height = (pos/100 x h) + "px" ;}, (pos * 10 ));
}
}
}
// Gradient
Function fadeIn (elem ){
Show (elem );
SetOpacity (elem, 0 );
For (var I = 0; I <= 100; I + = 5 ){
New function (){
Var pos = I;
SetTimeout (function () {setOpacity (elem, pos) ;}, (pos + 1) * 10 );
}
}
}
// Obtain the cursor position relative to the entire page.
Function getX (e ){
E = e | window. event;
Return e. pageX | e. clientX + document. body. scrollLeft;
}
Function getY (e ){
E = e | window. event;
Return e. pageY | e. clientY + document. body. scrollTop;
}
// Obtain the cursor position relative to the current element.
Function getElementX (e ){
Return (e & e. layerX) | window. event. offsetX;
}
Function getElementY (e ){
Return (e & e. layerY) | window. event. offsetY;
}
// Obtain the page height and width
Function getPageHeight (){
Var demo-document.doc umentElement;
Return document. body. scrollHeight | (de & de. scrollHeight );
}
Function getPageWidth (){
Var demo-document.doc umentElement;
Return document. body. scrollWidth | (de & de. scrollWidth );
}
// Obtain the position of the scroll bar.
Function scrollX (){
Var demo-document.doc umentElement;
Return self. pageXOffset | (de & de. scrollLeft) | document. body. scrollLeft;
}
Function scrollY (){
Var demo-document.doc umentElement;
Return self. pageYOffset | (de & de. scrollTop) | document. body. scrollTop;
}
// Obtain the height and width of the view.
Function compute wheight (){
Var de = document.doc umentElement;
Return self. innerHeight | (de & de. offsetHeight) | document. body. offsetHeight;
}
Function compute wwidth (){
Var de = document.doc umentElement;
Return self. innerWidth | (de & de. offsetWidth) | document. body. offsetWidth;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.