Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i stop sweetalert scrolling to top after clicking ok?

I'm using sweetalert2 script for when a user posts a comment on my site, It scrolls down to their comment and sweet alert pops up but when they click ok on the sweet alert box it scrolls back upto the top.

From what i've been reading i need some sort of preventdefault or something but i can't figure out where that would go?

Here is my script:

<!-- Sweet alert -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/limonte-sweetalert2/6.6.8/sweetalert2.min.js"></script>
<script>
window.location.hash = "comment-<?php echo $newcommentid; ?>";
 $(document).ready(function () {
    swal({
                title: "Comment Posted",
                text: "Thanks, your comment has now been successfully posted.",
                type: "success"
            });
 });     

</script>
like image 466
Exoon Avatar asked Sep 09 '17 20:09

Exoon


3 Answers

I successfully prevented that by simply adding a line in the fire(): heightAuto: false

Swal.fire({
  heightAuto: false,
  text: "I\'m sure this would run on SWAL2@7*"
)};
like image 142
Vu Tuan Hung Avatar answered Oct 09 '22 04:10

Vu Tuan Hung


Try this

$('#comment').click(function(event){
   //....swal stuff
   event.preventDefault();
});
like image 40
Achraf Avatar answered Oct 09 '22 04:10

Achraf


Base on version 7.* you can use this style:

html.swal2-shown,body.swal2-shown { overflow-y: hidden !important; height: auto!important;}
like image 5
Nhat Ngo Avatar answered Oct 09 '22 05:10

Nhat Ngo