Dakular's profile打哭了的拨烙锅PhotosBlogLists Tools Help

Blog


    10/30/2006

    JS取clientHeight与scrollTop

    先来段数据,下表的值为document.body.clientHeight / document.documentElement.clientHeight

                 IE               FF
    Html       608/0          630/11096
    Xhtml     10942/591    11076/630

    在html/xhtml与ie/ff这4种排列组合下,取得的clientHeight几乎没有一样的,可见写一段兼容3种浏览器两种页面标准的js脚本有多头疼。

    暂时总结的判断方法如下:
    var h1 = document.body.clientHeight;
    var h2 = document.documentElement.clientHeight;
    var isXhtml = (h2<=h1&&h2!=0)?true:false; //判断当前页面的Doctype是否为Xhtml
    var body = isXhtml?document.documentElement:document.body;
    alert(body.clientHeight); //最终结果比较一致

    安全的取到scrollTop:
    document.body.scrollTop + document.documentElement.scrollTop

    判断浏览器的类型,这种写法挺喜欢的:
    var ua = navigator.userAgent.toLowerCase ();
    var os = new Object();
    os.isFirefox = ua.indexOf ("gecko") != -1;
    os.isOpera = ua.indexOf ("opera") != -1;
    os.isIE = !os.isOpera && ua.indexOf ("msie") != -1;

    Comments (7)

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.

    To add a comment, sign in with your Windows Live ID (if you use Hotmail, Messenger, or Xbox LIVE, you have a Windows Live ID). Sign in


    Don't have a Windows Live ID? Sign up

    Sept. 22
    No namewrote:
    <a href="http://www.topowerleveling.com">world of warcraft power leveling</a> <a href="http://www.topowerleveling.com">wow power leveling</a> <a href="http://www.topowerleveling.com">power leveling</a> <a href="http://www.gowowpowerleveling.com">wow gold</a> <a href="http://www.cnaosheng.com.cn">橡塑发泡</a> <a href="http://www.ac021.com">直流电源</a> <a href="http://www.paper-cup-machine.com">paper cup machine</a> <a href="http://www.liandaauto.com/main.asp">Mass air flow</a> <a href="http://www.pabx365.com">程控交换机</a> <a href="http://www.mystery.net.cn/index/index.php">餐饮软件</a> <a href="http://www.mystery.net.cn">餐饮软件</a> <a href="http://www.mystery.net.cn">收银机</a> <a href="http://www.zhongke-china.com/product.asp">automatic rigid box line</a> <a href="http://www.zhongke-china.com/product.asp">paper converting and wrapping machines</a> <a href="http://www.zhongke-china.com/product.asp">automatic box makers</a> <a href="http://www.zhongke-china.com/about.asp">rigid set-up gift box</a>,<a href="http://www.zhongke-china.com/about.asp">paperboard converting equipment</a>


    <a href="http://www.lszwjx.com">气动马达</a> <a href="http://www.lszwjx.com">气动搅拌机</a> <a href="http://www.lszwjx.com">防爆马达</a> <a href="http://www.lszwjx.com/about.htm">防爆搅拌机</a> <a href="http://www.lszwjx.com/about.htm">气动搅拌机</a> <a href="http://www.lszwjx.com/news.htm">空气马达</a> <a href="http://www.lszwjx.com/news.htm">防爆马达</a> <a href="http://www.lszwjx.com/product.htm">气动马达</a> <a href="http://www.lszwjx.com/product2.htm">油漆搅拌机</a> <a href="http://www.lszwjx.com/plist493.htm">油漆搅拌器</a> <a href="http://www.lszwjx.com/product2.htm">气动搅拌器</a> <a href="http://www.lszwjx.com/plist490.htm">防爆搅拌器</a> <a href="http://www.lszwjx.com/product2.htm">油墨搅拌器</a> <a href="http://www.lszwjx.com/plist495.htm">油墨搅拌机</a> <a href="http://www.ruian2machine.cn">安检门</a> <a href="http://www.ruian2machine.cn">金属探测器</a> <a href="http://www.ruian2machine.cn">金属探测仪</a> <a href="http://www.plastic-thermoforming-machine.com/product.htm">thermoforming Equipment</a> <a href="http://www.packagemachinery.cn">bag making machine</a> <a href="http://www.todesign.com.cn">工业设计</a> <a href="http://www.todesign.com.cn">产品设计</a> <a href="http://www.tzonegroup.cn">储罐</a> <a href="http://www.tzonegroup.cn/about.asp">中药提取设备</a> <a href="http://www.tzonegroup.cn/products.asp">乳化机</a> <a href="http://www.rajayj.cn">反应釜</a> <a href="http://www.rajayj.cn">真空干燥箱</a> <a href="http://www.ashuashi.com.cn">厚壁钢管</a>


    <a href="http://www.gowowpowerleveling.com">power leveling</a> <a href="http://www.gowowpowerleveling.com">wow power leveling</a> <a href="http://www.zhongke-china.com">paper box</a> <a href="http://www.zhongke-china.com/about.asp">paper box equipment</a> <a href="http://www.plastic-thermoforming-machine.com">thermoforming Equipment</a> <a href="http://www.plastic-thermoforming-machine.com">Plastic Machinery</a> <a href="http://www.plastic-thermoforming-machine.com/about.htm">Plastic Machine</a> <a href="http://www.wzbtjx.cn">马达</a> <a href="http://www.wzbtjx.cn">气动马达</a> <a href="http://www.wzbtjx.cn">搅拌机</a>
    Aug. 19
    July 11
    No namewrote:
    天.灭.中.共,天.佑.中.华。全.球.退.党.热.线.电.话:001-416-361-9895 邮箱:tuidang@epochtimes.com (请使用海外邮箱和破网软件配合发送.三.退.声明) 请访问 天佑中华 获取破网软件。
    July 28
    No namewrote:
    读《.九.评.共.产.党》 做.三.退.保.平.安 全.球.退.党.热.线.电.话:001-416-361-9895 邮箱:tuidang@epochtimes.com (请使用海外邮箱和破网软件配合发送.三.退.声明)更多请访问 美好未来 获取破网软件
    July 2
    May 9
    Picture of Anonymous
    (没有名称) wrote:
    maybe you can try these 2 functions
     
    //
    // getPageScroll()
    // Returns array with x,y page scroll values.
    // Core code from - quirksmode.org
    //
    function getPageScroll(){
     var yScroll;
     if (self.pageYOffset) {
      yScroll = self.pageYOffset;
     } else if (document.documentElement && document.documentElement.scrollTop){  // Explorer 6 Strict
      yScroll = document.documentElement.scrollTop;
     } else if (document.body) {// all other Explorers
      yScroll = document.body.scrollTop;
     }
     arrayPageScroll = new Array('',yScroll)
     return arrayPageScroll;
    }
     
    //
    // getPageSize()
    // Returns array with page width, height and window width, height
    // Core code from - quirksmode.org
    // Edit for Firefox by pHaez
    //
    function getPageSize(){
     
     var xScroll, yScroll;
     
     if (window.innerHeight && window.scrollMaxY) { 
      xScroll = document.body.scrollWidth;
      yScroll = window.innerHeight + window.scrollMaxY;
     } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
      xScroll = document.body.scrollWidth;
      yScroll = document.body.scrollHeight;
     } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
      xScroll = document.body.offsetWidth;
      yScroll = document.body.offsetHeight;
     }
     
     var windowWidth, windowHeight;
     if (self.innerHeight) { // all except Explorer
      windowWidth = self.innerWidth;
      windowHeight = self.innerHeight;
     } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
      windowWidth = document.documentElement.clientWidth;
      windowHeight = document.documentElement.clientHeight;
     } else if (document.body) { // other Explorers
      windowWidth = document.body.clientWidth;
      windowHeight = document.body.clientHeight;
     } 
     
     // for small pages with total height less then height of the viewport
     if(yScroll < windowHeight){
      pageHeight = windowHeight;
     } else {
      pageHeight = yScroll;
     }
     // for small pages with total width less then width of the viewport
     if(xScroll < windowWidth){ 
      pageWidth = windowWidth;
     } else {
      pageWidth = xScroll;
     }

     arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
     return arrayPageSize;
    }
    Nov. 22

    Trackbacks (1)

    The trackback URL for this entry is:
    http://dakular.spaces.live.com/blog/cns!8668FFEC49BC51CE!440.trak
    Weblogs that reference this entry