日期:2014-05-16  浏览次数:20413 次

求自动跟随DIV js
像淘宝里 商品详情 那条选项卡超过了高度都自动跟随的代码


效果如图
效果地址:http://detail.tmall.com/item.htm?spm=a2106.m895.1000384.d11.Jjhd6L&id=20503104378&source=dou&scm=1029.newlist-0.bts7.50011131&ppath=

谢谢

------解决方案--------------------
<style type="text/css">
    #scs{
        border: 1px solid red;
        height:20px;
        font:12px/25px "宋体";
        width:500px;
    }
</style>
<div style="height:200px"></div>
<div id="scs">这是固定的</div>
<div style="height:2000px"></div>
<script type="text/javascript">
    window.onload=function(){
        var scs=document.getElementById("scs");//获取对象
        var t=scs.offsetTop;//获得本身到网页顶部的距离
        var isIE6 = !-[1,] && !window.XMLHttpRequest;//判断ie6
        var i=0;
        window.onscroll=function(){
            i=document.documentElement.scrollTop;
            if(i>t){
                //网页被卷去的高大于本身到网页顶部的距离(简单理解就是对象开始要被卷到网页顶部不可视区域)
                if(isIE6){
                    scs.style.position="absolute";
                    scs.style.top=i+"px";
                }else{
                    scs.style.position="fixed";
                    scs.style.top=0;
                }
            }else{
                scs.style.position="static";
                scs.style.top="auto";
            }
        }
    }
</script>