1- import 'sticky-kit/dist/sticky-kit' ;
2-
31import $ from 'jquery' ;
42import _ from 'lodash' ;
53import responsiveCutoff from 'vj/breakpoints.json' ;
@@ -10,40 +8,26 @@ const navHeight = isBelow(responsiveCutoff.mobile)
108 ? 0
119 : $ ( '.nav' ) . height ( ) ;
1210
11+ function getCutoff ( str ) {
12+ if ( str === 'medium' ) return responsiveCutoff . mobile ;
13+ if ( str === 'large' ) return responsiveCutoff . desktop ;
14+ return 0 ;
15+ }
16+
1317function updateStickies ( $stickies ) {
1418 $stickies . get ( ) . forEach ( ( element ) => {
1519 const $sticky = $ ( element ) ;
16- const shouldEnableSticky = ( isAbove ( $sticky . data ( 'sticky-cutoff-min' ) ) ) ;
17- const stickyEnabled = $sticky . data ( 'sticky-enabled' ) ;
18- if ( shouldEnableSticky && ! stickyEnabled ) {
19- const stickyOptions = { } ;
20- const $stickyParent = $sticky . closest ( '[data-sticky-parent]' ) ;
21- if ( $stickyParent . length > 0 ) {
22- stickyOptions . parent = $stickyParent ;
23- }
24- stickyOptions . offset_top = 10 + navHeight ;
25- $sticky . stick_in_parent ( stickyOptions ) ;
26- $sticky . data ( 'sticky-enabled' , true ) ;
27- } else if ( ! shouldEnableSticky && stickyEnabled ) {
28- $sticky . trigger ( 'sticky_kit:detach' ) ;
29- $sticky . data ( 'sticky-enabled' , false ) ;
20+ const shouldEnableSticky = isAbove ( $sticky . data ( 'sticky-cutoff-min' ) ) ;
21+ if ( shouldEnableSticky ) {
22+ element . style . position = 'sticky' ;
23+ element . style . top = `${ 10 + navHeight } px` ;
24+ } else {
25+ element . style . position = '' ;
26+ element . style . top = '' ;
3027 }
3128 } ) ;
3229}
3330
34- function getCutoff ( str ) {
35- if ( str === 'medium' ) {
36- return responsiveCutoff . mobile ;
37- } if ( str === 'large' ) {
38- return responsiveCutoff . desktop ;
39- }
40- return 0 ;
41- }
42-
43- function stickyRelayout ( ) {
44- $ ( 'body' ) . trigger ( 'sticky_kit:recalc' ) ;
45- }
46-
4731const stickyPage = new AutoloadPage ( 'stickyPage' , ( ) => {
4832 let shouldListenResize = false ;
4933 const $stickies = $ ( '[data-sticky]' ) ;
@@ -54,13 +38,11 @@ const stickyPage = new AutoloadPage('stickyPage', () => {
5438 shouldListenResize = true ;
5539 }
5640 $sticky . data ( 'sticky-cutoff-min' , getCutoff ( minEnabledSize ) ) ;
57- $sticky . data ( 'sticky-enabled' , false ) ;
5841 } ) ;
5942 updateStickies ( $stickies ) ;
6043 if ( shouldListenResize ) {
6144 $ ( window ) . on ( 'resize' , _ . throttle ( ( ) => updateStickies ( $stickies ) , 300 ) ) ;
6245 }
63- $ ( document ) . on ( 'vjLayout' , _ . throttle ( stickyRelayout , 100 ) ) ;
6446} ) ;
6547
6648export default stickyPage ;
0 commit comments