直接上代码,本例采用了jQuery来进行介绍

 

CSS部分:

.sticky {
  position: fixed; 
  top: 0;
}

  

JS部分:

var stickyBar = $('.sticky-bar');
var stickyLine = stickyBar.offset().top;
function toggleSticky(e) {
  window.scrollY > stickyLine ? stickyBar.addClass('sticky') : stickyBar.removeClass('sticky');
}
$(document).on('scroll', toggleSticky);

 

Posted on 2015-07-27 07:01  向民  阅读(698)  评论(0编辑  收藏  举报