Created
July 24, 2020 13:10
-
-
Save webag/27a83c1f7730855224ae8199000ac0ea to your computer and use it in GitHub Desktop.
Прогресс слайдера, slider progress, stories, сторис
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(function($){ | |
const duration = 5; | |
const interval = 10; | |
const stepsBars = $('.intro-step'); | |
let isPaused = false; | |
let percentTime, step, tick, scaleWidth; | |
let currentIndex = 0; | |
let introSlider = $('.intro-slider').flickity({ | |
cellAlign: 'left', | |
pageDots: false, | |
prevNextButtons: false, | |
contain: true, | |
imagesLoaded: true, | |
dragThreshold: 5, | |
adaptiveHeight: true, | |
cellSelector: '.intro-slider__slide', | |
wrapAround: true | |
}); | |
startProgressbar(); | |
introSlider.on('change.flickity', function() { | |
currentIndex = $('.intro-slider__slide.is-selected').index(); | |
startProgressbar(); | |
}); | |
// Pause control | |
introSlider.on('pointerDown.flickity', function(event,pointer) { | |
isPaused = true; | |
}); | |
introSlider.on('pointerUp.flickity', function(event,pointer) { | |
isPaused = false; | |
}); | |
// Main function | |
function startProgressbar() { | |
console.log(currentIndex); | |
resetProgressbar(currentIndex); | |
percentTime = 0; | |
isPaused = false; | |
tick = window.setInterval(increase, interval); | |
} | |
function increase() { | |
const bar = stepsBars.eq(currentIndex).find('.intro-step__progress'); | |
if (!isPaused) { | |
step = (duration * 1000) / interval; | |
percentTime += 100 / step; | |
scaleWidth = percentTime / 100; | |
bar.css('transform','scale3d('+scaleWidth+',1,1)'); | |
if (percentTime >= 100) { | |
introSlider.flickity('next'); | |
startProgressbar(); | |
} | |
} | |
} | |
function resetProgressbar() { | |
const bar = stepsBars.eq(currentIndex).find('.intro-step__progress'); | |
bar.css('transform','scale3d(0,1,1)'); | |
stepsBars.eq(currentIndex).nextAll(stepsBars).find('.intro-step__progress').css('transform','scale3d(0,1,1)'); | |
stepsBars.eq(currentIndex).prevAll(stepsBars).find('.intro-step__progress').css('transform','scale3d(1,1,1)'); | |
clearTimeout(tick); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment