Add automatic scrolling behavior to jam carousel

This commit is contained in:
Ben Visness 2022-08-02 15:14:29 -05:00
parent ba667f717e
commit 081186c8f0
3 changed files with 18 additions and 3 deletions

View File

@ -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() {

View File

@ -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 }}

View File

@ -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', () => {