$(function(){ var $homeSlider = $('#homeSlider'); var autoplayTick = null; var SLIDER_AUTOPLAY_SPEED = 7000; var isAutoplay = false; var isTransition = false; var swipeType = 'swiperight'; function startAutoplay() { if ( autoplayTick ) { clearInterval(autoplayTick); autoplayTick = null; } autoplayTick = setInterval(function(){ showNextSlide(); }, SLIDER_AUTOPLAY_SPEED); } function stopAutoplay() { if ( autoplayTick ) { clearInterval(autoplayTick); autoplayTick = null; } $('.home-slider-indices .bar').stop().css('width', 0); $('.home-slider-indices li.active .bar').stop().css('width', '100%'); } function showPrevSlide() { swipeType = 'swiperight'; $homeSlider.attr('data-swipe', swipeType); var currentSlide = $('.slide.current', $homeSlider); var prevSlide = currentSlide.prev(); if ( ! prevSlide[0]) { prevSlide = $('.slide:last-child', $homeSlider); } showSlide(prevSlide); } function showNextSlide() { swipeType = 'swipeleft'; $homeSlider.attr('data-swipe', swipeType); var currentSlide = $('.slide.current', $homeSlider); var nextSlide = currentSlide.next(); if ( ! nextSlide[0]) { nextSlide = $('.slide:eq(0)', $homeSlider); } showSlide(nextSlide); } function showSlide(nextSlide) { var currentSlide = $('.slide.current', $homeSlider); nextSlide.removeClass('active'); $('.home-slider-indices .bar').stop().css('width', 0); var index = $('.home-slider-indices a[href="#' + nextSlide.attr('id') + '"]').closest('li'); index.addClass('active').siblings().removeClass('active'); isTransition = true; currentSlide.addClass('out-start'); setTimeout(function(){ currentSlide.addClass('out').css('z-index', 3); currentSlide.siblings().removeClass('active').css('z-index', 1); nextSlide.css('z-index', 2).addClass('active').removeClass('out out-start'); currentSlide.removeClass('current'); nextSlide.addClass('current'); }, 100); setTimeout(function(){ isTransition = false; }, 500); if ( isAutoplay ) { $('.bar', index).stop().css('width', 0).animate({'width': '100%'}, SLIDER_AUTOPLAY_SPEED, 'linear'); startAutoplay(); } else { $('.bar', index).stop().css('width', '100%'); } var video = $('.video-bg', nextSlide); if ( video[0]) { currentVideo = video[0]; video[0].currentTime = 0; video[0].play(); } } $('.home-slider-indices a.index').on('click', function(e){ e.preventDefault(); if ( isTransition ) return; if ($(this).closest('li').hasClass('active')) return; var slideId = $(this).attr('href'); showSlide($('.slide' + slideId)); }); $('.indices .btn-pause').on('click', function(e){ e.preventDefault(); if ( isTransition ) return; if (isAutoplay) { isAutoplay = false; $('.indices .btn-pause').addClass('play'); stopAutoplay(); } else { isAutoplay = true; $('.indices .btn-pause').removeClass('play'); showNextSlide(); } }); function checkVideoSize() { var windowWidth = $(window).innerWidth(); var windowHeight = $(window).innerHeight(); $('.video-bg', $homeSlider).each(function(){ var videoWidth = $(this).attr('data-width'); var videoHeight = $(this).attr('data-height'); var scaleX = windowWidth / videoWidth; var scaleY = windowHeight / videoHeight; var scale = Math.max(scaleX, scaleY); var width = videoWidth * scale; var height = videoHeight * scale; $(this).css({'width':width, 'height':height}); }); } $(window).on('scroll', function(){ var scrollTop = $(window).scrollTop(); var windowHeight = $(window).innerHeight(); if ( scrollTop > windowHeight / 2) { $homeSlider.addClass('scrolled'); } else { $homeSlider.removeClass('scrolled'); } }); $(window).on('resize', function(){ checkVideoSize(); }).trigger('resize'); $(window).on('ds-loaded', function(e){ $('.slide:eq(0)', $homeSlider).addClass('active current'); isAutoplay = true; showSlide($('.slide:eq(0)', $homeSlider)); checkVideoSize(); startAutoplay(); }); var mc = new Hammer($homeSlider[0]); mc.get('swipe').set({ direction: Hammer.DIRECTION_HORIZONTAL }); mc.on("swipeleft swiperight", function(e) { if ( Math.abs(e.deltaX) < 30) return; if ( e.type == "swiperight") { showPrevSlide(); } else { showNextSlide(); } }); });