Home  >  Article  >  Web Front-end  >  Jquery author John Resig's own encapsulated javascript common functions_javascript skills

Jquery author John Resig's own encapsulated javascript common functions_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:42:08830browse
Copy code The code is as follows:

//Get the style value of the 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
}
}
//Get the x and y coordinates of the element relative to this 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;
}
//Get 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);
}
//Get the x and y coordinates of the element positioned 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";
}
//Increase the X and y coordinates of the element.
function addX(elem,pos){
set(elem,(posX(elem) pos));
}
function addY(elem,pos){
set(elem,( posY(elem) pos));
}
//Get the height and width of the element using css to control the size
function getHeight(elem){
return parseInt(getStyle(elem,"height") );
}
function getWidth(elem){
return parseInt(getStyle(elem,"width"));
}
//Get element possible, complete height and width
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];
}
}
//Show 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*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);
}
}
}
//Get the mouse cursor relative position on 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;
}
//Get the position of the mouse cursor 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;
}
//Get the height and width of the page
function getPageHeight(){
var de=document.documentElement;
return document.body.scrollHeight|| (de&&de.scrollHeight);
}
function getPageWidth(){
var de=document.documentElement;
return document.body.scrollWidth||(de&&de.scrollWidth);
}
//Get the position of the scroll bar.
function scrollX(){
var de=document.documentElement;
return self.pageXOffset||(de&&de.scrollLeft)||document.body.scrollLeft;
}
function scrollY(){
var de=document.documentElement;
return self.pageYOffset||(de&&de.scrollTop)||document.body.scrollTop;
}
//获取视口的高度和宽度。
function windowHeight() {
var de = document.documentElement;
return self.innerHeight||(de && de.offsetHeight)||document.body.offsetHeight;
}
function windowWidth() {
var de = document.documentElement;
return self.innerWidth||( de && de.offsetWidth )||document.body.offsetWidth;
}
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn