Skip to content

Instantly share code, notes, and snippets.

@breakerh
Created July 16, 2021 10:40
Show Gist options
  • Save breakerh/b6faf102708d60802b16d09a2c8bef8a to your computer and use it in GitHub Desktop.
Save breakerh/b6faf102708d60802b16d09a2c8bef8a to your computer and use it in GitHub Desktop.
Add fading background on body in Elementor Pro
<script type="text/javascript">
let faders = [...document.querySelectorAll('section[data-background]')].reverse();
function Utils() {
}
Utils.prototype = {
constructor: Utils,
isElementInView: function (element, fullyInView) {
var pageTop = jQuery(window).scrollTop();
var pageBottom = pageTop + jQuery(window).height();
var elementTop = jQuery(element).offset().top + (jQuery(window).height()/4);
var elementBottom = elementTop + jQuery(element).height() - (jQuery(window).height()/4);
if (fullyInView === true) {
return ((pageTop < elementTop) && (pageBottom > elementBottom));
} else {
return ((elementTop <= pageBottom) && (elementBottom >= pageTop));
}
}
};
var Utils = new Utils();
jQuery(function(){
jQuery(window).scroll(function(){
faders.some(function(element){
if(Utils.isElementInView(jQuery(element), false)){
document.querySelector('body').style.background = '#'+element.getAttribute('data-background');
return true;
}
});
});
});
</script>
body {
transition: background .8s ease;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment