javascript 自定义常用函数
1、获取元素的位置
说明:obj为DOM元素对象。返回位置对象(pos)
function getPos(obj) {
var pos = {
"top" : 0,
"left" : 0
};
if (obj.offsetParent) {
while (obj.offsetParent) {
pos.top += obj.offsetTop;
pos.left += obj.offsetLeft;
obj = obj.offsetParent;
}
} else if (obj.x) {
pos.left += obj.x;
} else if (obj.x) {
pos.top += obj.y;
}
return pos;
}
2、获取滚动位置
function getPageScroll(){
var scrollObj = {
"top" : 0,
"left" : 0
};
if (self.pageYOffset) {
scrollObj.top = self.pageYOffset;
scrollObj.left = self.pageXOffset;
} else if (document.documentElement && document.documentElement.scrollTop){
scrollObj.top = document.documentElement.scrollTop;
scrollObj.left = document.documentElement.scrollLeft;
} else if (document.body) {
scrollObj.top = document.body.scrollTop;
scrollObj.left = document.body.scrollLeft;
}
return scrollObj;
}
3、获取窗口尺寸
function getWindowSize()
{
var win = {
"width" : 0,
"height" : 0
};
//获取窗口宽度
if (window.innerWidth)
win.width = window.innerWidth;
else if ((document.body) && (document.body.clientWidth))
win.width = document.body.clientWidth;
//获取窗口高度
if (window.innerHeight)
win.height = window.innerHeight;
else if ((document.body) && (document.body.clientHeight))
win.height= document.body.clientHeight;
//获取窗口大小
if (document.documentElement && document.documentElement.clientHeight && document.documentElement.clientWidth)
{
win.height = document.documentElement.clientHeight;
win.width = document.documentElement.clientWidth;
}
return win;
}
4、触发事件
/**
* 触发事件
*
* @param el 对象
* @param eventName 事件名称
* @return
*/
function touchOffEvent(el, eventName) {
if (document.all) {
el.fireEvent(eventName);
} else {
if ("onchange" == eventName.toLowerCase()) {
el.onchange();
} else if ("onclick" == eventName.toLowerCase()) {
el.onclick();
} else if ("ondblclick" == eventName.toLowerCase()) {
el.ondblclick();
} else if ("onfocus" == eventName.toLowerCase()) {
el.onfocus();
} else if ("onselect" == eventName.toLowerCase()) {
el.onselect();
} else if ("onhelp" == eventName.toLowerCase()) {
el.onhelp();
} else if ("onkeydown" == eventName.toLowerCase()) {
el.onkeydown();
} else if ("onkeypress" == eventName.toLowerCase()) {
el.onkeypress();
} else if ("onkeyup" == eventName.toLowerCase()) {
el.onkeyup();
} else if ("onmousedown" == eventName.toLowerCase()) {
el.onmousedown();
} else if ("onmousemove" == eventName.toLowerCase()) {
el.onmousemove();
} else if ("onmouseout" == eventName.toLowerCase()) {
el.onmouseout();
} else if ("onmouseover" == eventName.toLowerCase()) {
el.onmouseover();
} else if ("onmouseup" == eventName.toLowerCase()) {
el.onmouseup();
} else {
alert("没有"+eventName+"事件!");
}
}
}
5、检查用户使用的浏览器
/**
* 检查用户使用的浏览器
*
* @return
*/
function checkUserAgent() {
if ((navigator.userAgent.indexOf('MSIE') >= 0)
&& (navigator.userAgent.indexOf('Opera') < 0)) {
alert('你是使用IE');
} else if (navigator.userAgent.indexOf('Firefox') >= 0) {
alert('你是使用Firefox');
} else if (navigator.userAgent.indexOf('Opera') >= 0) {
alert('你是使用Opera');
} else {
alert('你是使用其他的浏览器浏览网页!');
}
}
6、去除两端空格
function