Hello,
i try to add a previous and next button to the slides.
So far i have this code, which works nearly like it should, but if I the direction is to the top and I click on the "prev" buttons, all the slides jumps over the page to a new direction, and then it works.
Is it possible, to just reverse the function on click and the other way round?
Here is my current code:
var imgs = gsap.utils.toArray(".slider-item"),
noImgs = imgs.length,
next = 2, // time to change
endless,
var img00,
img01,
img02,
img03;
img04;
var start = 0;
var switchJump = 0;
var vars = {};
function crossfade(){
$(imgs).each(function( index ) {
if (index == 0) { img00 = imgs[0];}
if (index == 1) { img01 = imgs[1];}
if (index == 2) { img02 = imgs[2];}
if (index == 3) { img03 = imgs[3];}
if (index >= 4) { img04 = imgs[index];}
});
if (start == 0) {
var action = gsap.timeline()
.to(img00, {x:20, y:0, duration:1, opacity: .5})
.set(img00, {zIndex:1},0.2)
.set(img01, {zIndex:2},0.1)
.to(img01, {x:60, duration:1, opacity: 1 },0.1)
.to(img01, {y:100, duration:1 },0.3)
.to(img02, {x:20, y:200, duration:1, opacity:.5},0.1)
.to(img03, {x:0, y:-125, zIndex: 0, duration:1, opacity:0},0)
.set(img03, {x:20, y:300})
.to(img04, {x:0, y:-125, zIndex: 0, duration:1, opacity:0},0)
.set(img04, {x:20, y:300})
.to('#card-slider', {autoAlpha:1})
// imgs.push( imgs.shift());
} else {
// REVERSE
var action = gsap.timeline()
.to(img00, {x:20, y:200, duration:1, opacity: .5})
.set(img00, {zIndex:1},0.2)
.set(img01, {zIndex:2},0.1)
.to(img01, {x:60, duration:1, opacity: 1 },0.1)
.to(img01, {y:100, duration:1 },0.3)
.to(img02, {x:20, y:0, duration:1, opacity:.5},0.1)
.to(img03, {x:20, y:300, zIndex: 0, duration:1, opacity:0},0)
.set(img03, {x:0, y:-125})
.to(img04, {x:20, y:300, zIndex: 0, duration:1, opacity:0},0)
.set(img04, {x:0, y:-125})
.to('#card-slider', {autoAlpha:1})
}
imgs.push( imgs.shift());
endless = gsap.delayedCall(next, crossfade);
}
// start the crossfade
endless = gsap.delayedCall(0, crossfade);
// hover =================
var hover = document.querySelector("#card-slider");
$('.nextSlide').click(function() {
endless.kill();
start = 0;
endless = gsap.delayedCall(0, crossfade);
});
$('.prevSlide').click(function() {
endless.kill();
start = 1;
endless = gsap.delayedCall(0, crossfade);
});
hover.addEventListener("mouseenter", function (){
endless.kill();
});
hover.addEventListener("mouseleave", function (){
endless = gsap.delayedCall(1, crossfade);
});