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

137
Visualizações
Convert 45 to 1 and 345 to 2

I'm building a carousel with a swiping limit exceeding both left and right of 45px on each item has 300px width I wonder How to convert 45 to 1 index and 345 to 2 index. I have a total of 3 items for the carousel.

I have the following formula for converting it to 2 indexes however not on the first index

If the current position is on 100px which is greater than 45 it should still result 1 index

(345 / 345 * 2) a result of 2

(45 / 345 * 2) a result of 0.2608695652173913

I'm trying to make that one line

  let move = current_translate - previous_translate_item;

  if (move < -345) current_index += 1;
  if (move < -40) current_index += 1;
  if (move > 40) current_index -= 1;
  if (move > 345) current_index -= 1;

Carousel for mobile screens

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Try using this one:

let val_converter = val => -Math.sign(val)*Math.floor((Math.abs(val)-40)/300 +1);

console.log(val_converter(-645));   //  3
console.log(val_converter(-644));   //  2
console.log(val_converter(-345));   //  2
console.log(val_converter(-344));   //  1
console.log(val_converter(- 40));   //  1
console.log(val_converter(- 39));   //  0
console.log(val_converter(   0));   //  0
console.log(val_converter(  39));   //  0
console.log(val_converter(  40));   // -1
console.log(val_converter( 344));   // -1
console.log(val_converter( 345));   // -2
console.log(val_converter( 644));   // -2
console.log(val_converter( 645));   // -3

In your specific source code, it would be something like this:

let val_converter = val => -Math.sign(val)*Math.floor((Math.abs(val)-40)/300+1);

let move = current_translate - previous_translate_item;

current_index += val_converter(move);
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