粘性盒jQuery

时间:2012-12-01 06:40:17

标签: javascript jquery sticky

我有这个代码: http://jsfiddle.net/k56yR/1/

但是当浏览器滚动条停止并开始顶部#sticky颤抖时,任何人都知道如何解决这个问题?

这是jQuery代码:

$(function(){ // document ready
   if (!!$('#sticky').length) { // make sure "#sticky" element exists
      var el = $('#sticky');
      var stickyTop = $('#sticky').offset().top; // returns number
      var footerTop = $('#footer').offset().top; // returns number
      var stickyHeight = $('#sticky').height();
      var limit = footerTop - stickyHeight - 20;
      $(window).scroll(function(){ // scroll event
          var windowTop = $(window).scrollTop(); // returns number

          if (stickyTop < windowTop){
             el.css({ position: 'fixed', top: 0 });
          }
          else {
             el.css('position','static');
          }

          if (limit < windowTop) {
          var diff = limit - windowTop;
          el.css({top: diff});
          }     
        });
   }
});​

0 个答案:

没有答案