Answer the question
In order to leave comments, you need to log in
Implementation of a mini slider - why does the arrow work correctly in one case, but not in the other?
Hello everyone
In general, I'm making my own mini slider, and at this moment I'm just stuck, I don't understand what the error is
. The fact is that when you click on the back button, the slider changes the active element, here is its code
$(".meal-small-slider-arrow--prev").on('click', function(){
$(".meal-small-slider__item").each(function(i,v){
if($(this).hasClass("meal-small-slider__item--is-active")){
if(i == 0){
return true;
}else{
let new_index = i - 1;
$(`.meal-small-slider__item`).removeClass("meal-small-slider__item--is-active")
$(`.meal-small-slider__item:eq(${new_index})`).addClass("meal-small-slider__item--is-active")
}
}
})
})
$(".meal-small-slider-arrow--next").on('click', function(){
$(".meal-small-slider__item").each(function(i,v){
if($(this).hasClass("meal-small-slider__item--is-active")){
if(i == 4){
return true;
}else{
console.log(i)
let new_index = ++i;
$(`.meal-small-slider__item`).removeClass("meal-small-slider__item--is-active")
$(`.meal-small-slider__item:eq(${new_index})`).addClass("meal-small-slider__item--is-active")
}
}
})
})
Answer the question
In order to leave comments, you need to log in
The solution was to stop the execution of the condition through return false
$(".meal-small-slider-arrow--next").on('click', function(){
$(".meal-small-slider__item").each(function(i,v){
if($(this).hasClass("meal-small-slider__item--is-active")){
if(i == 4){
return true;
}else{
console.log(i)
let new_index = ++i;
$(`.meal-small-slider__item`).removeClass("meal-small-slider__item--is-active")
$(`.meal-small-slider__item:eq(${new_index})`).addClass("meal-small-slider__item--is-active")
return false;
}
}
})
})
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question