得到一个div,在JS中怎么设置让他始终浮动在屏幕最上方,有滚动条啊

解决方案 »

  1.   

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="content-type" content="text/html; charset=gb2312">
    <title> new document </title>
    <meta name="keywords" content="">
    <meta name="description" content="">
    </head> <body>
    <div id="" style="height:1500px;"></div>
    <div id="kk" style="border:1px solid #f00;width:200px; height:35px; top:0px; position:absolute;">QQ在线</div>
    <script type="text/javascript">
    <!--
    function newtoponload() {
    var c = document.getElementById("kk");
    function b() {
    var a = document.documentElement.scrollTop || window.pageYOffset || document.body.scrollTop;
    setTimeout(function(){c.style.top = (0 + a) + "px"},200);
    } window.onscroll = b;
    b()
    }
    if (window.attachEvent) {
    window.attachEvent("onload", newtoponload)
    } else {
    window.addEventListener("load", newtoponload, false)
    }
    //-->
    </script>
    </body>
    </html>
      

  2.   


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>无标题页</title>
        <style>
        #head{position:fixed;_position:absolute;top:0px;_top:0px;_margin-top:expression(this.style.pixelHeight+document.documentElement.scrollTop)}
        </style>
    </head>
    <body style="height:1000px;">
        <div id="head" >随意滚动</div>
    </body>
    </html>
      

  3.   

    谢谢大虾们,可是我只能控制js,不能写在html中写,我是问用js怎么写,我现在能得到div元素,$("div"); 然后用js怎么把那些css样式加上
      

  4.   

    document.getElementById("mydiv").className="head";
    #head 改为 .head