Prevent BODY from scrolling when a modal is opened in angular application

Viewed 783

I want my body to stop scrolling to top when I am trying to open popup modals. I am using both angular material and ng-bootstrap popup modals.

I've tried the piece of CSS code below, but the issue is still there, please help.

body.modal-open {
  overflow: hidden;
  position: fixed;
}
1 Answers

From what I read here here the position of the modal is set to default at the top. To completely remove the scrolling, use that to position the modal down. If you just want to be at the same position after the modal close, you can use this which I used from the comments: here

  openModal() {
    // Remember the current scroll position
    let pos = (document.documentElement.scrollTop || document.body.scrollTop);
    this.bsModalRef = this.modalService.show(ModalComponent);
     this.modalHideSubscription = this.modalService.onHide.subscribe(() => {
      //Return back to the position before opening modal
      window.scrollTo(0,pos);
      this.modalHideSubscription.unsubscribe();
    });
  }
Related