const list = document.querySelector('nav')
const items = Array.from(list.children)
const prev = document.querySelector('#prev')
const next = document.querySelector('#next')
const width = 400
const height = 200
const {
abs
} = Math
// Get last item and place it next to first
// list.insertAdjacentElement('afterbegin', list.lastElementChild)
// Move one back
// let transform = -width
let transform = 0
list.style.transform = `translate3d(${transform}px, 0,0)`
let index = 0
let lastIndex = 0
document.addEventListener('mousedown', ({
target
}) => {
let dir
if (target === next) {
if (index >= items.length - 1) {
index = 0
} else {
index++
}
dir = true
}
if (target === prev) {
if (index === 0) {
index = items.length - 1
} else {
index--
}
dir = false
}
if (target.closest('#direct')) {
index = target.id
}
let range
const isRight = index > lastIndex
if (isRight) {
range = index - lastIndex
} else {
range = index - lastIndex
}
console.log(index, lastIndex, range, isRight)
console.log('T', transform)
if (dir) {
transform -= range * width
} else {
transform -= range * width
}
list.style.transform = `translate3d(${transform}px, 0,0)`
lastIndex = index
})
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.