Created
July 16, 2021 10:40
-
-
Save breakerh/b6faf102708d60802b16d09a2c8bef8a to your computer and use it in GitHub Desktop.
Add fading background on body in Elementor Pro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
body { | |
transition: background .8s ease; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment