mirror of
https://github.com/ViViDboarder/Vivid-Casper.git
synced 2024-12-23 03:57:38 +00:00
Refactor post.hbs JS
This commit is contained in:
parent
397a096949
commit
c97defd40a
105
post.hbs
105
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
|
|
||||||
$(document).ready(function(){
|
|
||||||
$(function() {
|
|
||||||
// Start fitVids
|
|
||||||
var $postContent = $(".post-full-content");
|
|
||||||
$postContent.fitVids();
|
|
||||||
// End fitVids
|
|
||||||
|
|
||||||
// Start show/hide floating header
|
// NOTE: Scroll performance is poor in Safari
|
||||||
$(window).scroll(function() {
|
// - this appears to be due to the events firing much more slowly in Safari.
|
||||||
var header = $(".floating-header");
|
// Dropping the scroll event and using only a raf loop results in smoother
|
||||||
var title = $(".post-full-title");
|
// scrolling but continuous processing even when not scrolling
|
||||||
var trigger = title.offset().top;
|
$(document).ready(function () {
|
||||||
var scroll = $(window).scrollTop();
|
// Start fitVids
|
||||||
|
var $postContent = $(".post-full-content");
|
||||||
|
$postContent.fitVids();
|
||||||
|
// End fitVids
|
||||||
|
|
||||||
if (scroll >= trigger + title.height() + 35 ) {
|
var progressBar = document.querySelector('progress');
|
||||||
header.addClass("floating-active");
|
var header = document.querySelector('.floating-header');
|
||||||
} else {
|
var title = document.querySelector('.post-full-title');
|
||||||
header.removeClass("floating-active");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
// End show/hide floating header
|
|
||||||
});
|
|
||||||
|
|
||||||
// Start scroll-progress bar
|
var lastScrollY = window.scrollY;
|
||||||
// Source: https://codepen.io/pankajparashar/pen/towxF
|
var lastWindowHeight = window.innerHeight;
|
||||||
// Markup: floating-header.hbs
|
var lastDocumentHeight = $(document).height();
|
||||||
var getMax = function(){
|
var ticking = false;
|
||||||
return $(document).height() - $(window).height();
|
|
||||||
|
function onScroll() {
|
||||||
|
lastScrollY = window.scrollY;
|
||||||
|
requestTick();
|
||||||
}
|
}
|
||||||
|
|
||||||
var getValue = function(){
|
function onResize() {
|
||||||
return $(window).scrollTop();
|
lastWindowHeight = window.innerHeight;
|
||||||
|
lastDocumentHeight = $(document).height();
|
||||||
|
requestTick();
|
||||||
}
|
}
|
||||||
|
|
||||||
if('max' in document.createElement('progress')){
|
function requestTick() {
|
||||||
var progressBar = $('progress');
|
if (!ticking) {
|
||||||
progressBar.attr({ max: getMax() });
|
requestAnimationFrame(update);
|
||||||
|
}
|
||||||
$(document).on('scroll', function(){
|
ticking = true;
|
||||||
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(){
|
function update() {
|
||||||
value = getValue();
|
var trigger = title.getBoundingClientRect().top + window.scrollY;
|
||||||
width = (value/max) * 100;
|
var triggerOffset = title.offsetHeight + 35;
|
||||||
width = width + '%';
|
var progressMax = lastDocumentHeight - lastWindowHeight;
|
||||||
return width;
|
|
||||||
|
// show/hide floating header
|
||||||
|
if (lastScrollY >= trigger + triggerOffset) {
|
||||||
|
header.classList.add('floating-active');
|
||||||
|
} else {
|
||||||
|
header.classList.remove('floating-active');
|
||||||
}
|
}
|
||||||
|
|
||||||
var setWidth = function(){
|
progressBar.setAttribute('max', progressMax);
|
||||||
progressBar.css({ width: getWidth() });
|
progressBar.setAttribute('value', lastScrollY);
|
||||||
}
|
|
||||||
|
|
||||||
$(document).on('scroll', setWidth);
|
ticking = false;
|
||||||
$(window).on('resize', function(){
|
|
||||||
max = getMax();
|
|
||||||
setWidth();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
// End scroll-progress bar
|
|
||||||
|
window.addEventListener('scroll', onScroll, {passive: true});
|
||||||
|
window.addEventListener('resize', onResize, false);
|
||||||
|
|
||||||
|
update();
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
{{/contentFor}}
|
{{/contentFor}}
|
||||||
|
Loading…
Reference in New Issue
Block a user