D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
home
/
ipuco
/
www
/
js
/
Filename :
timeline.js
back
Copy
(function() { // VARIABLES const timeline = document.querySelector(".rs-timeline-2 ol"), elH = document.querySelectorAll(".rs-timeline-2 li > div"), arrows = document.querySelectorAll(".rs-timeline-2 .arrows .arrow"), arrowPrev = document.querySelector(".rs-timeline-2 .arrows .arrow__prev"), arrowNext = document.querySelector(".rs-timeline-2 .arrows .arrow__next"), firstItem = document.querySelector(".rs-timeline-2 li:first-child"), lastItem = document.querySelector(".rs-timeline-2 li:last-child"), xScrolling = 280, disabledClass = "disabled"; // START window.addEventListener("load", init); function init() { setEqualHeights(elH); animateTl(xScrolling, arrows, timeline); setSwipeFn(timeline, arrowPrev, arrowNext); } // SET EQUAL HEIGHTS function setEqualHeights(el) { let counter = 0; for (let i = 0; i < el.length; i++) { const singleHeight = el[i].offsetHeight; if (counter < singleHeight) { counter = singleHeight; } } for (let i = 0; i < el.length; i++) { el[i].style.height = `${counter}px`; } } // CHECK IF AN ELEMENT IS IN VIEWPORT // http://stackoverflow.com/questions/123999/how-to-tell-if-a-dom-element-is-visible-in-the-current-viewport function isElementInViewport(el) { const rect = el.getBoundingClientRect(); return ( rect.top >= 0 && rect.left >= 0 && rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) && rect.right <= (window.innerWidth || document.documentElement.clientWidth) ); } // SET STATE OF PREV/NEXT ARROWS function setBtnState(el, flag = true) { if (flag) { el.classList.add(disabledClass); } else { if (el.classList.contains(disabledClass)) { el.classList.remove(disabledClass); } el.disabled = false; } } // ANIMATE TIMELINE function animateTl(scrolling, el, tl) { let counter = 0; for (let i = 0; i < el.length; i++) { el[i].addEventListener("click", function() { if (!arrowPrev.disabled) { arrowPrev.disabled = true; } if (!arrowNext.disabled) { arrowNext.disabled = true; } const sign = (this.classList.contains("arrow__prev")) ? "" : "-"; if (counter === 0) { tl.style.transform = `translateX(-${scrolling}px)`; } else { const tlStyle = getComputedStyle(tl); // add more browser prefixes if needed here const tlTransform = tlStyle.getPropertyValue("-webkit-transform") || tlStyle.getPropertyValue("transform"); const values = parseInt(tlTransform.split(",")[4]) + parseInt(`${sign}${scrolling}`); tl.style.transform = `translateX(${values}px)`; } setTimeout(() => { isElementInViewport(firstItem) ? setBtnState(arrowPrev) : setBtnState(arrowPrev, false); isElementInViewport(lastItem) ? setBtnState(arrowNext) : setBtnState(arrowNext, false); }, 1100); counter++; }); } } // ADD SWIPE SUPPORT FOR TOUCH DEVICES function setSwipeFn(tl, prev, next) { const hammer = new Hammer(tl); hammer.on("swipeleft", () => next.click()); hammer.on("swiperight", () => prev.click()); } })();