请js高手帮助看一下,我这里有一段是js实现元素不随浏览器滚动而改变位置的代码,现在的效果是把元素固定在了浏览器窗口的顶部。我的问题是:如何把它固定在距离窗口顶部30px位置?也就是说,随着浏览器的滚动,元素固定距离顶部30px的位置。$(document).ready(function () {
    $(window).scroll(function () {
        var startScroll = $("#startScroll"), scrollBox = $("#letterList"), offset = startScroll.offset();
        if ($(this).scrollTop() > offset.top) {
            scrollBox.css({ "position": "fixed", "top": "0", "z-index": "1" });
            if (!window.XMLHttpRequest) { scrollBox.css({ "position": "absolute", "top": $(this).scrollTop(), "z-index": "1" }); }
        } else { scrollBox.css({ "position": "static", "top": "0", "z-index": "1" }); }
    });
});
javascript

解决方案 »

  1.   

    scrollBox.css({ "position": "fixed", "top": "0", "z-index": "1" });
    改成
    scrollBox.css({ "position": "fixed", "top": "30px", "z-index": "1" });
    试试可以不
      

  2.   

    $(window).scroll(function () {
              $("#letterList").css({ "position": "absolute", "top": $(this).scrollTop()+30, "z-index": "1" });        
        });
      

  3.   

    <!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>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无标题文档</title>
    <style type="text/css">
    #test{
    width:100px;
    height:400px;
    background-color:#F00;
    }
    </style>
    <script type="text/javascript" src="jquery-1.8.3.js"></script>
    <script type="text/javascript">
    $(function(){
    $('#test').css({ "position": "fixed", "top": "30px", "z-index": "1"});
    })
    </script>
    </head><body>
    <div id="test"></div>
    <img src="u=3377829953,1380725767&amp;fm=25&amp;gp=0.jpg"><br/>
    <img src="u=3377829953,1380725767&amp;fm=25&amp;gp=0.jpg"><br/>
    <img src="u=3377829953,1380725767&amp;fm=25&amp;gp=0.jpg"><br/>
    <img src="u=3377829953,1380725767&amp;fm=25&amp;gp=0.jpg"><br/>
    <img src="u=3377829953,1380725767&amp;fm=25&amp;gp=0.jpg"><br/>
    <img src="u=3377829953,1380725767&amp;fm=25&amp;gp=0.jpg"><br/>
    <img src="u=3377829953,1380725767&amp;fm=25&amp;gp=0.jpg"><br/>
    <img src="u=3377829953,1380725767&amp;fm=25&amp;gp=0.jpg"><br/>
    </body>
    </html>
    你看下是不是你哪个判断条件没满足啊  我试了下可以啊