实现简易Alert组件案例

效果如图

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <meta http-equiv="X-UA-Compatible" content="ie=edge">

    <title>Alert</title>

    <style type="text/css">

        html {

            font-size: 14px;

        }

        * {

            margin: 0;

            padding: 0;

        }

        .container {

            width: 80%;

            margin-left: 10%;

            margin-top: 50px;

            list-style-type: none;

            border: 1px solid #eee;

            border-radius: 4px;

            overflow: hidden;

        }

        .button {

            width: 100%;

            height: 2.2rem;

            text-align: center;

            line-height: 2.2rem;

            box-sizing: border-box;

            color: #fff;

            background: #E37EFC;

            cursor: pointer;

        }

        .button:hover {

            background: #D334FF;

        }

        .container li:nth-child(1),

        .container li:nth-child(2) {

            border-bottom: 1px solid #eee;

        }

        .title_container {

            position: absolute;

            left: 50%;

            transform: translateX(-50%);

            line-height: 100%;

            padding: 4px 8px;

            text-align: center;

            overflow: hidden;

            text-overflow: ellipsis;

            white-space: nowrap;

            min-width: 7rem;

            max-width: 80%;

            height: 2.4rem;

            line-height: 2.4rem;

            background: #000;

            color: #fff;

            border-radius: 4px;

            opacity: 0.5;

            z-index: 9999 !important;

        }

        .button_mask {

            position: absolute;

            left: 0;

            top: 0;

            right: 0;

            bottom: 0;

            width: 100%;

            height: 100%;

            z-index: 9998 !important;

            background: #000;

            opacity: 0.3;

        }

        .button_container {

            position: absolute;

            left: 50%;

            transform: translateX(-50%);

            padding: 0;

            text-align: center;

            overflow: hidden;

            text-overflow: ellipsis;

            white-space: nowrap;

            width: 14rem;

            height: 8rem;

            line-height: 4rem;

            border-radius: 4px;

            z-index: 9999 !important;

            background: #fff;

            box-sizing: content-box;

        }

        .button_container_title,

        .button_container_content,

        .button_container_buttons {

            width: 100%;

            padding: 0px;

            text-align: center;

            box-sizing: border-box;

        }

        .button_container_title {

            height: 3rem;

            line-height: 3rem;

        }

        .button_container_buttons {

            height: 2.7rem;

            line-height: 2.7rem;

        }

        .button_container_content {

            height: 2.4rem;

            line-height: 2.4rem;

            border-bottom: 1px solid #eee;

            clear: both;

        }

        .button_container_buttons_left,

        .button_container_buttons_right {

            float: left;

            width: 50%;

            text-align: center;

            cursor: pointer;

        }

        .button_container_buttons_left:hover,

        .button_container_buttons_right:hover {

            background-color: #E37EFC;

            color: #fff;

        }

        .button_container_buttons_right {

            box-sizing: border-box;

            border-left: 1px solid #eee;

        }

        .notification_container {

            position: fixed;

            right: 0px;

            top: 12rem;

            margin: 0;

            padding: 0;

            width: 14rem;

        }

        .notification {

            position: relative;

            right: -400px;

            top: 0px;

            margin: 0;

            padding: 0;

            width: 100%;

            height: 8rem;

            border-radius: 4px;

            border: 1px solid #ccc;

            background: #E37EFC;

            color: #fff;

            margin-top: 16px;

            text-align: center;

            line-height: 8rem;

        }

        .notification_close {

            position: absolute;

            right: 10px;

            top: 4px;

            cursor: pointer;

            width: 1.4rem;

            height: 1.4rem;

            border-radius: 0.7rem;

            line-height: 1.4rem;

            background: #fff;

            color: #D334FF;

            font-weight: bold;

            text-align: center;

        }

    </style>

</head>

<body>

    <ul class="container">

        <li class="button">Title</li>

        <li class="button">Button</li>

        <li class="button">Notification</li>

    </ul>

    <script src="http://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script>

    <script type="text/javascript">

        $(document).ready(function () {

            var typeCodes = ["Title", "Button", "Notification"]

            function init(title) {//监听初始化

                $(".button").on("click", function () {

                    createAlert(typeCodes[$(this).index()], "title");

                })

            }

            function createAlert(type, title) {

                let deviceHeight = window.innerHeight;

                if (type === "Title") {

                    titleInit(title)

                } else if (type === "Button") {

                    buttonInit(title);

                } else if (type === "Notification") {

                    notificationInit(5);

                }

            }

            function titleInit(title) {//title类型初始化

                $("body").append(

                    `<div class="title_container" style="top:${(innerHeight - (14 * 2.4)) / 2}px;">${title}</div>`

                );

                $(".title_container").animate({ opacity: 0 }, 2500, function () {

                    $(this).remove();

                })

            }

            function buttonInit(title) {//button类型初始化

                $("body").append(

                    `<div class="button_mask"></div>`

                );

                $("body").append(

                    `<div class="button_container" style="top:${(innerHeight - (14 * 8)) / 2}px;">

                            <div class="button_container_title">Alertjs提示</div>

                            <div class="button_container_content">欢迎使用alertjs</div>

                            <div class="button_container_buttons">

                                <div class="button_container_buttons_left">确定</div>

                                <div class="button_container_buttons_right">取消</div>

                            </div>

                        </div>`

                );

                $('.button_mask').on('click', function (e) {

                    $(this).animate({ opacity: 0 }, 500, function () {

                        $(this).remove();

                    });

                    $(".button_container").animate({ opacity: 0 }, 500, function () {

                        $(".button_container").remove();

                    });

                })

                $('.button_container_buttons_right').on('click', function (e) {

                    $(".button_mask").animate({ opacity: 0 }, 500, function () {

                        $(".button_mask").remove();

                    });

                    $(".button_container").animate({ opacity: 0 }, 500, function () {

                        $(".button_container").remove();

                    });

                })

            }

            function notificationInit(num, duration, playDuration) {//notification类型初始化

                $('body').append(`<div class='notification_container'>

                        <div class='notification'>

                            <div class='notification_close'>x</div>

                            HELLO NOTIFICATION

                        </div>

                    </div>`);

                setTimeout(function () {

                    $('.notification').animate({ right: "10px" }, function () {

                        var that = this;

                        setTimeout(function () {

                            $(that).animate({ height: "0px", opacity: 0.3 }, function () {

                                $(this).remove();

                            });

                        }, duration ? duration : 5000);

                    });

                    $('.notification_close').on('click', function (e) {

                        $(e.target).parent().animate({ height: "0px", opacity: 0.3 }, function () {

                            $(this).remove();

                        });

                    })

                }, playDuration ? playDuration : 0);

            }

            init("HELLO ALERT");

        })

    </script>

</body>

</html>

因为是半小时内完成的,所以有部分展示内容是写死的,如果要参考请改一下参数即可

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 224,383评论 6 522
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 96,028评论 3 402
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 171,486评论 0 366
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 60,786评论 1 300
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 69,793评论 6 399
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 53,297评论 1 314
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 41,685评论 3 428
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 40,655评论 0 279
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 47,192评论 1 324
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 39,223评论 3 345
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 41,343评论 1 354
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 36,966评论 5 350
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 42,656评论 3 336
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 33,130评论 0 25
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 34,257评论 1 275
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 49,869评论 3 381
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 46,407评论 2 365

推荐阅读更多精彩内容