Add automatic scrolling behavior to jam carousel
This commit is contained in:
parent
ba667f717e
commit
081186c8f0
|
@ -1,4 +1,7 @@
|
||||||
function initCarousel(container, durationMS = 0) {
|
function initCarousel(container, options = {}) {
|
||||||
|
const durationMS = options.durationMS ?? 0;
|
||||||
|
const onChange = options.onChange ?? (() => {});
|
||||||
|
|
||||||
const numCarouselItems = container.querySelectorAll('.carousel-item').length;
|
const numCarouselItems = container.querySelectorAll('.carousel-item').length;
|
||||||
const buttonContainer = container.querySelector('.carousel-buttons');
|
const buttonContainer = container.querySelector('.carousel-buttons');
|
||||||
|
|
||||||
|
@ -25,6 +28,8 @@ function initCarousel(container, durationMS = 0) {
|
||||||
buttons[i].classList.add('active');
|
buttons[i].classList.add('active');
|
||||||
|
|
||||||
current = i;
|
current = i;
|
||||||
|
|
||||||
|
onChange(current);
|
||||||
}
|
}
|
||||||
|
|
||||||
function activateNext() {
|
function activateNext() {
|
||||||
|
|
|
@ -71,6 +71,8 @@
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
initCarousel(document.querySelector('.project-carousel'), 10000);
|
initCarousel(document.querySelector('.project-carousel'), {
|
||||||
|
durationMS: 10000,
|
||||||
|
});
|
||||||
</script>
|
</script>
|
||||||
{{ end }}
|
{{ end }}
|
||||||
|
|
|
@ -428,7 +428,15 @@
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
const { next, prev } = initCarousel(document.querySelector('.carousel-container'));
|
const carouselContainer = document.querySelector('.carousel-container');
|
||||||
|
|
||||||
|
const { next, prev } = initCarousel(carouselContainer, {
|
||||||
|
onChange() {
|
||||||
|
if (carouselContainer.getBoundingClientRect().top < 0) {
|
||||||
|
carouselContainer.scrollIntoView({ behavior: 'smooth' });
|
||||||
|
}
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
document.querySelector('.carousel-thinger.next')
|
document.querySelector('.carousel-thinger.next')
|
||||||
.addEventListener('click', () => {
|
.addEventListener('click', () => {
|
||||||
|
|
Loading…
Reference in New Issue