日期:2014-05-16 浏览次数:20323 次
function getScrollTop () {
var scrollPos;
if (window.pageYOffset) {
scrollPos = window.pageYOffset;
}
else if (document.compatMode && document.compatMode != 'BackCompat') {
scrollPos = document.documentElement.scrollTop;
}
else if (document.body) {
scrollPos = document.body.scrollTop;
}
return scrollPos;
}
/**
* 设置元素的左偏移量
* type function
* @param {Object} element 对应的元素
*/
function getElementLeft(element){
var actualLeft = element.offsetLeft;
var current = element.offsetParent;
while (current != null){
actualLeft += current.offsetLeft;
current = current.offsetParent;
}
return actualLeft;
}
/**
* 设置元素的上偏移量
* type function
* @param {Object} element 对应的元素
*/
function getElementTop(element){
var actualTop = element.offsetTop;
var current = element.offsetParent;
while (current != null){
actualTop += current.offsetTop;
current = current.offsetParent;
}
return actualTop;
}
/**
* 得到元素的rect
* type function
* @param {Object} element 对应的元素
*/
function getBoundingClientRect(element){
var scrollTop = document.body.scrollTop
------解决方案--------------------
document.documentElement.scrollTop;
var scrollLeft = document.body.scrollLeft
------解决方案--------------------
document.documentElement.scrollLeft;
//判断支不支持getBoundingClientRect方法
if(element.getBoundingClientRect()){
//去除不同浏览器的2px的bug
if(typeof arguments.callee.offset != "number"){
var temp = document.createElement("div");
temp