Refactor post.hbs JS
This commit is contained in:
parent
397a096949
commit
c97defd40a
107
post.hbs
107
post.hbs
|
@ -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
|
||||||
$(document).ready(function(){
|
// - this appears to be due to the events firing much more slowly in Safari.
|
||||||
$(function() {
|
// 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
|
// 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}}
|
||||||
|
|
Loading…
Reference in New Issue