S
S
Suma18212020-02-12 23:50:34
JavaScript
Suma1821, 2020-02-12 23:50:34

The 'wheel' event doesn't work correctly in firefox, why?

Hi Durzya! The bottom line is, I have a slider that should scroll with a scroll, I hung a wheel event on this whole thing, everything works fine in chrome and safari, but firefox, after the second or narrow scroll, sticks, because this.scrollTop has time to scroll, it looks like this , as if the browser did not fully process event.preventDefault()

scrollWrapper();
function scrollWrapper() {
  const wrapper = document.querySelector('.wrapper');
  const sections = document.querySelectorAll('.section');

  sections[0].setAttribute('second-slide',0);
  sections[1].setAttribute('second-slide', wrapper.offsetHeight);
  sections[2].setAttribute('second-slide', wrapper.offsetHeight * 2);
  //function for mouse scroll

  wrapper.addEventListener('wheel', function(event){

    if (this.scrollTop === +sections[0].getAttribute('second-slide')) {
      if (event.deltaY > 0) {
        this.scrollTo({
        top: sections[1].getAttribute('second-slide'),
        behavior: "smooth"
      });
    }
  }

   if (this.scrollTop === +sections[1].getAttribute('second-slide')) {
    if (event.deltaY > 0) {
      this.scrollTo({
      top: sections[2].getAttribute('second-slide'),
      behavior: "smooth"
      });
    } 
    else {
          this.scrollTo({
          top: sections[0].getAttribute('second-slide'),
          behavior: "smooth"
          });
        }
      }

      if (this.scrollTop === +sections[2].getAttribute('second-slide')) {
        if (event.deltaY < 0) {
          this.scrollTo({
          top: sections[1].getAttribute('second-slide'),
          behavior: "smooth"
        });
      }
    }
    event.preventDefault();
  });
  }

Any help on interpreting why is welcome. Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Suma1821, 2020-02-13
@Suma1821

DONE! Everything was much simpler, as usual, the problem was in strict comparison, in the mozzilla the event worked several times and the browser did not have time to process it. Changed the strict comparison to more, less. Here is the solution:

function scroll (event){
    event.preventDefault();

    //down to second slide
    if (this.scrollTop >= +sections[0].getAttribute('second-slide') ) {
      if (event.deltaY > 0 ) {
        this.scrollTo({
        top: +sections[1].getAttribute('second-slide'),
      });
    }
  }

    //down to third slide
    if (this.scrollTop >= +sections[1].getAttribute('second-slide')) {
      if (event.deltaY > 0) {
        this.scrollTo({
        top: +sections[2].getAttribute('second-slide'),
      });
    } 
  }

    //up to first slide
    if (this.scrollTop <= +sections[2].getAttribute('second-slide')) {
      if (event.deltaY < 0) {
        this.scrollTo({
        top: +sections[1].getAttribute('second-slide'),
      });
    }
  }

    //up to second slide
    if (this.scrollTop <= +sections[1].getAttribute('second-slide')) {
      if (event.deltaY < 0) {
        this.scrollTo({
        top: +sections[0].getAttribute('second-slide'),
      });
    }
  }
};

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question