当前位置:编程学习 > JS >>

javascript/jquery实现div始终在页面底部

代码如下

 代码如下 复制代码

<html>
<body>
<div id="divDetail" style="overFlow-y:scroll; width:250px;height: 200px;">
<table style="border:1px solid; ">
<tr><td>id</td><td>name</td><td>age</td><td>
关键的部分部分在这里:div.scrollTop = div.scrollHeight; 滚动看看我是不是在底部哦。
</td></tr>
</table>
</div>
</body>
<script type="text/javascript" defer>
var div = document.getElementById('divDetail');

div.scrollTop = div.scrollHeight;
//alert(div.scrollTop);
</script>
</html>

另一种方法

利用DIV的scrollIntoView方法,将最底端滚动到可视位置 [list=1]

 代码如下 复制代码
<script language="javascript1.2" type="text/javascript">
function onGetMessage(context)
{
msg.innerHTML+=context;
msg_end.scrollIntoView();
}
</script>
<div style="width:500px;overflow:auto">
<div id="msg" style="overflow:hidden;width:480px;"></div>
<div id="msg_end" style="height:0px; overflow:hidden"></div>
</div>


最后一种是兼容大量的浏览的用法,可以随用户滚动而滚动哦。

 代码如下 复制代码

<title>固定在页面底部的滚动公告</title>
    <style type="text/css">
    html,body,div ul{margin:0;padding:0;border:0; font-size:12px}
    #gg{position:fixed;bottom:0;background:#000;width:100%;height:23px;z-index:9999;opacity:.60;filter:alpha(opacity=60);_bottom:auto;_width:100%;_position:absolute;_top:expression(eval(document.documentElement.scrollTop+document.documentElement.clientHeight-this.offsetHeight-(parseInt(this.currentStyle.marginTop,10)||0)-(parseInt(this.currentStyle.marginBottom,10)||0)));}
 #gg a {color:#FFF}
    </style>

<script type="text/javascript">
(function($){
 $.fn.extend({
  Scroll:function(opt,callback){
  if(!opt) var opt={};
  var _this=this.eq(0).find("ul:first");
  var lineH=_this.find("li:first").height(),
  line=opt.line?parseInt(opt.line,10):parseInt(this.height()/lineH,10),
  speed=opt.speed?parseInt(opt.speed,10):7000,//卷动速度,数值越大,速度越慢(毫秒)
  timer=opt.timer?parseInt(opt.timer,10):7000;//滚动的时间间隔(毫秒)
   if(line==0) line=1;
   var upHeight=0-line*lineH;
   scrollUp=function(){
    _this.animate({
    marginTop:upHeight
   },speed,function(){
    for(i=1;i<=line;i++){
     _this.find("li:first").appendTo(_this);
    }
    _this.css({marginTop:0});
   });
  }
  _this.hover(function(){
   clearInterval(timerID);
  },function(){
   timerID=setInterval("scrollUp()",timer);
  }).mouseout();
 }
 })
})(jQuery);
$(document).ready(function(){
 $(".bulletin").Scroll({line:1,speed:1000,timer:5000});//修改此数字调整滚动时间
});
</script>
    </head>
    <body>
<div style="height:3000px"></div>
<div id="gg"><a href="/">固定在页面底部的滚动公告</a></div>
    </body>
    </html>

补充:网页制作,js教程 
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,