Created
August 8, 2017 14:34
-
-
Save tinothepro/6d9b1fc885079e6ff628745d2d6a4790 to your computer and use it in GitHub Desktop.
Turn MyBookTable shortcode results into carousel
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
(function () { | |
'use strict'; | |
if (window.innerWidth >= 1024) { | |
const bookSet = document.getElementsByClassName('mbt-book-archive-books')[0]; | |
const books = bookSet.getElementsByClassName('mbt-book'); | |
var bookList = []; | |
//convert books to array called bookList | |
for (let i = 0; i < books.length; i++) { | |
bookList.push(books[i]); | |
} | |
const diff = bookList.length - 7; | |
let it = 0; | |
const bookSection = document.querySelector('.book-set .wrap'); | |
bookList.map((book) => { | |
book.style.left = 0; | |
}); | |
const carousel = () => { | |
if (diff > it) { | |
for (let i = 0; i < bookList.length; i++) { | |
let pos = parseInt(bookList[i].style.left); | |
bookList[i].style.left = (pos - 149) + 'px'; | |
} | |
it++; | |
} else if (diff === it) { | |
for (let j = 0; j < bookList.length; j++) { | |
bookList[j].style.left = 0; | |
} | |
it = 0; | |
} | |
}; | |
setInterval(carousel, 3500); | |
const prev = () => { | |
if (diff >= it && it > 0) { | |
for (let i = 0; i < bookList.length; i++) { | |
let pos = parseInt(bookList[i].style.left); | |
bookList[i].style.left = (pos + 149) + 'px'; | |
} | |
it--; | |
} | |
}; | |
const next = () => { | |
if (diff > it) { | |
for (let i = 0; i < bookList.length; i++) { | |
let pos = parseInt(bookList[i].style.left); | |
bookList[i].style.left = (pos - 149) + 'px'; | |
} | |
it++; | |
} | |
}; | |
const createNav = () => { | |
var leftArr = document.createElement('div'); | |
leftArr.setAttribute('id', 'leftArr'); | |
var rightArr = document.createElement('div'); | |
rightArr.setAttribute('id', 'rightArr'); | |
leftArr.addEventListener('click', prev); | |
rightArr.addEventListener('click', next); | |
bookSection.appendChild(leftArr); | |
bookSection.appendChild(rightArr); | |
}; | |
window.addEventListener('load', createNav); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment