how-it-works.js 7.24 KB
$(document).bind('ready ajaxComplete', function() {
    $(window).on("resize load", function() {
         // Initialize ScrollMagic
        var controller = new ScrollMagic.Controller();
        currentItem = this;

        var parallaxTimeline = new TimelineMax();
        var parallaxItem = $('.hero-parallax');

        if ($(window).width() > 1480) {
            parallaxTimeline.fromTo(parallaxItem, 1, {
                x: 0,

            }, {
                x: 20,
                ease: Power2.easeInOut
            })
        }
        if ($(window).width() < 1480) {
            parallaxTimeline.fromTo(parallaxItem, 1, {
                x: 0,

            }, {
                x: 20,
                ease: Power2.easeInOut
            })
        }

        var scene = new ScrollMagic.Scene({
            triggerElement: '.hiw-hero',
            duration: '80%',

        })
            .setTween(parallaxTimeline)
            .addTo(controller);

    })

    $.scrollify.destroy();
    $.scrollify.disable();

    //HERO LOAD ANIMATION
    var timelineMenu = new TimelineLite()

    var fadeElement = $(".fadeIn__hiw, .hiw-list-item:first");

    timelineMenu.staggerFromTo(fadeElement, .5, {
        opacity: 0,
        y: 50,
        ease: Power4.easeOut
    }, {
        opacity: 1,
        y: 0,
        ease: Power4.easeOut
    }, .070)

    timelineMenu.pause();

    function showSlider() {
        timelineMenu.play()
     }
    setTimeout(showSlider, 200);


    //FEATURE ITEMS ANIMATION
    var timelineFeat = new TimelineLite()

    var feats = $(".hiw-list-item:not(:first)");

    timelineFeat.staggerFromTo(feats, .5, {
        opacity: 0,
        y: 50,
        ease: Power4.easeOut
    }, {
        opacity: 1,
        y: 0,
        ease: Power4.easeOut
    }, .070)

    timelineFeat.pause();


    var lastScrollTop = 0;
    var noScroll = false;
    var scrollHandler = function() {
        if ($('.hiw-list-item.open').length > 0) {


            var hideHeight = $('.hiw-list-item.open').height() + $('.hiw-list-item.open').position().top - parseInt($('.hiw-close').css('top'))


            if ($(window).scrollTop() > hideHeight-$('.hiw-close').height() || $(window).scrollTop()<$('.hiw-list-item.open').position().top+$('.hiw-close').height()-180) {
                $('.hiw-close').hide()
            } else {
                $('.hiw-close').show()
            }
        }
        if ($(window).width() <= 1023) {
            if ($(window).scrollTop() > $('.nav-home').height()) {
                $('.main-navi').css('position', 'fixed')
                $('.main-navi').css('top', '0px')
            } else {
                $('.main-navi').css('position', 'absolute')
                $('.main-navi').css('top', '80px')
            }
        }


        //OPEN NEXT/PREV FEATURE BOX ON SCROLL


        if (!noScroll) {
            if($(window).width()>=1024) {
                var st = $(this).scrollTop();
                //SCROLL DOWN
                if (st > lastScrollTop) {

                    if ($('.hiw-list-item.open').length > 0) {

                        if ($(window).scrollTop() > $('.hiw-list-item.open').height() + $('.hiw-list-item.open').offset().top) {

                            var target = $('.hiw-list-item.open').removeClass('open')

                            $('.hiw-close').hide()

                            setTimeout(function () {
                                TweenLite.to(window, .1, {
                                    scrollTo: {
                                        y: target.next().offset().top - target.next().height() + $('.nav-home').height()
                                    }
                                });
                                $('.hoverEffect').removeClass('hoverEffect')
                                target.next().find('.hiw-item-title').addClass('hoverEffect');
                            }, 100)

                        }
                    }
                    //SCROLL UP
                } else {

                    if ($('.hiw-list-item.open').length > 0) {

                        if ($(window).scrollTop() < $('.hiw-list-item.open').offset().top - $('.nav-home').height()) {

                            var target = $('.hiw-list-item.open').removeClass('open')

                            $('.hiw-close').hide()

                            setTimeout(function () {
                                 TweenLite.to(window, .1, {
                                    scrollTo: {
                                        y: target.prev().offset().top - $('.nav-home').height() - 20
                                    }
                                });
                                $('.hoverEffect').removeClass('hoverEffect')
                                target.prev().find('.hiw-item-title').addClass('hoverEffect');
                            }, 100)

                        }
                    }
                }
                lastScrollTop = st;

            }

            var hero = $('.hiw-hero')
            if (hero.length == 0) {
                hero = $('.home-hero')
            }
            if($(window).scrollTop()>hero.height() && !$('body').hasClass('attivazione-restart')) {
                $('.nav-link-ct').removeClass('hide')
                timelineFeat.play();
            } else {
                $('.nav-link-ct').addClass('hide')
            }

        }
    }


    $(document).scroll(scrollHandler)

    repower.body.on('click', '.hiw-list-item', function() {

         var offset = -$('.nav-home').height();
        if ($(window).width() <= 768)
            offset = -$('.main-navi').height()

        if (!$(this).hasClass('open')) {
            $('.hiw-list-item').removeClass('open')
            $(this).addClass('open')
            $('.hiw-close').show()
            $(this).velocity('scroll', {

                duration: 400,
                offset: offset,
                complete: function() {
                    noScroll = false;
                    lastScrollTop = $(document).scrollTop()
                }
            });
        }

    })

    $('.hiw-list-item').each(function(i) {

        var nextTitle = $($('.hiw-list-item').get(i + 1)).find('.feat-right h3').html()
        if (typeof nextTitle !== "undefined") {
            $(this).find('.next-item').append('<figure> <img src="img/freccia.svg"> </figure>' + nextTitle)

        }

    })


    repower.body.on('click', '.next-item', function(e) {
        e.stopPropagation()
        e.preventDefault()
        var p = $(this).parents('.hiw-list-item')

        p.removeClass('open')
        //$(document).off('scroll')
        noScroll = true;
        p.next().trigger('click')
    })

    repower.body.on('click', '.hiw-close', function(e) {
        e.stopPropagation();
        $('.hiw-list-item.open').removeClass('open')
        $(this).hide()
    })

    repower.body.on('click', '.down-arrow', function(e) {
        var offset = -140;
        if ($(window).width() <= 375)
            offset = -144
        $('.hiw-features').velocity('scroll', {
            duration: 400,
            offset: offset,
        });
    })
})