zoukankan      html  css  js  c++  java
  • JS实现的模态框弹窗并自动消失

    一、Demo.html

    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="UTF-8">
            <title>js弹框3秒后自动消失</title>
            <link rel="stylesheet" type="text/css" href="./js/bootstrap/css/bootstrap.css"/>
            <link rel="stylesheet" type="text/css" href="./css/demo.css"/>            
        </head>
        <body>
            <button onclick="showModal()">点击弹出模态框</button>
            
            <div class='modal my-modal-alert' id='my-modal-alert'>
                <div class='modal-dialog boxBodySmall'>
                    <div class='modal-content'>
                        <div class='modal-header boxHeader'>
                            <button type='button' class='close boxClose' data-dismiss='modal'>
                                <span aria-hidden='true'>×</span><span class='sr-only'>Close</span>
                            </button>
                            <h4 class='modal-title boxTitle' id='modal-title'>模态框</h4>
                        </div>
                        <div class='modal-body' id='modal-body-alert'>
                            <div id='modal_message'>js弹框自动消失案例</div>
                            <span id='num'></span>
                        </div>
                        <div class='modal-footer boxFooter' id='modal-footer'>
                            <button type='button' class='btn btn-default boxButton' data-dismiss='modal'>关闭</button>
                            <button type='button' class='btn btn-primary boxButton'>保存</button>
                        </div>
                    </div>
                </div>
            </div>
            
            <script src="./js/jquery/jquery-1.11.2.js"></script>
            <script src="./js/bootstrap/js/bootstrap.min.js"></script>
            <script src="./js/jquery-ui/jquery-ui.min.js"></script>
            
            <script>
                var clearFlag = 0;
                var count = 3;//设置3秒后自动消失
                var showModal = function(){
                    $("#my-modal-alert").toggle();//显示模态框
                    
                    $("#my-modal-alert").draggable({//设置模态框可拖动(需要引入jquery-ui.min.js)
                        handle: ".modal-header"
                    });
                    
                    clearFlag = self.setInterval("autoClose()",1000);//每过一秒调用一次autoClose方法
                }
                
                var autoClose = function(){
                    if(count>0){
                        $("#num").html(count + "秒后窗口将自动关闭");
                        count--;
                    }else if(count<=0){
                        window.clearInterval(clearFlag); 
                        $("#num").html("");
                        $("#my-modal-alert").fadeOut("slow");
                        count = 3;
                        $("#my-modal-alert").toggle();
                    }
                }
            </script>
        </body>
    </html>
    

    注意:1、bootstrap依赖于jquery,引入bootstrap前需要引入jquery

    二、Demo.css

    /*弹框本身(大)*/ 
    .my-modal-alert .boxBodyBig{
        496px;
        height: 418px;
    }
    /*弹框本身(小)*/ 
    .my-modal-alert .boxBodySmall{
        412px;
        height: 418px;
    }
    /*弹框头*/
    .my-modal-alert .boxHeader{
        background-color: #f6f6f6;
        border-bottom: #e5e5e5 1px;
        height: 48px;
    } 
    /*弹框标题*/ 
    .my-modal-alert .boxTitle{
        background-color: #f6f6f6;
        color:#333333;
        font-family:"SimHei";
        font-size: 16px;
    }
    /*弹框头部分右侧关闭按钮*/
    .my-modal-alert .boxClose{
        
    }
    .my-modal-alert .boxClose:hover{
        color: #ff7800;
    }
    /*弹框按钮的父级元素块*/
    .my-modal-alert .boxFooter{
        margin: auto;
        text-align: center;
        padding:24px 15px 24px 15px;
        margin:0px 15px 0px 15px;
    }
    /*弹框按钮*/
    .my-modal-alert .boxButton{
        font-family:"SimHei";
        background-color:#ff7800;
         70px;
        height: 30px;
        color:white;
        text-align:center;
        line-height: 1;
    }
    
    /*已下为选用*/
    /*弹框主题label框*/
    .my-modal-alert .boxLabel{
        80px;
        font-size: 14px; 
        font-family:'SimHei';
        color: #999999;
        
    }
    /*文本框*/
    .my-modal-alert .boxInput{
        176px;
        font-size: 14px;
        color: #333333;
    }
    /*纯文本*/
    .my-modal-alert .boxText{
        color:#ff7800;
        font-family:'SimHei';
        font-size: 12px;
    }
    .my-modal-alert .boxTextarea{
        font-size: 12px;
    }
    
    .my-modal-alert .modal-body{
         400px;
        height: 100px;
        text-align: center;
    }
    .my-modal-alert .modal_message{
        margin-top: 20px;
        
    }
    

    本文来自博客园,作者:農碼一生,转载请注明原文链接:https://www.cnblogs.com/wml-it/p/15763810.html


    技术的发展日新月异,随着时间推移,无法保证本博客所有内容的正确性。如有误导,请大家见谅,欢迎评论区指正!
    个人开源代码链接:
    GitHub:https://github.com/ITMingliang
    Gitee:https://gitee.com/mingliang_it
    GitLab:https://gitlab.com/ITMingliang
    进开发学习交流群:

  • 相关阅读:
    零基础学python-2.6 数字与字符串
    零基础学python-2.5 变量与赋值
    零基础学python-2.4 操作符
    零基础学python-2.3 注释
    js原型和原型链
    ES6数据结构Set、Map
    JS高阶函数--------map、reduce、filter
    vue-cli3.0以上项目中引入jquery的方法
    vue项目中使用echarts map报错Cannot read property 'push' of undefined nanhai.js
    js 将时间戳转成时间格式化
  • 原文地址:https://www.cnblogs.com/wml-it/p/15763810.html
Copyright © 2011-2022 走看看