Refactor post.hbs JS

This commit is contained in:
Kevin Ansfield 2017-06-19 00:11:32 +01:00
parent 397a096949
commit c97defd40a

105
post.hbs
View File

@ -151,77 +151,66 @@ into the {body} of the default.hbs template --}}
{{!-- The #contentFor helper here will send everything inside it up to the matching #block helper found in default.hbs --}} {{!-- The #contentFor helper here will send everything inside it up to the matching #block helper found in default.hbs --}}
{{#contentFor "scripts"}} {{#contentFor "scripts"}}
<script> <script>
// TODO: SOMEONE PLEASE REFACTOR THIS SHITSHOW? 😭
// Note: Scroll performance is poor in Safari // 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
$(document).ready(function () { $(document).ready(function () {
$(function() {
// Start fitVids // Start fitVids
var $postContent = $(".post-full-content"); var $postContent = $(".post-full-content");
$postContent.fitVids(); $postContent.fitVids();
// End fitVids // End fitVids
// Start show/hide floating header var progressBar = document.querySelector('progress');
$(window).scroll(function() { var header = document.querySelector('.floating-header');
var header = $(".floating-header"); var title = document.querySelector('.post-full-title');
var title = $(".post-full-title");
var trigger = title.offset().top;
var scroll = $(window).scrollTop();
if (scroll >= trigger + title.height() + 35 ) { var lastScrollY = window.scrollY;
header.addClass("floating-active"); var lastWindowHeight = window.innerHeight;
var lastDocumentHeight = $(document).height();
var ticking = false;
function onScroll() {
lastScrollY = window.scrollY;
requestTick();
}
function onResize() {
lastWindowHeight = window.innerHeight;
lastDocumentHeight = $(document).height();
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 { } else {
header.removeClass("floating-active"); header.classList.remove('floating-active');
}
});
// End show/hide floating header
});
// Start scroll-progress bar
// Source: https://codepen.io/pankajparashar/pen/towxF
// Markup: floating-header.hbs
var getMax = function(){
return $(document).height() - $(window).height();
} }
var getValue = function(){ progressBar.setAttribute('max', progressMax);
return $(window).scrollTop(); progressBar.setAttribute('value', lastScrollY);
ticking = false;
} }
if('max' in document.createElement('progress')){ window.addEventListener('scroll', onScroll, {passive: true});
var progressBar = $('progress'); window.addEventListener('resize', onResize, false);
progressBar.attr({ max: getMax() });
$(document).on('scroll', function(){ update();
progressBar.attr({ value: getValue() });
});
$(window).resize(function(){
progressBar.attr({ max: getMax(), value: getValue() });
});
}
else {
var progressBar = $('.progress-bar'),
max = getMax(),
value, width;
var getWidth = function(){
value = getValue();
width = (value/max) * 100;
width = width + '%';
return width;
}
var setWidth = function(){
progressBar.css({ width: getWidth() });
}
$(document).on('scroll', setWidth);
$(window).on('resize', function(){
max = getMax();
setWidth();
});
}
// End scroll-progress bar
}); });
</script> </script>
{{/contentFor}} {{/contentFor}}