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

100
Visualizações
Nuxt dragable slider teleports to mouse position upon re-sliding

I've tried to make a dragabe slider at first glance it seems to work fine. But upon sliding it more than once you'll notice it bugs out.

It looks like it teleports to the mouse position on the first mousemovement. I've tried several things but can't wrap my head around it.

My html:

<div class="container">
  <div class="slider-wrapper" v-on:mousemove="slide()">
    <div class="slider">
      <div class="slide"></div>
      <div class="slide"></div>
      <div class="slide"></div>
      <div class="slide"></div>
    </div>
  </div>
</div>

My javascript:

export default {
  data() {
    return {
      mouseDown: 0,
      start: undefined,
      position: undefined,
    }
  },
  methods: {
    slide() {
      const vm = this
      document.body.onmousedown = function () {
        vm.start = window.event.clientX
        vm.mouseDown = 1
      }
      document.body.onmouseup = function () {
        vm.mouseDown = 0
      }
      if (this.mouseDown == 1) {
        vm.position = window.event.clientX - vm.start
        document.querySelector('.slider').style.left = vm.position + 'px'
      }
    },
  },
}

I've recreated a demo of my problem.

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

0

Turns out i needed to remember the old reposition before repositioning the slider. To fix this i changed the code as following:

data() {
  return {
    mouseDown: 0,
    start: undefined,
    position: undefined,
    old_pos: 0 // initialize old_pos
  }
},

slide() {
  const vm = this
  document.body.onmousedown = function() {
    vm.start = window.event.clientX
    vm.mouseDown = 1
  }
  document.body.onmouseup = function() {
    vm.old_pos = vm.position + vm.old_pos // Sets old_pos when a new position has been set.
    vm.mouseDown = 0
  }
  if(this.mouseDown == 1) {
    vm.position = window.event.clientX - vm.start
    document.querySelector(".slider").style.left = vm.position + vm.old_pos + "px"; // Adds the old position as starting point of movement.
  }
}

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