头部跟随浏览器顶端示例

javascript

2015-04-13 15:02

<style type="text/css">
.asd
{
left: 0px; top: 0px; position: fixed;
}
</style>
<body>
<script type="text/javascript">
window.onscroll = function(){
    var t = document.documentElement.scrollTop || document.body.scrollTop;
    var top_div = document.getElementById( "ttt" );
    if( t >= 300 ) {
        top_div.className='asd';
    } else {
         top_div.className='';
    }
}

</script>
<div style="width:800px; height:300px; background-color:#FC3;"></div>
<div style="height:40xp; width:500px; background-color:#FCC;" id="ttt">1111111111111</div>
<table width="200" border="1">
  <tr>
    <td height="2000">&nbsp;</td>
  </tr>
</table>