1101} else {1102window.addEventListener(1103'resize',1104t_throttle(function () {1105scrollParallaxOptions = t_animationExt__getParallaxOffests(elements);1106t_animationExt__animateParallaxByScroll(scrollParallaxOptions);1107}, 50)1108);1109}11101111window.addEventListener('scroll', function () {1112t_animationExt__animateParallaxByScroll(scrollParallaxOptions);
});}/** * create object with necessary params to create and animate parallax * * @param {HTMLElement[]} elements * @returns {ObjecForCreatingParallax[]} */function t_animationExt__getParallaxOffests(elements) { return elements.map(function (scrollParallaxEl) { var speedFactor = scrollParallaxEl.getAttribute('data-parallax-speed'); var posY = window.pageYOffset; var scaledDifference = t_animationExt__calcScaledDiff(scrollParallaxEl); var blockTopPos = scrollParallaxEl.getBoundingClientRect().top + posY + scaledDifference; var blockHeight = scrollParallaxEl.clientHeight; var isAboveParallax = posY > blockTopPos; // update element top position inside zero-block, that use autoscale var zoomValue = t_animationExt__getZoom(scrollParallaxEl); if (window.shouldUseScaleFactor) blockTopPos *= zoomValue; // apparently parallax equation everyone uses var percentageY = 0.5; var speedPoint = 10; var speed = speedFactor ? t_animationExt__getParallaxSpeed(speedFactor, -speedPoint, speedPoint) : 0; var positionY = t_animationExt__getParallaxPosition(speed, percentageY); return { el: scrollParallaxEl, elHeight: blockHeight, elTopPos: blockTopPos, elBottomPos: blockHeight + blockTopPos, posY: positionY, speed: speed, isAboveParallax: isAboveParallax, }; });}/** * create object with necessary params to create and animate parallax * * @param {ObjecForCreatingParallax[]} scrollParallaxOptions */function t_animationExt__animateParallaxByScroll(scrollParallaxOptions) { var viewportHeight = document.documentElement.clientHeight; var viewportTopPos = window.pageYOffset; var viewportBottomPos = viewportTopPos + viewportHeight; scrollParallaxOptions.forEach(function (opt) { var isElementInsideViewport = opt.elTopPos < viewportBottomPos && opt.elBottomPos > viewportTopPos && opt.elBottomPos <= viewportBottomPos && opt.elTopPos >= viewportTopPos; if (!opt.isAboveParallax && !isElementInsideViewport) return; var percentageY = (viewportBottomPos - opt.elTopPos) / (opt.elHeight + viewportHeight); var updatePositionY = t_animationExt__getParallaxPosition(opt.speed, percentageY); if (!opt.isAboveParallax) { opt.posY = updatePositionY; opt.isAboveParallax = true; } updatePositionY -= opt.posY; opt.el.style.transform = 'translateY(' + updatePositionY + 'px)'; });}//============== ending of old fashion way to create parallax animation =============///** * get correct trigger offsets in autoscale mode, in case of use Firefox/Opera * * @param {HTMLElement} el * @returns {number} - difference between top position of parent and scaled children */function t_animationExt__calcScaledDiff(el) { var scaledWrapper = el.querySelector('.tn-atom__scale-wrapper'); if (window.isOnlyScalable && scaledWrapper && el) { var elTopPos = el.getBoundingClientRect().top; var scaleTopPos = scaledWrapper.getBoundingClientRect().top; var difference = elTopPos - scaleTopPos; el.setAttribute('data-scaled-diff', difference.toString()); } var diff = el.getAttribute('data-scaled-diff') || '0'; return parseInt(diff, 10);}