簡體   English   中英

單擊“確定”后,如何停止 Sweetalert 滾動到頂部?

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

當用戶在我的網站上發表評論時,我正在使用sweetalert2腳本,它向下滾動到他們的評論並彈出甜蜜警報,但是當他們在甜蜜警報框中單擊確定時,它會滾動回頂部。

從我一直在閱讀的內容來看,我需要某種預防違約或其他東西,但我不知道那會去哪里?

這是我的腳本:

<!-- 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>

我通過簡單地在fire(): heightAuto: false添加一行成功地阻止了這種情況fire(): heightAuto: false

Swal.fire({
  heightAuto: false,
  text: "I\'m sure this would run on SWAL2@7*"
)};

試試這個

$('#comment').click(function(event){
   //....swal stuff
   event.preventDefault();
});

基於版本 7.* 你可以使用這種風格:

html.swal2-shown,body.swal2-shown { overflow-y: hidden !important; height: auto!important;}

對於那些在身體上添加了隱藏溢出的人來說,這是一個常見的問題。

來源: https : //github.com/sweetalert2/sweetalert2/issues/781

解決方案很簡單……將其添加到您的 CSS 中:

 body.swal2-shown:not(.swal2-no-backdrop):not(.swal2-toast-shown) { overflow-y: visible !important; }

使用backdrop選項並將其設置為false ,如下所示。

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

對於那些仍然有相同問題的人,您可以將這個:“ heightAuto: false ”添加到您的 Sweet Alert 選項中,問題就解決了。

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

有時您可能需要更多。 我試圖使用以前的答案,但它在 Electron 上對我不起作用。 我通過使用 heightAuto 和背景解決了這個問題。 使用 rgba 值似乎可以很好地模擬默認背景。 手動設置背景是唯一為我修復它的方法。

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

我通過使用讓它工作,我在移動設備上遇到了問題,它把身體放在固定的位置,它使 html 的高度為 0。

body.swal2-shown, body.swal2-shown.swal2-iosfix {
position: initial !important;

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM