0

我正在尝试为我的网站制作一个持久标题。我写了以下不起作用的代码。在进行调试时,我发现它甚至无法读取 div 的正确位置,这就是问题所在。这是代码:

<script>
function UpdateTableHeaders() {

      /*var el             = $("#top_menu", this);
      var offset         = el.offset();
      var scrollTop      = $(window).scrollTop();
      var floatingHeader = $(".floatingHeader", this);


       if (scrollTop > offset.top) {
           floatingHeader.css(
            "visibility", "visible"
           );
       } else {
           floatingHeader.css(
            "visibility", "hidden"
           );      
       }; */
       //following lines is the code I have tried in turns to see if it can actually read the correct position or not
       $("#content").append( "position top:" + $('#top_menu').position().top);
       $("#content").append( "position top:" + $('#top_menu').offset().top);
}

// DOM Ready      
$(function() {

   var clonedHeaderRow;


       clonedHeaderRow = $("#top_menu", this);
       clonedHeaderRow
         .before(clonedHeaderRow.clone())
         .addClass("floatingHeader");



   $(window)
    .scroll(UpdateTableHeaders)
    .trigger("scroll");

});

但是,输出答案似乎不正确。这是我滚动页面时的输出:

position top:101.45001220703125position top:101.41668701171875position top:101.41668701171875position top:101.04998779296875position top:101.16668701171875position top:101.39999389648438position top:100.63333129882812position top:100.98333740234375position top:101.33331298828125position top:100.79998779296875position top:101.21665954589844position top:101.10000610351562position top :100.63333129882812position top:101.05000305175781position top:100.93333435058594position top:101.23333740234375position top:101.41667175292969position top:100.60000610351562position top:100.71665954589844position top:101.18333435058594position top:100.53334045410156position top:101.23333740234375position top:101.35000610351562position top:100.69999694824219position top:101.05000305175781位置顶部:100.51666259765625位置顶部:100.63333129882812位置顶部:100。86666870117188位置顶部:101.33332824707031位置顶部:100.91667175292969

它在 100 左右波动。谁能告诉我哪里出错了?

提前感谢您的帮助!

4

1 回答 1

3

不要这样做。改为在标题上使用 css position : fixed

于 2012-07-10T19:24:47.317 回答