Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

203
Visualizações
How to loop an addition and substraction on a base of 3 in query?

I have a simpple Slider with 3 slides. Now everything works, but if I press more than 3 times in one direction I get "false" numbers. Mathemathically they are right, but Im aiming for this scenario:

If i click 4 times the button for previous slide, the counter of my numbers should be again 3, not -1. Any way to archive this?

  $( document ).ready(function() {
    
  var current =  $('.slider_number').data("hasso");
  var total = $('.slider_number').data("total")

  $('.slider_next').click(function(){
  ++current;
  console.log(current)
  $(".slider_number").text(current)
});
  $('.slider_prev').click(function(){
  --current;
  console.log(current)
}                                     
);
 });
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Use the circular index formula (i % n + n) % n where n is the length of the array (4 in your case).

use:

current = (current+1 % total + total) % total;

to move forward;

and

current = (current-1 % total + total) % total;

to move backwards.

current will always be a valid index.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda