SweetAlert 弹窗的时候会擅自滚动页面到顶端,怎么禁止滚动?[找到解法三!]
如下图所示,准备删除页面底部一篇文章,点击删除按钮后 SweetAlert 弹窗询问是否真要删除。问题是:弹窗的时候,页面也被 SweetAlert 滚动到最顶端了。我不希望它滚动,求禁止滚动的方法,谢谢!
产生这个问题的原因,跟我写的代码无关,这应该是 SweetAlert 插件自身的问题。我直接从 https://sweetalert.bootcss.com/guides/ 这个网站复制了一段代码,同样存在这个问题。我复制的代码内容如下
swal({
title: "Are you sure?",
text: "Once deleted, you will not be able to recover this imaginary file!",
icon: "warning",
buttons: true,
dangerMode: true,
})
.then((willDelete) => {
if (willDelete) {
swal("Poof! Your imaginary file has been deleted!", {
icon: "success",
});
} else {
swal("Your imaginary file is safe!");
}
});
现在回想起来,问题出在了 href='#' 这上面,SweetAlert 插件本身没有问题!
执行这个代码之前,用 e.preventDefault(); 阻止其他的event试试
代码可能就是这样子