var controller = new ScrollMagic.Controller(); $('.menusections li').each(function(i) { var tg = $(this).children('a').attr('href'); var id = '#'+$(this).attr('id'); var ht = $(tg).height(); new ScrollMagic.Scene({ triggerElement: tg, triggerHook: 'onLeave', offset: -100, duration: ht }) .setClassToggle(id, "active") // add class toggle .addTo(controller); }); $('.menusections li a').on('click', function() { $.scrollTo($(this).attr('href'), 1000, { offset: -90, easing: 'swing'}); return false; })