index.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. "use strict";
  2. $(function () {
  3. var windowWidth = $(window).width(); // 页面滚动偏移
  4. var offset = 0; // pc端
  5. if (windowWidth > 768) {
  6. offset = getScrollOffset('.navbar'); // 导航菜单展开折叠
  7. collapseToggle('[data-collapse-toggle="category"]', 'click', function (result) {
  8. var bindEl = result.bindEl,
  9. parent = result.parent,
  10. height = result.height,
  11. isCollapse = result.isCollapse;
  12. var text = isCollapse ? '展开' : '收起';
  13. var lineHeight = height - 32;
  14. bindEl.children('.icon').toggleClass('arrowup');
  15. bindEl.children('span').text(text);
  16. parent.children('.name').children('em').height(lineHeight);
  17. }); // 可折叠楼层
  18. makeFloorList({
  19. root: '.floor',
  20. // 根节点元素
  21. collapse: '.article',
  22. //可折叠元素
  23. item: '.section',
  24. // 子元素
  25. touch: '.more-btn',
  26. // 事件绑定元素
  27. size: 6,
  28. // 每页最多显示的子元素个数
  29. col: 3,
  30. // 每列最多显示的子元素个数
  31. minHeight: $('.floor').find('.section').height() + 24,
  32. // 单个子元素的高度
  33. // 展开时的回调
  34. open: function open(floor) {
  35. if (floor.current === floor.step) {
  36. floor.$floor.find('.more span').text('收起全部');
  37. floor.$floor.find('.more .icon').addClass('arrowup');
  38. }
  39. },
  40. // 收起时的回调
  41. close: function close(floor) {
  42. floor.$floor.find('.more span').text('查看更多');
  43. floor.$floor.find('.more .icon').removeClass('arrowup');
  44. }
  45. });
  46. } else {
  47. // 移动端
  48. offset = getScrollOffset(['.navbar', '.category']);
  49. stickyResponse('#category', getScrollOffset('.banner'), function (e) {
  50. if (e.sticky) {
  51. $('#category-list').addClass('fixed');
  52. } else {
  53. $('#category-list').removeClass('fixed');
  54. }
  55. });
  56. } // 生成导航
  57. makeNavigate('.floor h2', '.navigate');
  58. makeNavigate('.floor h2', '#category-list'); // 楼层滚动
  59. bindCategory('.navigate li,.category-list li', '.floor', offset, function (index) {
  60. activeCategory('.navigate li, .category-list li', index);
  61. }); // 页面滚动
  62. $('.navigate').hide();
  63. pageScrollObserve('.floor', offset, function (el, index) {
  64. if (index > -1) {
  65. windowWidth > 768 && $('.navigate').fadeIn();
  66. activeCategory('.navigate li, .category-list li', index);
  67. } else {
  68. windowWidth > 768 && $('.navigate').fadeOut();
  69. }
  70. });
  71. });