mirror of
				https://github.com/ZetaKebab/kesper.git
				synced 2025-11-04 08:09:49 +00:00 
			
		
		
		
	Fixed sticky title and author hover JS (#629)
no issue
- replaces author hover JS that was lost during merge
- replaces `floating-header.js` with `sticky-nav-title.js` containing the relevant JS that was lost during merge with a cleanup for unused variables and unnecessary event listeners
- removes reference to non-existent `{{> floating-header}}` partial
			
			
This commit is contained in:
		@@ -1,73 +0,0 @@
 | 
			
		||||
/* eslint-env browser */
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Floating header
 | 
			
		||||
 * Used on invividual post pages, displays a sticky header with progress indicator
 | 
			
		||||
 *
 | 
			
		||||
 * This JS is automatically applied for any template where you use the
 | 
			
		||||
 * {{> floating-header}} partial
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
(function (window, document) {
 | 
			
		||||
    // set up Casper as a global object
 | 
			
		||||
    if (!window.Casper) {
 | 
			
		||||
        window.Casper = {};
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    window.Casper.floatingHeader = function floatingHeader() {
 | 
			
		||||
        // NOTE: Scroll performance is poor in Safari
 | 
			
		||||
        // - this appears to be due to the events firing much more slowly in Safari.
 | 
			
		||||
        //   Dropping the scroll event and using only a raf loop results in smoother
 | 
			
		||||
        //   scrolling but continuous processing even when not scrolling
 | 
			
		||||
 | 
			
		||||
        var progressBar = document.querySelector('#reading-progress');
 | 
			
		||||
        var header = document.querySelector('.floating-header');
 | 
			
		||||
        var title = document.querySelector('.post-full-title');
 | 
			
		||||
 | 
			
		||||
        var lastScrollY = window.scrollY;
 | 
			
		||||
        var lastWindowHeight = window.innerHeight;
 | 
			
		||||
        var lastDocumentHeight = document.body.clientHeight;
 | 
			
		||||
        var ticking = false;
 | 
			
		||||
 | 
			
		||||
        function onScroll() {
 | 
			
		||||
            lastScrollY = window.scrollY;
 | 
			
		||||
            requestTick();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        function onResize() {
 | 
			
		||||
            lastWindowHeight = window.innerHeight;
 | 
			
		||||
            lastDocumentHeight = document.body.clientHeight;
 | 
			
		||||
            requestTick();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        function requestTick() {
 | 
			
		||||
            if (!ticking) {
 | 
			
		||||
                requestAnimationFrame(update);
 | 
			
		||||
            }
 | 
			
		||||
            ticking = true;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        function update() {
 | 
			
		||||
            var trigger = title.getBoundingClientRect().top + window.scrollY;
 | 
			
		||||
            var triggerOffset = title.offsetHeight + 35;
 | 
			
		||||
            var progressMax = lastDocumentHeight - lastWindowHeight;
 | 
			
		||||
 | 
			
		||||
            // show/hide floating header
 | 
			
		||||
            if (lastScrollY >= trigger + triggerOffset) {
 | 
			
		||||
                header.classList.add('floating-active');
 | 
			
		||||
            } else {
 | 
			
		||||
                header.classList.remove('floating-active');
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            progressBar.setAttribute('max', progressMax);
 | 
			
		||||
            progressBar.setAttribute('value', lastScrollY);
 | 
			
		||||
 | 
			
		||||
            ticking = false;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        window.addEventListener('scroll', onScroll, {passive: true});
 | 
			
		||||
        window.addEventListener('resize', onResize, false);
 | 
			
		||||
 | 
			
		||||
        update();
 | 
			
		||||
    };
 | 
			
		||||
})(window, document);
 | 
			
		||||
							
								
								
									
										61
									
								
								assets/js/sticky-nav-title.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										61
									
								
								assets/js/sticky-nav-title.js
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,61 @@
 | 
			
		||||
/* eslint-env browser */
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Nav/Title replacement
 | 
			
		||||
 * Used on invividual post pages, displays the post title in place of the nav
 | 
			
		||||
 * bar when scrolling past the title
 | 
			
		||||
 *
 | 
			
		||||
 * Usage:
 | 
			
		||||
 * ```
 | 
			
		||||
 * Casper.stickyTitle({
 | 
			
		||||
 *     navSelector: '.site-nav-main',
 | 
			
		||||
 *     titleSelector: '.post-full-title',
 | 
			
		||||
 *     activeClass: 'nav-post-title-active'
 | 
			
		||||
 * });
 | 
			
		||||
 * ```
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
(function (window, document) {
 | 
			
		||||
    // set up Casper as a global object
 | 
			
		||||
    if (!window.Casper) {
 | 
			
		||||
        window.Casper = {};
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    window.Casper.stickyNavTitle = function stickyNavTitle(options) {
 | 
			
		||||
        var nav = document.querySelector(options.navSelector);
 | 
			
		||||
        var title = document.querySelector(options.titleSelector);
 | 
			
		||||
 | 
			
		||||
        var lastScrollY = window.scrollY;
 | 
			
		||||
        var ticking = false;
 | 
			
		||||
 | 
			
		||||
        function onScroll() {
 | 
			
		||||
            lastScrollY = window.scrollY;
 | 
			
		||||
            requestTick();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        function requestTick() {
 | 
			
		||||
            if (!ticking) {
 | 
			
		||||
                requestAnimationFrame(update);
 | 
			
		||||
            }
 | 
			
		||||
            ticking = true;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        function update() {
 | 
			
		||||
            var trigger = title.getBoundingClientRect().top + window.scrollY;
 | 
			
		||||
            var triggerOffset = title.offsetHeight + 35;
 | 
			
		||||
 | 
			
		||||
            // show/hide post title
 | 
			
		||||
            if (lastScrollY >= trigger + triggerOffset) {
 | 
			
		||||
                nav.classList.add(options.activeClass);
 | 
			
		||||
            } else {
 | 
			
		||||
                nav.classList.remove(options.activeClass);
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            ticking = false;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        window.addEventListener('scroll', onScroll, {passive: true});
 | 
			
		||||
 | 
			
		||||
        update();
 | 
			
		||||
    };
 | 
			
		||||
})(window, document);
 | 
			
		||||
		Reference in New Issue
	
	Block a user