UNPKG

1.84 kBJavaScriptView Raw
1$().ready(() => {
2 const verticalSections = $('.vertical-section')
3
4 /**
5 * it positions section header that it sticks to the left side of the section and
6 * follows scroll position until the end of a section.
7 *
8 * @param {JQuery} verticalSection particular section of methods/members in the UI
9 * identified by a '.vertical-section' class
10 * @param {Number} leftOffset offset of the header from the left side of the page
11 */
12 const positionSectionHeader = (verticalSection, leftOffset) => {
13 const bottomOfASection = verticalSection.offset().top + verticalSection.height()
14 const topOfASection = verticalSection.offset().top
15 const sectionTitleHeight = verticalSection.find('.title').height()
16 const isSectionCurrentlyViewed =
17 window.pageYOffset > topOfASection &&
18 window.pageYOffset < bottomOfASection
19
20 if (isSectionCurrentlyViewed) {
21 // verticalSection.addClass('active')
22 if (window.pageYOffset > ( bottomOfASection - sectionTitleHeight )) {
23 verticalSection.find('.title').css({ left: 0, position: 'absolute', bottom: '0', top: 'auto'})
24 } else {
25 verticalSection.find('.title').css({ left: leftOffset, bottom: 'auto', position: 'fixed', top: 0})
26 }
27 } else {
28 // verticalSection.removeClass('active')
29 verticalSection.find('.title').css({ left: 0, position: 'absolute' })
30 }
31 }
32
33 if (verticalSections.length > 0) {
34 let leftOffset = verticalSections.offset().left
35 $(window).resize((e) => {
36 leftOffset = verticalSections.offset().left
37 verticalSections.each((i, el) => {
38 positionSectionHeader($(el), leftOffset)
39 })
40 })
41 $(window).scroll((e) => {
42 verticalSections.each((i, el) => {
43 positionSectionHeader($(el), leftOffset)
44 })
45 })
46 }
47})
\No newline at end of file