Home > Back-end >  After preventing the default scroll event, scrollIntoView(); doesn't work Properly on Firefox d
After preventing the default scroll event, scrollIntoView(); doesn't work Properly on Firefox d

Time:11-04

In my scenario the code should prevent the the default scroll event and use scrollIntoView() to move the user to a specific section according to the scroll direction.

https://stackoverflow.com/a/4770179/9164633 I used this method to prevent the scroll default event.
And I detect the direction like so,

preventDefault(e) {
  e.preventDefault(); 
  if(this.waiting == false && this.forceInitialScroll != true) {
    if(e.deltaY && e.deltaY > 7) {
      this.scrollDirection = 'down';
        this.checkScroll()
    }else if(e.deltaY && e.deltaY < -7) {
      this.scrollDirection = 'up';
        this.checkScroll()
    }else {
      
    }
  }
},

and Im pretty sure that both are working fine.

After preventing the scroll and detecting the scroll direction I try to scroll the user to the section like so,

checkScroll() {
     let element;
     if(this.scrollDirection == 'down' && this.scrollIndex != 4 ) {
       element = document.getElementById(`section-${this.scrollIndex 1}`);
     }else if(this.scrollDirection == 'up' && this.scrollIndex != 0) {
       element = document.getElementById(`section-${this.scrollIndex-1}`);
     } 
     this.waiting = true;
     if(element) {
       console.log(element)
       element.scrollIntoView({
         behavior: 'smooth',
         block: 'center',
       })
     }
     setTimeout(() => {
       if(this.waiting == true) {
         this.waiting = false;
         this.scrollDirection = null;
       }
     }, 450)
   },

this.waiting is used to prevent the user form scrolling more than one section at a time.
On firefox the browser doesnt scroll the user properly though its working fine on chrome.

CodePudding user response:

Apparently Firefox's behavior with event.preventDefault() was that it blocked any kind of user scrolling and even the js manual scrolling.

The problem in my case was solved by hiding the scroll in css instead of js by using overflow: hidden; and then controlling the scroll manually instead of preventing the default behavior.

  • Related