22Name: Header Resize
33Author: Marktime Media
44Author URI: http://marktimemedia.com
5- Version: 0.2
5+ Version: 0.3
66License: GPLv2
77
88 This program is free software; you can redistribute it and/or modify
@@ -18,63 +18,35 @@ License: GPLv2
1818 http://www.gnu.org/licenses/gpl-2.0.html
1919*/
2020
21- ( function ( $ ) {
21+ const header = document . querySelector ( '.header-main' ) ;
2222
23- /* Scroll Header */
23+ // The header is sticky but still in flow, so shrinking it lifts the page by 25px
24+ // ($headerInnerHeight 125px down to the 100px cap) and scroll anchoring corrects scrollY
25+ // to match. One threshold would be re-crossed by that correction and the class would
26+ // toggle in a loop, so the bound to beat depends on which state we are in. Growing back
27+ // only at 0 is the safe end: browsers suppress anchoring while the scroller sits at 0.
28+ const SHRINK_AT = 40 ;
29+ const GROW_AT = 0 ;
2430
25- var $lastScrollTop = $ ( window ) . scrollTop ( ) ; // reset variable any time it reloads
26- var $siteHeader = $ ( '.header-main' ) ; // your header element
27- var $content = $ ( '#content' ) ; // content container
28- var $contentWithHeader = $ ( 'body #content:not(.has_no_sidebar)' ) ; // content container
29- var changeDirection = - 1 ; // base comparitive variable
30- var shrinkClass = 'header-main-small' ; // your small header class
31- var paddingTop = $siteHeader . innerHeight ( ) ;
32- var paddingTopPlus = ( $siteHeader . innerHeight ( ) + 50 ) ;
31+ if ( header ) {
32+ let ticking = false ;
3333
34- // $content.css('padding-top', paddingTop);
35- // $contentWithHeader.css('padding-top', paddingTopPlus);
34+ const update = ( ) => {
35+ ticking = false ;
36+ const isSmall = header . classList . contains ( 'header-main-small' ) ;
37+ const next = window . scrollY > ( isSmall ? GROW_AT : SHRINK_AT ) ;
3638
37- $ ( window ) . on ( 'scroll' , ( function ( event ) {
38- var $scrollPosition = $ ( this ) . scrollTop ( ) ;
39-
40- if ( $ ( window ) . width ( ) > 784 ) { // we're mobile first so this is anything larger than our mobile breakpoint
41-
42- if ( $scrollPosition > 100 ) { // once you get far enough down, shrink the header
43-
44- $siteHeader . addClass ( shrinkClass ) ;
45-
46- } else { // bring it back up again when we get back to the top
47-
48- $siteHeader . removeClass ( shrinkClass ) ;
49- }
50-
51- } else { // this is mobile breakpoint or smaller
52-
53- if ( $scrollPosition > 120 && $scrollPosition > $lastScrollTop ) { // once you get far enough down, hide the header
54-
55- changeDirection = - 1 ; // reset changeDirection
56- $siteHeader . addClass ( shrinkClass ) ;
57-
58- } else { // bring it back up again if we scroll up at all
59-
60- if ( - 1 == changeDirection ) {
61- changeDirection = $scrollPosition ; // only set changeDirection once
62- }
63-
64- // console.log(changeDirection + ' ' + scrollPosition);
65-
66- if ( $scrollPosition < ( changeDirection - 100 ) ) { // only add after you've scrolled up a bit
67-
68- $siteHeader . removeClass ( shrinkClass ) ;
69- changeDirection = - 1 ; // reset changeDirection
70- }
71-
72- }
73-
74- $lastScrollTop = $scrollPosition ;
39+ if ( next !== isSmall ) {
40+ header . classList . toggle ( 'header-main-small' , next ) ;
7541 }
42+ } ;
7643
77- } ) ) ;
78-
44+ window . addEventListener ( 'scroll' , ( ) => {
45+ if ( ! ticking ) {
46+ ticking = true ;
47+ window . requestAnimationFrame ( update ) ;
48+ }
49+ } , { passive : true } ) ;
7950
80- } ) ( jQuery ) ;
51+ update ( ) ; // reloads restore scroll position before this runs
52+ }
0 commit comments