/    Sign up×
Community /Pin to ProfileBookmark

JavaScript Carousel

I have a clone slide and I want to make the track to jump on the first slide or ‘second slide’ when I get to the slide clone.
Do you have any ideas on how I can do that?
Here is the code: https://codepen.io/raul-rogojan/pen/wVGXVM?editors=0010

to post a comment
JavaScript

18 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumJul 29.2019 — I recommend using the slideshow from the tutorial you posted previously. The code is clear and inserting the clones should be easy.
Copy linkTweet thisAlerts:
@codyhillauthorJul 29.2019 — @Sempervivum#1606886 that's exactly what I did. The problem is that I can't figure out how I can make the track to jump to the first slide once it gets to the last clone.
Copy linkTweet thisAlerts:
@SempervivumJul 29.2019 — I see. IMO the variable names in the tutorial are more meaningful.

Appending the first slide at the end seems to work fine. For skipping to the first slide after the last one has been reached I recommend to use the code from the tutorial.
Copy linkTweet thisAlerts:
@SempervivumJul 29.2019 — Obviously the cyclical sliding is working now. Fine!
Copy linkTweet thisAlerts:
@codyhillauthorJul 29.2019 — @Sempervivum#1606892 The dot's aren't. See my new post
Copy linkTweet thisAlerts:
@KeverJul 29.2019 — Does this work?track.addEventListener('transitionend', ()=> {
if (slides[counter].id === 'last-clone') {
track.style.transition = 'none';
counter = slides.length-counter;
track.style.transform = 'translateX(' + (-slideWidth * counter) + 'px)';
updateDots(dots[dots.length-1], dots[1]);
};
if (slides[counter].id === 'first-clone') {
track.style.transition = 'none';
counter = slides.length-2;
track.style.transform = 'translateX(' + (-slideWidth * counter) + 'px)';
updateDots(dots[0], dots[dots.length-2]);
};
Copy linkTweet thisAlerts:
@codyhillauthorJul 30.2019 — @Kever#1606894 Perfect! Tanks!!!

Do you have any ideas on how I can make the dots update uppon arriving on the clone and not after the transition ends?
Copy linkTweet thisAlerts:
@KeverJul 30.2019 — You'll have to move the code for the update from the transitionend function to a place where it runs immediately. So the click function of the next/prev button or the updatedots function.
function updateDots(currentDot, targetDot) {
currentDot.classList.remove('active-dot');
targetDot.classList.add('active-dot');
<br/>
if (slides[counter].id === 'last-clone') {
updateDots(dots[dots.length-1], dots[1]);
}
if (slides[counter].id === 'first-clone') {
updateDots(dots[0], dots[dots.length-2]);
}
}
Copy linkTweet thisAlerts:
@codyhillauthorJul 31.2019 — @Kever#1606941 I tried and it doesn't work
Copy linkTweet thisAlerts:
@KeverJul 31.2019 — Did you remove the updateDots calls from the transitionend function?

Nevermind. I managed to break it when clicking the buttons rapidly.
Copy linkTweet thisAlerts:
@codyhillauthorAug 01.2019 — @Kever#1607012 Yeah :)) that's a problem. Unfortunately, I'm gonna have to deal with it.

There might be another fix but I don't know how to do it. I could set a time on every slide that needs to pass before allowing the user to go to the next slide. Basically not allow the user to click rapidly on any slide.
Copy linkTweet thisAlerts:
@KeverAug 01.2019 — I cleaned up some repeating code, so you don't have to mind for the updateDots function at several places. Seems to work fine now.const track = document.querySelector('[data-track]');
const nextBtn = document.querySelector('[data-button-next]');
const prevBtn = document.querySelector('[data-button-prev]');
const navDots = document.querySelector('[data-dots-nav]');
const updateTime = 3500;

/******************************************************************/

const slides = Array.from(track.children);
const dots = Array.from(navDots.children);
let counter = 1;
let nextTimer;

//This is the first slide that needs to pushed to the end
const firstSlide = slides[0];
const firstSlideClone = firstSlide.cloneNode(true);
firstSlideClone.setAttribute('id', 'last-clone');

track.appendChild(firstSlideClone);
slides.push(firstSlideClone);

//This is the last slide that needs to be unshifted to the front
const lastSlide = slides[slides.length-2];
const lastSlideClone = lastSlide.cloneNode(true);
lastSlideClone.setAttribute('id', 'first-clone');

track.appendChild(lastSlideClone);
slides.unshift(lastSlideClone);

const slideWidth = slides[0].getBoundingClientRect().width;

const setSlidePosition = (slide, index) =&gt; {
slide.style.transform = 'translateX(' + slideWidth * index + 'px' +')';
};

slides.forEach(setSlidePosition);

track.style.transform = 'translateX(' + (-slideWidth * counter) + 'px)';

//setTimerout function!
window.onload = () =&gt; {
nextTimer = setTimeout(nextImg, updateTime);
};

nextBtn.addEventListener('click', nextImg);
prevBtn.addEventListener('click', prevImg);

function updateDots(currentDot, targetDot) {
currentDot.classList.remove('active-dot');
targetDot.classList.add('active-dot');
}
function nextImg(e) {
if(counter &gt;= slides.length-1) return;
counter++;
updateImg();
};
function prevImg(e) {
if(counter &lt;= 0) return;
counter--;
updateImg();
};

function updateImg() {
track.style.transition = 'transform 400ms ease-in-out';
track.style.transform = 'translateX(' + (-slideWidth * counter) + 'px)';

var currentDot = navDots.querySelector('.active-dot');
var newDot = dots[(dots.length + counter - 1) % dots.length];
updateDots(currentDot, newDot);
// reset the current timer so it doesn't run right after a user initiated update
clearTimeout(nextTimer);
nextTimer = setTimeout(nextImg, updateTime);
}

// Dots caode
navDots.addEventListener('click', e =&gt; {
var clickedDot = e.target.closest('button');
if(clickedDot === null) {
return;
};

var targetIndex = dots.findIndex(dot =&gt; dot === clickedDot);
counter = targetIndex + 1;
updateImg();
});

track.addEventListener('transitionend', ()=&gt; {
if (slides[counter].id === 'last-clone') {
counter = slides.length-counter;
};
if (slides[counter].id === 'first-clone') {
counter = slides.length-2;
};
track.style.transition = 'none';
track.style.transform = 'translateX(' + (-slideWidth * counter) + 'px)';
});
Copy linkTweet thisAlerts:
@codyhillauthorAug 01.2019 — @Kever#1607062 Thanks, man! I looked over it and I will try to replicate it. Just one question tho. What does % dots.length mean?
Copy linkTweet thisAlerts:
@KeverAug 01.2019 — Forgot to mention that I also removed the extra dots from the HTML code.

The % operator returns the integer remainder of dividing.
for(var i=0, s='ABC'; i&lt;9; i++) {
console.log(i, i%s.length, s[i%s.length])
}
Copy linkTweet thisAlerts:
@codyhillauthorAug 02.2019 — @Kever#1607066 Cool! Thx Alot!
Copy linkTweet thisAlerts:
@hanifanzeAug 05.2019 — @RaulRogojan#1606884 Did you remove the updateDots calls from the transitionend function?
Copy linkTweet thisAlerts:
@codyhillauthorAug 05.2019 — @hanifanze#1607201 No, I did not have a chance to take a look into it yet. I am working on a website right now
Copy linkTweet thisAlerts:
@hanifanzeAug 05.2019 — @RaulRogojan#1607202 ok thanks

[url=https://redtube.onl/][color=#000000]Redtube[/color][/url] [url=https://beeg.onl/][color=#000000]Beeg[/color][/url] [url=https://spanktube.vip/spankbang/][color=#000000]Spankbang[/color][/url]
×

Success!

Help @codyhill spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.13,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...