8

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>
Limon Monte
  • 44,025
  • 43
  • 163
  • 189
Exoon
  • 1,379
  • 2
  • 16
  • 31

7 Answers7

4

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*"
)};
Azametzin
  • 4,342
  • 12
  • 22
  • 38
3

Try this

$('#comment').click(function(event){
   //....swal stuff
   event.preventDefault();
});
Achraf
  • 924
  • 5
  • 14
  • Still doesn't seem to do anything. After pressing OK on the sweetalert2 it scrolls back to the top of the page. – Exoon Sep 10 '17 at 13:09
  • Try This body.swal2-height-auto { overflow-y: visible !important; height:100% !important; } – Khaled Feb 28 '19 at 00:10
3

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

html.swal2-shown,body.swal2-shown { overflow-y: hidden !important; height: auto!important;}
Stephen Rauch
  • 40,722
  • 30
  • 82
  • 105
Nhat Ngo
  • 65
  • 1
  • 8
1

This is a common problem for those who have added a hidden overflow to their body.

Source: https://github.com/sweetalert2/sweetalert2/issues/781

The solution is simple...add this to your CSS:

body.swal2-shown:not(.swal2-no-backdrop):not(.swal2-toast-shown) { overflow-y: visible !important; }
user2662680
  • 412
  • 5
  • 13
1

Use the backdrop option and set it to false as below.

swal({
   backdrop:false,
   title: "Comment Posted",
   text: "Thanks, your comment has now been successfully posted.",
   type: "success"
});
gihandilanka
  • 374
  • 3
  • 14
1

For those having still the same issue, you can add this: "heightAuto: false" to your Sweet Alert options and the problem solves.

Swal.fire({
heightAuto: false,
title: 'Hello',
Text: 'This is an alert'
});
0

There are times where you might need more. I was trying to use the previous answers but it wasn't working for me on Electron. I fixed this by using heightAuto and backdrop. Using an rgba value seems to simulate the default backdrop pretty well. Setting the backdrop manually was the only thing that fixed it for me.

swal({
   title: "Title here",
   text: "Text here",
   icon: "success",
   heightAuto: false,
   backdrop: "rgba(0,0,0,0.4)"
});