在子鼠的博客看到了2个js的例子,正好是我一直搞不清的height系列。。
2篇博客分别是《让一行页脚保持在底部》《右下角广告代码-已测-支持FIREFOX IE7 IE6》

我对里面代码根据自己的理解添加了注释。我觉得还是蛮口语化的,大家随便看看。

这是《让一行页脚保持在底部》的注释,效果可以点此

  1. function test(){
  2.  var infoHeight = document.getElementById("info").scrollHeight;
  3.  //scrollHeight 为层实际的高度,有实际有多高就多高,与当前网页高度无关。
  4.  var bottomHeight = document.getElementById("bottom").scrollHeight;
  5.  var allHeight = document.documentElement.clientHeight;
  6.  //clientHeight 为层在当前屏幕的高度,非该层的实际高度
  7.  
  8.  var bottom = document.getElementById("bottom");
  9.  //获取bottom
  10.  if((infoHeight + bottomHeight) < allHeight){ //如果"info层的高度"加"bottom层的高度"小于"当前屏幕的高度"
  11.   bottom.style.position = "absolute"; //定义bottom的position为absolute
  12.   bottom.style.bottom = "0"; //bottom属性设为0.
  13.  }else{
  14.   bottom.style.position = "";
  15.   bottom.style.bottom = "";
  16.  }
  17.  setTimeout(function(){test();},10); //当内容减少,窗口缩小,依然保持在底部
  18. }
  19. test();

《右下角广告代码-已测-支持FIREFOX IE7 IE6》的注释,效果可以点此

  1. //<div style="position:absolute;border:1px solid #000;right:0;height:20px;" id="ad"><a href="http://www.zishu.cn">子鼠测试.</a></div> <!--定义需要测试的层的css。position:absolute;是关键-->
  2. function rightBottomAd(){
  3.     var abc = document.getElementById("ad"); //获取ad的值并赋给变量abc
  4.     abc.style.top = document.documentElement.scrollTop+document.documentElement.clientHeight-20+"px";
  5.     // document.documentElement.scrollTop 是获取当前页面的滚动条纵坐标位置,注意不是document.top.scrollTop
  6.     // document.documentElement.clientHeight 获取屏幕的实际大小
  7.     // 整句就是"滚动条纵坐标"加"当前屏幕的坐标"减"20"(20对应上面的层的高度),这样就是可以让层永远处于当前页面的最下端。可以通过alert(document.documentElement.scrollTop)和alert(document.documentElement.clientHeight)加深理解。
  8.     setTimeout(function(){rightBottomAd();},50); //不断的计算层的位置。
  9. }
  10. rightBottomAd();


无觅相关文章插件,快速提升流量