Last active
May 1, 2019 16:23
-
-
Save scrawlon/5975812da90656581203c3916b194d0f to your computer and use it in GitHub Desktop.
Swiper JS fullscreen image slider JavaScript
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
<script> | |
window.onload = function() { | |
var swiper = new Swiper('.swiper-container', { | |
loop: true, | |
navigation: { | |
nextEl: '.swiper-button-next', | |
prevEl: '.swiper-button-prev', | |
}, | |
pagination: { | |
el: '.swiper-pagination' | |
}, | |
slidesPerView: 3, | |
spaceBetween: 10 | |
}); | |
var swiperSlides = Array.from(swiper.slides); | |
swiperSlides.forEach(function(slide) { | |
openFullscreenSliderHandler(slide); | |
closeFullscreenSliderHandler(slide); | |
}); | |
function openFullscreenSliderHandler(slide) { | |
var slideImage = slide.querySelector('img'); | |
slideImage.addEventListener('click', function() { | |
var slideNumber = slide.dataset.swiperSlideIndex; | |
openFullscreenSwiper(slideNumber); | |
}); | |
} | |
function openFullscreenSwiper(slideNumber) { | |
swiper.el.classList.add('fullscreen'); | |
swiper.params.slidesPerView = 1; | |
swiper.update(); | |
swiper.slideToLoop(parseInt(slideNumber, 10), 0); | |
} | |
function closeFullscreenSliderHandler(slide) { | |
var slideNumber = slide.dataset.swiperSlideIndex; | |
var backdrop = document.createElement('div'); | |
var closeButton = document.createElement('div'); | |
slide.appendChild(backdrop); | |
slide.appendChild(closeButton); | |
backdrop.classList.add('backdrop'); | |
closeButton.classList.add('close-button'); | |
closeButton.innerHTML = 'x'; | |
backdrop.addEventListener('click', function() { | |
closeFullscreenSwiper(slideNumber); | |
}); | |
closeButton.addEventListener('click', function() { | |
closeFullscreenSwiper(slideNumber); | |
}); | |
} | |
function closeFullscreenSwiper(slideNumber) { | |
swiper.el.classList.remove('fullscreen'); | |
swiper.params.slidesPerView = 3; | |
swiper.update(); | |
swiper.slideToLoop(parseInt(slideNumber, 10), 0); | |
} | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment