Last active
December 21, 2016 23:05
-
-
Save kbytin/a19324c30d9fb4612420a7a7f99a1b5a to your computer and use it in GitHub Desktop.
Lory slider nav with thumbs
This file contains hidden or 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
import {lory} from 'lory.js'; | |
document.addEventListener('DOMContentLoaded', () => { | |
const productSlider = document.querySelector('.product-page-slider'); | |
const productSliderCount = productSlider.querySelectorAll('.slide').length; | |
const productNavContainer = productSlider.querySelector('.product-page-slider__nav'); | |
const productNavItem = document.createElement('li') | |
const productNavItemInner = document.createElement('div') | |
function handleSliderNav(e) { | |
if (e.type === 'before.lory.init') { | |
for (let i = 0; i < productSliderCount; i++) { | |
let clone = productNavItem.cloneNode(); | |
let backgroundImg = productSlider.querySelector('.js_slides').children[i].style.backgroundImage; | |
let cloneInner = productNavItemInner.cloneNode(); | |
cloneInner.style.backgroundImage = backgroundImg; | |
clone.appendChild(cloneInner); | |
productNavContainer.appendChild(clone); | |
} | |
productNavContainer.childNodes[0].classList.add('is-active'); | |
} | |
if (e.type === 'after.lory.init') { | |
for (let i = 0; i < productSliderCount; i++) { | |
productNavContainer.childNodes[i].addEventListener('click', function(e){ | |
renderProductSlider.slideTo(Array.prototype.indexOf.call(productNavContainer.childNodes, e.target)); | |
}); | |
} | |
} | |
if (e.type === 'after.lory.slide') { | |
for (let i = 0; i < productSliderCount; i++) { | |
productNavContainer.childNodes[i].classList.remove('is-active'); | |
} | |
productNavContainer.childNodes[e.detail.currentSlide - 1].classList.add('is-active'); | |
} | |
} | |
productSlider.addEventListener('before.lory.init', handleSliderNav); | |
productSlider.addEventListener('after.lory.init', handleSliderNav); | |
productSlider.addEventListener('after.lory.slide', handleSliderNav); | |
const renderProductSlider = lory(productSlider, { | |
enableMouseEvents: true, | |
infinite: true, | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment