SweetAlert2 scrolls to initiating element after close
Asked Answered
P

3

6

After closing of the modal I force the browser to scroll to the top of the page to see any error messages. After integrating the SweetAlert2 module to confirm submission the application will auto scroll back down to the submit button now after closing instead of staying at the top.

submit() {
    swal({
      title: "Submit application",
      html: "All submissions are final",
      type: "warning",
      showCancelButton: true,
      confirmButtonText: "Yes, delete it!",
      cancelButtonText: "Cancel"
    }).then(result => {
          window.scrollTo(0,0);
        }
    );
  }

As you can see I'm trying to force the scroll to the top in multiple areas and this works, but then it snaps back to the bottom. Please see attached gif as demonstration.

Scrolling error

Any ideas on how to fix this?

Bootstrap 4

SweetAlert2 7.28.2

EDIT: Reproduced in simple JSFiddle https://jsfiddle.net/s8f12xad/

Prodigious answered 27/9, 2018 at 18:19 Comment(0)
B
8

didClose parameter is what you need in this case:

Swal.fire({
  didClose: () => window.scrollTo(0,0)
})
Boche answered 28/9, 2018 at 9:15 Comment(1)
I have a similar problem when the button is semi visible at the bottom of the page when opened.. when it closes it scrolls the page so that the WHOLE button is visible. I don't want it to move to the top, or to the elem.. I just want it to close. Any suggestions? PS. Been reading through the Git issues, great to see such fast responses from you, awesome work and great plugin!Barrister
K
2

I know this is an old post but all mentioned above failed when I tested. The solution that worked is the following configuration option:

returnFocus: false,
Korrie answered 9/5 at 4:43 Comment(0)
F
1

Really ? the author answer you, thats its great !!!

I had a redirection problem which consisted of losing the navigation scroll, I solved it with this code:

Swal.fire({
    title: 'Example',
    type: 'success',
    onAfterClose: () => window.location.href = "url"
});

Thank you.

Fidelity answered 20/2, 2019 at 15:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.