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

306
Visualizações
jQuery - Multiple events, get deltaY for one event

I use two events (scroll and mousewheel).

I would like to get deltaY for mousewheel event to control a slider.

The variable getDelta returns numbers and undefined despite of my condition on mousewheel event.

I would like to get deltaY values outside.

What's wrong please ? Thank you in advance.

$(window).on('scroll mousewheel', function(e) {
    if (e.type == "mousewheel") {
        var getDelta = e.deltaY;
    }

    console.log(getDelta);
}

enter image description here

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

0

you put console log outside if condition.

$(window).on('scroll mousewheel', function(e) {
       var getDelta;
    if (e.type == "mousewheel") {
         getDelta = e.deltaY;
        
    }
    
    if(getDelta)
    {
        console.log(getDelta);

    }else{

     console.log("delta is null");

   }
    

    
}
about 4 years ago · Juan Pablo Isaza Relatório

0

The best way (in my view) to get your things done with deltaY / X is to use event listeners. They can be easily attached and detached - by human action or by some automatic trigger generated by your application / script.

Put the code below on a webpage and check if is OK for you.

    <script>
    // Getting delta trough event listeners
    // then passing them as parameters to be consumed by other function(s)

    function getDelta (e) // ... starting delta events capture
    {
      // We capture here both X and Y delta values (but you can use only deltaY) because
      // some mice may have left and right buttons on the scroll wheel
      // (Logitec g502 / Razer Basilisk / etc, ...)

      console.log(e.deltaX + ' <- left right || up down -> ' + e.deltaY);
      doSome(e.deltaY, e.deltaX); // ... the function which will consume events
    }

    function startDelta ()
    {
      document.addEventListener('wheel', getDelta);
    }

    function stopDelta () // we stop deltaX and deltaY capture
    {
      document.removeEventListener('wheel', getDelta);
    }

    function doSome (y, x) // we pass events as arguments so we can use them
    {
      if (x > 0)
        console.log('some div with overflow will scroll left');
      if (x < 0)
        console.log('some div with overflow will scroll right');
      if (y > 0)
        console.log('some div with overflow will scroll up');
      if (y < 0)
        console.log('some div with overflow will scroll down');
    }

</script>
<button onclick="startDelta()"> Start Delta </button>
&nbsp;&nbsp;
<button onclick="stopDelta()"> Stop Delta </button>
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