main.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. jQuery(document).ready(function($){
  2. var mainHeader = $('.v3listHead .m_top'),
  3. secondaryNavigation = $('.y_qylist'),
  4. //this applies only if secondary nav is below intro section
  5. belowNavHeroContent = $('.y_homelist_list'),
  6. headerHeight = mainHeader.height();
  7. //set scrolling variables
  8. var scrolling = false,
  9. previousTop = 0,
  10. currentTop = 0,
  11. scrollDelta = 10,
  12. scrollOffset = 150;
  13. $(window).on('scroll', function(){
  14. if( !scrolling ) {
  15. scrolling = true;
  16. (!window.requestAnimationFrame)
  17. ? setTimeout(autoHideHeader, 250)
  18. : requestAnimationFrame(autoHideHeader);
  19. }
  20. });
  21. $(window).on('resize', function(){
  22. headerHeight = mainHeader.height();
  23. });
  24. function autoHideHeader() {
  25. var currentTop = $(window).scrollTop();
  26. ( belowNavHeroContent.length > 0 )
  27. ? checkStickyNavigation(currentTop) // secondary navigation below intro
  28. : checkSimpleNavigation(currentTop);
  29. previousTop = currentTop;
  30. scrolling = false;
  31. }
  32. function checkSimpleNavigation(currentTop) {
  33. //there's no secondary nav or secondary nav is below primary nav
  34. if (previousTop - currentTop > scrollDelta) {
  35. //if scrolling up...
  36. mainHeader.removeClass('is-hidden');
  37. } else if( currentTop - previousTop > scrollDelta && currentTop > scrollOffset) {
  38. //if scrolling down...
  39. mainHeader.addClass('is-hidden');
  40. }
  41. }
  42. function checkStickyNavigation(currentTop) {
  43. //secondary nav below intro section - sticky secondary nav
  44. var secondaryNavOffsetTop = belowNavHeroContent.offset().top - secondaryNavigation.height() - mainHeader.height();
  45. if (previousTop >= currentTop ) {
  46. //if scrolling up...
  47. if( currentTop < secondaryNavOffsetTop ) {
  48. //secondary nav is not fixed
  49. mainHeader.removeClass('is-hidden');
  50. $('.v3listHead').removeClass('on'); //小明添加
  51. secondaryNavigation.removeClass('fixed slide-up');
  52. belowNavHeroContent.removeClass('secondary-nav-fixed');
  53. } else if( previousTop - currentTop > scrollDelta ) {
  54. //secondary nav is fixed
  55. mainHeader.removeClass('is-hidden');
  56. $('.v3listHead').removeClass('on'); //小明添加
  57. secondaryNavigation.removeClass('slide-up').addClass('fixed');
  58. belowNavHeroContent.addClass('secondary-nav-fixed');
  59. }
  60. } else {
  61. //if scrolling down...
  62. if( currentTop > secondaryNavOffsetTop + scrollOffset ) {
  63. //hide primary nav
  64. mainHeader.addClass('is-hidden');
  65. $('.v3listHead').addClass('on'); //小明添加
  66. secondaryNavigation.addClass('fixed slide-up');
  67. belowNavHeroContent.addClass('secondary-nav-fixed');
  68. } else if( currentTop > secondaryNavOffsetTop ) {
  69. //once the secondary nav is fixed, do not hide primary nav if you haven't scrolled more than scrollOffset
  70. mainHeader.removeClass('is-hidden');
  71. $('.v3listHead').removeClass('on'); //小明添加
  72. secondaryNavigation.addClass('fixed').removeClass('slide-up');
  73. belowNavHeroContent.addClass('secondary-nav-fixed');
  74. }
  75. }
  76. }
  77. });