/* home-animation.js ================= This file is manually included by the home_inc-js.php file within this themes directory if animations are not disabled. This file is only loaded on the homepage of this theme, and doesn't apply animations across the site. If you wish to apply an animation across the whole site then please do so in global-animation.js Remember that animations are theme specific, meaning this file only effects the current theme that you are animating, and not every theme across the TemplateOTS platform. See detailed documentation here:- https://docs.google.com/document/d/1n5sWQ8SIr-zjOpTv8YnOTHJapO8WdedjDfbeo-lkqMM/edit#heading=h.lmxb59mpcpe2 */ /* FUNCTIONS ========= fOnScroll() - Called when the user scrolls, simply requests and animation tick fOnScrollUpdate() - Called when a scroll tick has been allowed, handels the actual scroll logic fRequestScrollTick() - Called to request a scroll tick depending on if the frame has been met */ (function () { /* VARIABLE DECLORATION FOR SCROLLING STUFF ======================================== */ // Store the last known scroll position var nLastKnownScrollPosition = 0; // Store the value if frame is ticking or not var bScrollTicking = false; // Store the object var oDocument = $(document); /* On Document Loaded ================== Called by Jquery event handler when the document is ready (When content has been loaded) */ $( document ).ready( function() { // If we have a products section if( $(".product-section").length ) { // If we are on desktop if( $(window).width() > 991 ) { // Make the landing container fixed $(".landing-container").css( { position : "fixed" } ); } } // Fade the navigation in TweenMax.to(".navigation", 0.8, { opacity : 1, ease : Power2.easeIn } ); // Call scroll update to set inital values fOnScroll(); // On document scroll oDocument.scroll(function() { // Call on scroll fOnScroll(); }); }); /* fOnScroll() =========== Simply store the scroll position and request an animation frame. */ function fOnScroll() { // Store the last known scroll position nLastKnownScrollPosition = oDocument.scrollTop(); // Fix safari bug where you can scroll upwards due to the scroll top offset safari allows if( nLastKnownScrollPosition < 0 ) { // Clamp the last known scroll position nLastKnownScrollPosition = 0; } // Request a scroll animation tick fRequestScrollTick(); } /* fOnScrollUpdate() ================ This function is called by requestAnimationFrame when we log a tick. IF YOU DON'T KNOW WHAT YOU ARE DOING THEN PLEASE DON'T MODIFY, VALUES HAVE BEEN REFINED TO HAVE THE PERFECT ANIMATION BLEND, ANY TWEAKING OF VALUES YOU DON'T UNDERTSTAND COULD RUIN THE FLOW OF THE ANIMATIONS */ function fOnScrollUpdate() { // If we have a product section if( $(".product-section").length ) { // Store the window height var nWindowHeight = $(window).innerHeight(); // Store the opacity offset for the landing var nOpacityOffsetLanding = nLastKnownScrollPosition / ( nWindowHeight / 1.5); // Store the opacity offset for the products var nOpacityOffsetProducts = nLastKnownScrollPosition / ( nWindowHeight / 1.5); // Store the height offset var nHeightOffset = -( nLastKnownScrollPosition / ( nWindowHeight ) ); // If the offset is less than 1 if( nOpacityOffsetLanding > 1 ) { // Clamp the offset nOpacityOffsetLanding = 1; } // If the offset is less than 1 if( nOpacityOffsetProducts > 1 ) { // Clamp the offset nOpacityOffsetProducts = 1; } // If we still need to tween the height offset if( nOpacityOffsetLanding < 1 ) { /* This is no longer done to help with performance, instead we make the background fixed left here for historical // Tween the top of the landing relative container TweenMax.to(".landing-wrapper-relative-container", 0.05, { height : Math.round( nWindowHeight + (nHeightOffset * (nWindowHeight / 3) + ( nHeightOffset * (nWindowHeight / 1.5) ) ) ), ease : Power2.easeIn } ); // Tween the top of the landing relative container TweenMax.to(".landing-wrapper", 0.05, { top : Math.round(nHeightOffset * (nWindowHeight / 3) ), ease : Power2.easeIn } ); */ } // If opacity offset is not less than 1 else { // Set the opacity landing offset to 1 nOpacityOffsetLanding = 1; } // If we still need to tween the opacity offset if( nOpacityOffsetProducts > 1 ) { // Set the opacity products offset to 1 nOpacityOffsetProducts = 1; } // Tween the opacity of the product section TweenMax.to(".product-section", 0.05, { opacity : nOpacityOffsetProducts, ease : Power2.easeIn } ); // Tween the opacity of the landing relative container TweenMax.to(".landing-container", 0.05, { opacity : 1 - nOpacityOffsetLanding, ease : Power2.easeIn } ); } // Set the scroll ticking to be false, so this function can be called again bScrollTicking = false; } /* fRequestScrollTick() ==================== Simply requests an animation tick, all this function does is check if we have already called a tick, and if not then call a tick. simple. */ function fRequestScrollTick() { // If we aren't currently ticking if(!bScrollTicking) { // Prepare the request animation for the next frame requestAnimationFrame(fOnScrollUpdate); // Set the ticking to be true! bScrollTicking = true; } } }());