Skip to content

Commit ff835ee

Browse files
authored
Merge pull request #240 from OpenSourceOrg/fix/header-shrink-on-scroll-develop
Shrink the sticky header sooner, and stop it toggling in a loop
2 parents 869451c + a3ef8d6 commit ff835ee

7 files changed

Lines changed: 39 additions & 64 deletions

File tree

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<?php return array('dependencies' => array(), 'version' => '42979785231fcdbb3f33');
1+
<?php return array('dependencies' => array(), 'version' => 'e269384336004e90c6c8');

‎themes/osi/assets/js/build/theme.js‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎themes/osi/assets/js/src/theme/header-resize.js‎

Lines changed: 26 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Name: Header Resize
33
Author: Marktime Media
44
Author URI: http://marktimemedia.com
5-
Version: 0.2
5+
Version: 0.3
66
License: 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+
}

‎themes/osi/assets/scss/_1_settings.breakpoints.scss‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ $maxPadding: 48px; // duplicates var(--wp--custom--spacing--max-padding)
4646

4747
// header dimensions
4848
$headerInnerHeight: 125px; // .header--inner fixed height; admin-bar offsets come from core's --wp-admin--admin-bar--height
49+
$headerLogoHeight: 110px; // logo cap at rest; needs a length, not none, so the shrink can animate
4950
$mobileRowHeight: 34px; // mobile accordion row: the caret's 44px tap target centres its glyph on this
5051
$midPadding: 32px; // duplicates var(--wp--custom--spacing--mid-padding);
5152
$smallPadding: 16px; // duplicates var(--wp--custom--spacing--small-padding);

‎themes/osi/assets/scss/_6_components.header.scss‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@
2222
.header--inner {
2323
position: static;
2424
opacity: 1;
25-
transition: all .3s;
25+
transition: padding .3s, max-height .3s;
2626
z-index: 999;
2727
height: $headerInnerHeight;
28+
max-height: $headerInnerHeight;
2829
display: flex;
2930
align-items: center;
3031
@media only screen and ( max-width: 1200px) {
@@ -37,7 +38,7 @@
3738
.header--blog-name {
3839
box-sizing: border-box;
3940
max-width: calc(100% - 130px);
40-
max-height: 110px;
41+
max-height: $headerLogoHeight;
4142
height: 100%;
4243
display: flex;
4344
align-items: center;
@@ -56,10 +57,11 @@
5657
img {
5758
// all logos
5859
max-width: 300px;
60+
max-height: $headerLogoHeight;
5961
height: auto;
6062
width: auto;
6163
transform: translateZ(0); // chrome blurry fix
62-
transition: all .3s;
64+
transition: max-height .3s;
6365
@media only screen and (max-width: #{$break-small}) {
6466
max-width: 180px;
6567
}
@@ -116,7 +118,6 @@
116118
.header--inner {
117119
// opacity: 0;
118120
overflow: hidden;
119-
transition: all .3s;
120121
}
121122
.header--blog-name {}
122123
.header--extra-text {

‎themes/osi/style.css‎

Lines changed: 4 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎themes/osi/style.css.map‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)