首页 > 其他 > 详细

各种滚动条的获取

时间:2015-02-04 02:19:49      阅读:278      评论:0      收藏:0      [点我收藏+]

使用javascript如何获取页面滚动条呢?

(1)获取页面滚动条

// Cross browser gets the position of scroll
com.whuang.hsj.getScroll=function(){
    return {
        top:document.documentElement.scrollTop || document.body.scrollTop,
        left:document.documentElement.scrollLeft || document.body.scrollLeft,
        height:document.documentElement.scrollHeight ||document.body.scrollHeight
    }
}

?

(2)获取iframe滚动条

/***
* get iframe window‘scroll
*/
com.whuang.hsj.getIframeScroll=function(iframeObj){
	var document22=iframeObj.contentWindow.document;
    return {
        top:document22.documentElement.scrollTop || document22.body.scrollTop,
        left:document22.documentElement.scrollLeft || document22.body.scrollLeft,
        height:document22.documentElement.scrollHeight || document22.body.scrollHeight,
        width:document22.documentElement.scrollWidth || document22.body.scrollWidth
    }
}

?

(3)获取div滚动条

/***
 * Get scroll of div
 * @param divObj
 * @returns {{scrollHeight: (*|number), scrollWidth: (*|number)}}
 */
com.whuang.hsj.getDivScroll=function(divObj){
    if(typeof divObj == ‘string‘){
        divObj=com.whuang.hsj.$$id(divObj);
    }
    return {
        scrollHeight:divObj.scrollHeight,
        scrollWidth:divObj.scrollWidth
    }
}

?

(4)获取div的位置

/***
 * Get the Coordinate/Location of div
 * @param divObj
 * @returns {{width: number, height: number, left: *, top: Window}}
 */
com.whuang.hsj.divCoordinate=function(divObj){
    if(typeof divObj == ‘string‘){
        divObj=com.whuang.hsj.$$id(‘divObj‘);
    }
    return {‘width‘:divObj.offsetWidth,‘height‘:divObj.offsetHeight,
        ‘x‘:divObj.offsetLeft,‘y‘:divObj.offsetTop,
        ‘scrollLeft‘:com.whuang.hsj.getScroll().left,‘scrollTop‘:com.whuang.hsj.getScroll().top};
}

?

参考:利用函数的惰性载入提高javascript代码性能

http://blogread.cn/it/article/7304?f=hot1

?

各种滚动条的获取

原文:http://hw1287789687.iteye.com/blog/2182976

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!