the-glorious-startpage/js/body-background-set.js

54 lines
1.3 KiB
JavaScript
Raw Normal View History

2020-06-04 01:31:49 +02:00
// High quality background, we'll lazy load this
var hqBG = document.createElement("img");
2020-06-04 01:36:54 +02:00
var backgroundBody = document.getElementById('bodyBackground')
2020-06-04 01:31:49 +02:00
2020-06-04 01:36:54 +02:00
// Style body background
2020-06-04 01:31:49 +02:00
const styleBodyBackground = () => {
backgroundBody.style.backgroundSize = 'cover';
backgroundBody.style.backgroundRepeat = "no-repeat";
backgroundBody.style.backgroundPosition = "center";
backgroundBody.style.backgroundAttachment = "fixed";
}
const lazyLoadBackground = (fileName) => {
// Set a low quality background image
backgroundBody.style.background = "url('assets/backgrounds/" +
fileName + "-low" + ".webp')";
styleBodyBackground();
2020-06-04 01:36:54 +02:00
backgroundBody.className = "blurredBodyBackground";
2020-06-04 01:31:49 +02:00
hqBG.onload = function() {
// After downloading the HQ image, set it as bg
backgroundBody.style.background = "url("+ hqBG.src; + ")";
styleBodyBackground();
2020-06-04 01:36:54 +02:00
backgroundBody.className = "unblurredBodyBackground";
2020-06-04 01:31:49 +02:00
}
// Add a delay when to fetch background
setTimeout(
function() {
hqBG.src = "assets/backgrounds/" +
fileName + ".webp";
},
50
);
}
const initLazyLoad = () => {
var date = new Date();
var hour = date.getHours();
if (hour >= 6 && hour < 12) {
lazyLoadBackground("morning");
} else if (hour >= 12 && hour < 18 ) {
lazyLoadBackground("noon");
} else {
lazyLoadBackground("evening");
}
}
2020-06-04 01:36:54 +02:00
// Initialize
2020-06-04 01:31:49 +02:00
window.onload = initLazyLoad();