带有动态内容问题的JQuery Sticky Headers

时间:2015-08-18 18:26:16

标签: javascript jquery html css sticky

过去几天坚持这个问题。以为我尝试了一切。希望我能从某人那里得到一些解脱。

如果内容是静态的,则粘性标题可用。当另一个类别在视图中时,标题滚动显示和视图的展开。当HTML中加载了JQuery的动态数据时,会出现问题。此时,粘性标头仍然使用先前标头的旧位置并显示粘性标头。

因此,自从上一类别被扩展以来,不在类别的新位置显示粘性标题,而是将粘性标题显示在旧位置,就好像该类别未展开一样,从而产生不想要的效果。

认为解决方案很容易识别和捕获followmebar类的新位置并正确显示粘性标题。

提前感谢您的协助。

我使用JQuery代码粘贴标头:

var stickyHeaders =  function() {

                     console.log("enter stickyHeaders")

      var $window = $(window),
          $stickies;

      var load = function(stickies) {

        console.log("enter stickies")



            //});

        if (typeof stickies === "object" && stickies instanceof jQuery && stickies.length > 0) {

          $stickies = stickies.each(function() {

            //console.log(stickies);

            var $thisSticky = $(this).wrap('<div class="followWrap" />');


            $thisSticky
                .data('originalPosition', $thisSticky.offset().top)
                .data('originalHeight', $thisSticky.outerHeight())
                  .parent()
                  .height($thisSticky.outerHeight());   


          });




        }

         $window.off("scroll.stickies").on("scroll.stickies", function() {

              _whenScrolling();     
          });
      };

      var _whenScrolling = function() {

        console.log("enter _whenScrolling")

        $stickies.each(function(i) {            

          var $thisSticky = $(this),
              $stickyPosition = $thisSticky.data('originalPosition');

          if ($stickyPosition <= $window.scrollTop()) {        

            var $nextSticky = $stickies.eq(i + 1),
                $nextStickyPosition = $nextSticky.data('originalPosition') - $thisSticky.data('originalHeight');

            $thisSticky.addClass("fixed").addClass('show-followMeBar').removeClass('hide-followMeBar');

            if ($nextSticky.length > 0 && $thisSticky.offset().top >= $nextStickyPosition) {

              $thisSticky.addClass("absolute").css("top", $nextStickyPosition);
            }

          } else {

            var $prevSticky = $stickies.eq(i - 1);

            $thisSticky.removeClass("fixed").addClass('hide-followMeBar').removeClass('show-followMeBar');

            if ($prevSticky.length > 0 && $window.scrollTop() <= $thisSticky.data('originalPosition') - $thisSticky.data('originalHeight')) {

              $prevSticky.removeClass("absolute").removeAttr("style");
            }
          }
        });
      };

      return {
        load: load
      };

    }();

    $(function() {


                stickyHeaders.load($(".followMeBar"));  




    });

和CSS:

.show-followMeBar{
            display: block;

        }
        .hide-followMeBar{
            display: none;

        }
        .followMeBar {
  background: #ffffff;
  padding: 20px 0px 0px 20px;
  position: relative;
  z-index: -999;
  /*color: #fff*/;
  font-weight: bold;
  font-size: 16px;
  margin-left: -20px;
  margin-right: -20px;
}
.followMeBar.fixed {
  position: fixed;
  top: 0;
  width: 100%;
  box-sizing: border-box;
  z-index: 0;
}
.followMeBar.fixed.absolute {
  position: absolute;
}

和HTML:

  <div class="followMeBar">
                              One
   </div>

   <div class=" slider single-item division" id="d1">   

    <div class="slick-active">  

        <span class="">One | 13/14</span>
    </div>

    <!--Dynamic Content goes here-->
</div>

<div class="followMeBar">
                        Two
</div>

<div class=" slider single-item division" id="d1">  

    <div class="slick-active">  

        <span class="">Two | 13/14</span>
    </div>

    <!--Dynamic Content goes here-->
</div>

1 个答案:

答案 0 :(得分:0)

$('.followMeBar').css('transform','none');
$('.followMeBar').removeData( "originalPosition" );
$('.followMeBar').removeData( "originalHeight" );
$(window).scrollTop(0);
stickyHeaders.load($(".followMeBar"));