美文网首页
SweetAlert2例子

SweetAlert2例子

作者: 好好他爸爸 | 来源:发表于2020-07-03 22:07 被阅读0次
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>SweetAlert2例子</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/limonte-sweetalert2/8.11.8/sweetalert2.min.css">


</head>
<body>
<div class="demo_1">
    带有 确定和取消的 弹框1:
    <button>点击这里</button>
</div>
<div class="demo_2">
    带有 确定和取消的 弹框2:
    <button>点击这里</button>
</div>
<div class="demo_3">
    带有 展示 标签内容 的弹框:
    <button>点击这里</button>
</div>
<div class="demo_4">
    带有定位 和 消失时间的 弹框:
    <button>点击这里</button>
</div>
<div class="demo_5">
    自定义 第三方 css 的 弹框 (推荐使用 Animate.css ):
    <button>点击这里</button>
</div>
<div class="demo_6">
    右上角 通知类 弹窗:
    <button>点击这里</button>
</div>
<div class="demo_7">
    做 一个自己的 通用模版:
    <button>点击这里</button>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/limonte-sweetalert2/8.11.8/sweetalert2.all.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
    $(function () {
        $(".demo_1 button").click(function () {
            Swal.fire({
                type: 'warning', // 弹框类型
                title: '注销帐号', //标题
                text: "注销后将无法恢复,请谨慎操作!", //显示内容

                confirmButtonColor: '#3085d6',// 确定按钮的 颜色
                confirmButtonText: '确定',// 确定按钮的 文字
                showCancelButton: true, // 是否显示取消按钮
                cancelButtonColor: '#d33', // 取消按钮的 颜色
                cancelButtonText: "取消", // 取消按钮的 文字

                focusCancel: true, // 是否聚焦 取消按钮
                reverseButtons: true  // 是否 反转 两个按钮的位置 默认是  左边 确定  右边 取消
            }).then((isConfirm) => {
                try {
                    //判断 是否 点击的 确定按钮
                    if (isConfirm.value) {
                        Swal.fire("成功", "点击了确定", "success");
                    } else {
                        Swal.fire("取消", "点击了取消", "error");
                    }
                } catch (e) {
                    alert(e);
                }
            });
        });
        $(".demo_2 button").click(function () {
            Swal.fire({
                title: '领取你的寻龙装备!',
                input: 'select',
                html:
                    '<input id="swal-input1" class="swal2-input">' +
                    '<input id="swal-input2" class="swal2-input">',
                inputAttributes: {
                    autocapitalize: 'off'
                },
                showCancelButton: true,
                confirmButtonText: 'Look up',
                showLoaderOnConfirm: true,
                preConfirm: (login) => {
                    return fetch(`//api.github.com/users/${login}`)
                        .then(response => {
                            if (!response.ok) {
                                throw new Error(response.statusText)
                            }
                            return response.json()
                        })
                        .catch(error => {
                            Swal.showValidationMessage(
                                `Request failed: ${error}`
                            )
                        })
                },
                allowOutsideClick: () => !Swal.isLoading()
            }).then((result) => {
                if (result.value) {
                    Swal.fire({
                        title: `${result.value.login}'s avatar`,
                        imageUrl: result.value.avatar_url
                    })
                }
            })
        });
        $(".demo_3 button").click(function () {
            var content = "无记录";
            var msg = ""; //msg 是从外面传入的数据

            if (msg) {

                content = "<p style='color: red'>最近一次操作后的5小时内有效</p> "
                    + "<p>可以使用 Ctrl +F 查找关键字</p>"
                    + "<table class='table_list'>"
                    + "<tr>"
                    + "     <th class='js_tr_data'> 时间</th> <th>名称</th> <th>密码</th>"
                    + "</tr>"
                    + msg
                    + "</table>"
            }

            Swal.fire({
                title: '<strong>记录</strong>',
                type: 'info',
                html: content, // HTML
                focusConfirm: true, //聚焦到确定按钮
                showCloseButton: true,//右上角关闭
            })

        });
        $(".demo_4 button").click(function () {
            Swal.fire({
                position: 'top-end', //定位 左上角
                type: 'success',
                title: 'Your work has been saved',
                showConfirmButton: false,
                timer: 1500
            });

        });
        $(".demo_5 button").click(function () {
            Swal.fire({
                title: 'Custom animation with Animate.css',
                animation: false,
                customClass: 'animated tada'
            })

        });

        $(".demo_6 button").click(function () {
            Swal.fire({
                toast: true,
                position: 'top-end',
                showConfirmButton: false,
                timer: 3000,
                type: 'success',
                title: 'Signed in successfully'
            })

        });
        //定义模版 (可多次使用)
        var MyBox = Swal.mixin({
            toast: true,
            position: 'top-end',
            showConfirmButton: false,
            timer: 3000
        });
        $(".demo_7 button").click(function () {


            //调用
            MyBox.fire({
                type: 'success',
                title: 'successfully'
            });

        })
    });
</script>
</body>
</html>

相关文章

  • SweetAlert2: "setDefaults" & "re

    SweetAlert2: "setDefaults" & "resetDefaults" methods are ...

  • sweetAlert1 使用自定义样式设置弹窗宽度

    sweetAlert1不像sweetAlert2那样可以通过width属性来配置弹窗的宽度。但是可以通过custo...

  • Sweetalert2的使用

    替代alert的好插件 实例 需要在swal 写成 swal.fire 实例都能调用了! 地址github地...

  • 前端后端常用插件(转载)

    这里主要推荐一下自己平时常用,提高效率的一些库和软件。 前端常用 sweetalert2 一个自适应,且自定义性强...

  • 例子

    # 开学了 - 一天 -二天 - 三天

  • 例子

    例子1 例子2 例子3 例子4 例子5 例子6 超链接 黑体 下面是代码块 下面是引用 引用

  • 例子

    莫言在《母亲》一文里写到了两处他在小时候不能理解,但影响深远的事情。第一处是: 愁容满面的母亲,在辛苦地劳作时,嘴...

  • 例子

    要求1,三个inputview同时有值的时候按钮可点击. 要求2,网络请求完成之后,才能再次点击. 传统实现,在t...

  • 例子

    红绿灯真的那么有必要吗? 书里提到,荷兰的一个交通规划师莫德尔曼观察到,当一个城市停电导致红绿灯失...

  • 例子

    忽然想找个例子,既贴近生活,又能讲出深刻道理。 唱首歌就来了灵感。 生活就像我的歌声, 时而不靠谱 ,时而不着调。

网友评论

      本文标题:SweetAlert2例子

      本文链接:https://www.haomeiwen.com/subject/fhvpqktx.html