$(function() { var windowHeight = $(window).outerHeight(); var windowWidth = $(window).outerWidth(); var halfWindowHeight = (windowHeight / 2) * -1; var journey2018Height = $('.journeyinfo--2018').outerHeight(); var offsetHeight = (0.5 * journey2018Height) + 40 - (0.76 * windowHeight); var remainingOffset = (windowHeight + offsetHeight) * -1; var journeyContainerHeight = $('.journey .container').outerHeight(); var proportionNavOfWindow = 0.24 * windowHeight; var extraHeight = windowHeight - proportionNavOfWindow - 40 - parseInt($('html').css('font-size') , 10) * 31.5; $('.journey .container').css('height', journeyContainerHeight + extraHeight); $(window).resize(function() { windowHeight = $(window).outerHeight(); windowWidth = $(window).outerWidth(); halfWindowHeight = (windowHeight / 2) * -1; proportionNavOfWindow = 0.24 * windowHeight; extraHeight = windowHeight - proportionNavOfWindow - 40 - parseInt($('html').css('font-size') , 10) * 31.5; $('.journey .container').css('height', journeyContainerHeight + extraHeight); journey2018Height = $('.journeyinfo--2018').outerHeight(); offsetHeight = (0.5 * journey2018Height) + 40 - (0.76 * windowHeight); remainingOffset = (windowHeight + offsetHeight) * -1; console.log(offsetHeight); setWatchers(); if (windowWidth < 875) { $('.year--active').removeClass('year--active'); $('.journey--active').removeClass('journey--active'); } }) var yearWatcher = scrollMonitor.create($('.year')); var journeyWatcher = scrollMonitor.create($('.journeyinfo')); var journeyWatchers = []; var journeyItems = []; var yearItems = []; function setWatchers() { $('.year').each(function(index) { yearItems[index] = $(this); }) $('.journeyinfo').each(function(index) { journeyItems[index] = $(this); journeyWatchers[index] = scrollMonitor.create(journeyItems[index], {top:offsetHeight, bottom:remainingOffset}); journeyWatchers[index].enterViewport(function() { if (windowWidth >= 875) { $('.journey--active').removeClass('journey--active'); journeyItems[index].addClass('journey--active'); $('.year--active').removeClass('year--active'); yearItems[index].addClass('year--active'); } }); journeyWatchers[index].exitViewport(function() { if (windowWidth >= 875) { journeyItems[index].removeClass('journey--active'); yearItems[index].removeClass('year--active'); } }); }) }; setWatchers(); })