fly_out_nav.js 1.4 KB
Newer Older
1 2 3 4
export const calculateTop = (boundingRect, outerHeight) => {
  const windowHeight = window.innerHeight;
  const bottomOverflow = windowHeight - (boundingRect.top + outerHeight);

5 6
  return bottomOverflow < 0 ? (boundingRect.top - outerHeight) + boundingRect.height :
    boundingRect.top;
7 8
};

P
Phil Hughes 已提交
9
export const showSubLevelItems = (el) => {
P
Phil Hughes 已提交
10
  const subItems = el.querySelector('.sidebar-sub-level-items');
P
Phil Hughes 已提交
11

P
Phil Hughes 已提交
12
  if (!subItems) return;
P
Phil Hughes 已提交
13

P
Phil Hughes 已提交
14
  subItems.style.display = 'block';
P
Phil Hughes 已提交
15
  el.classList.add('is-over');
P
Phil Hughes 已提交
16 17

  const boundingRect = el.getBoundingClientRect();
P
Phil Hughes 已提交
18
  const top = calculateTop(boundingRect, subItems.offsetHeight);
P
Phil Hughes 已提交
19 20
  const isAbove = top < boundingRect.top;

P
Phil Hughes 已提交
21
  subItems.style.transform = `translate3d(0, ${Math.floor(top)}px, 0)`;
P
Phil Hughes 已提交
22 23

  if (isAbove) {
P
Phil Hughes 已提交
24
    subItems.classList.add('is-above');
P
Phil Hughes 已提交
25 26 27 28
  }
};

export const hideSubLevelItems = (el) => {
P
Phil Hughes 已提交
29
  const subItems = el.querySelector('.sidebar-sub-level-items');
P
Phil Hughes 已提交
30

P
Phil Hughes 已提交
31
  if (!subItems) return;
P
Phil Hughes 已提交
32

P
Phil Hughes 已提交
33
  el.classList.remove('is-over');
P
Phil Hughes 已提交
34 35
  subItems.style.display = 'none';
  subItems.classList.remove('is-above');
P
Phil Hughes 已提交
36 37
};

38
export default () => {
P
Phil Hughes 已提交
39 40
  const items = [...document.querySelectorAll('.sidebar-top-level-items > li:not(.active)')]
    .filter(el => el.querySelector('.sidebar-sub-level-items'));
P
Phil Hughes 已提交
41 42 43 44 45

  items.forEach((el) => {
    el.addEventListener('mouseenter', e => showSubLevelItems(e.currentTarget));
    el.addEventListener('mouseleave', e => hideSubLevelItems(e.currentTarget));
  });
46
};