javascript - div 内的 SweetAlert2

标签 javascript sweetalert2

我遇到了同样的问题,我想在 div 中包含 sweetalert2 (不是 sweetalert toast),因为我希望除了它之外的另一个 div 在弹出 sweetalert 时可以滚动。我已经尝试过上面的代码,但它仍然无法正常工作。请在此处查看图片:sweetalert2

var id = document.getElementById('mydiv');
swal({
title: "question",
target: id,
showConfirmButton: false,
showCloseButton: true,
allowOutsideClick: false,
allowEscapeKey : false,
customClass: "swal-height"
});

我还修复了 css 中的定位

#mydiv{
  overflow: hidden;
  height: calc(100% - 300px) !important;
  padding-bottom: 70px;
  position: relative !important;
}

#mydiv .swal-height {
    position:absolute !important;
    height: 720px !important;
    width: 780px !important;
    margin-left: 1100px !important;
    margin-top: 280px !important;
}

你能建议我在这里做错了什么吗?

这是 JSFiddle,它与我正在构建的类似:https://jsfiddle.net/0pt2vr87/7/

感谢您的帮助。

最佳答案

您错误地使用了 customClass 参数,它应该是一个对象,而不是字符串。

Swal.fire({
  text: 'Modal inside a custom target',
  target: '#custom-target',
  customClass: {                      // <------ customClass is an object!
    container: 'position-absolute'
  },
})
#custom-target {
  position: relative;
  width: 300px;
  height: 200px;
  border-style: solid;
}

.position-absolute {
  position: absolute !important;
}
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@10"></script> 

<div id="custom-target"></div>

参见官方示例:https://sweetalert2.github.io/recipe-gallery/toast-with-custom-target.html

关于javascript - div 内的 SweetAlert2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64744259/

相关文章:

javascript - 如何在 Rails 4 的 javascript 中包含 ruby​​ 变量?

javascript - 使用 jQuery FileReader 获取宽度和高度?

javascript - 使用 sweetalert 加载进度表提交

javascript - 对话框关闭后从 DOM 中删除 SweetAlert2

validation - SweetAlert2 : Validation required for one of the fields

javascript - Jquery 上传预览前的最大高度和最大宽度验证

javascript - PWA - 当我需要时提示输入 "add to home screen"?

javascript - Jquery Fade 过渡看起来不正确

angular - 我如何模拟 sweetalert 以及如何使其在 Angular Testing 中返回 true

javascript - SweetAlert2 在关闭后滚动到启动元素