Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- define(
- [
- 'jquery'
- ], function ($) {
- var heroElements = $('.js-hero-scrolling');
- var ticking = false;
- var lastScrollY = $(window).scrollTop();
- function onResize() {
- updateElements();
- lastScrollY = $(window).scrollTop();
- }
- function onScroll(evt) {
- if (!ticking) {
- ticking = true;
- requestAnimFrame(updateElements);
- lastScrollY = $(window).scrollTop();
- }
- }
- function updateElements() {
- if (heroElements !== null) {
- heroElements.each(function (){
- var heroHeight = $(this).height() + parseInt($(this).css('padding-top').replace(/[^-\d\.]/g, '')) + parseInt($(this).css('padding-bottom').replace(/[^-\d\.]/g, ''));
- if( lastScrollY > 0 && lastScrollY <= (heroHeight / 2)){
- var pos = parseInt(lastScrollY * 100 / (heroHeight / 2) );
- $(this).css('background-position', '50% ' + pos + "%");
- }else {
- if(lastScrollY > (heroHeight / 2)){
- $(this).css('background-position', '50% 100%');
- }else {
- $(this).css('background-position', '50% 0');
- }
- }
- });
- }
- ticking = false;
- }
- // shim layer with setTimeout fallback
- window.requestAnimFrame = (function() {
- return window.requestAnimationFrame ||
- window.webkitRequestAnimationFrame ||
- window.mozRequestAnimationFrame ||
- window.oRequestAnimationFrame ||
- window.msRequestAnimationFrame ||
- function(callback) {
- window.setTimeout(callback, 1000 / 60);
- };
- })();
- function initScroll() {
- updateElements();
- lastScrollY = $(window).scrollTop();
- }
- window.addEventListener('resize', onResize, false);
- window.addEventListener('scroll', onScroll, false);
- return {
- init: initScroll
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement