Refactor post.hbs JS

This commit is contained in:
Kevin Ansfield 2017-06-19 00:11:32 +01:00
parent 397a096949
commit c97defd40a
1 changed files with 47 additions and 58 deletions

107
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 --}}
{{#contentFor "scripts"}}
<script>
// TODO: SOMEONE PLEASE REFACTOR THIS SHITSHOW? 😭
// Note: Scroll performance is poor in Safari
$(document).ready(function(){
$(function() {
// 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 () {
// Start fitVids
var $postContent = $(".post-full-content");
$postContent.fitVids();
// End fitVids
// Start show/hide floating header
$(window).scroll(function() {
var header = $(".floating-header");
var title = $(".post-full-title");
var trigger = title.offset().top;
var scroll = $(window).scrollTop();
var progressBar = document.querySelector('progress');
var header = document.querySelector('.floating-header');
var title = document.querySelector('.post-full-title');
if (scroll >= trigger + title.height() + 35 ) {
header.addClass("floating-active");
var lastScrollY = window.scrollY;
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 {
header.removeClass("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();
header.classList.remove('floating-active');
}
var getValue = function(){
return $(window).scrollTop();
progressBar.setAttribute('max', progressMax);
progressBar.setAttribute('value', lastScrollY);
ticking = false;
}
if('max' in document.createElement('progress')){
var progressBar = $('progress');
progressBar.attr({ max: getMax() });
window.addEventListener('scroll', onScroll, {passive: true});
window.addEventListener('resize', onResize, false);
$(document).on('scroll', function(){
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
update();
});
</script>
{{/contentFor}}