3
I’m doing a slider and I need that when the images finish passing, they start again but I’m not getting it. Someone could help?
JS
$(function(){
// Buttons Hover Effect
buttonsHover();
function buttonsHover(){
var buttons = $('.buttons');
var container = $('#container');
container.hover(function(){
buttons.fadeIn();
}, function(){
buttons.fadeOut();
});
}
/* ----------------------------------------------------------------------------------------------------------------------------------------- */
// slide
var numImages = 0;
var currentImage = 1;
var totalWidth = 0;
var galleryUlLi = $('.gallery-ul li');
var galleryUl = $('.gallery-ul');
var leftButton = $('.left_b');
var rightButton = $('.right_b');
var interval = 0;
hideButtons();
loop();
// images_container li class with a function to add values for each numImages and totalWidth variables
galleryUlLi.each(function(){
numImages++;
totalWidth += 1200;
});
// images_container class with the css width value of the images
galleryUl.css('width', totalWidth + 'px');
// Right button click function
rightButton.click(function(){
moveLeft();
hideButtons();
clearLoop();
loop();
});
// Left button click function
leftButton.click(function(){
moveRight();
hideButtons();
clearLoop();
loop();
});
// Move Left function
function moveLeft(){
if(currentImage < numImages){
galleryUl.animate({'margin-left': '-=1200px'}, 1000);
currentImage++;
hideButtons();
}
}
// Move Right function
function moveRight(){
if(currentImage > 1){
galleryUl.animate({'margin-left': '+=1200px'}, 1000);
currentImage--;
hideButtons();
}
}
// function hide buttons
function hideButtons(){
if(currentImage === 1){
leftButton.hide();
}
else{
leftButton.show();
}
if(currentImage === numImages){
rightButton.hide();
}
else{
rightButton.show();
}
}
function loop(){
interval = setInterval(moveLeft, 3000);
}
function clearLoop(){
clearInterval(interval);
}
}); // End of main function
The code even shows some error or something?
– KhaosDoctor
No he just finishes the looping and doesn’t start again.
– Leonardo Silva
Have you checked if the moveleft function is running? Try placing a console.log in the loop
– KhaosDoctor