js彈框3秒後自動消失

來源:互聯網
上載者:User

標籤:

開發中有時候會需要最出彈框後,過幾秒自動消失的效果,下面給大家分享一下我自己做的一個小案例。

 

案例中的彈框使用的是bootstrap裡面的模態框,實現自動消失則用的是js中的setInterval方法。該彈框使用了jquery-ui中的draggable方法,可拖動。

目錄結構如下:

 

 

下面是案例代碼:

 

 

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‘>&times;</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>

 

demo.css

/*彈框本身(大)*/ .my-modal-alert .boxBodyBig{    width:496px;    height: 418px;}/*彈框本身(小)*/ .my-modal-alert .boxBodySmall{    width: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;    width: 70px;    height: 30px;    color:white;    text-align:center;    line-height: 1;}/*已下為選用*//*彈框主題label框*/.my-modal-alert .boxLabel{    width:80px;    font-size: 14px;     font-family:‘SimHei‘;    color: #999999;    }/*文字框*/.my-modal-alert .boxInput{    width: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{    width: 400px;    height: 100px;    text-align: center;}.my-modal-alert .modal_message{    margin-top: 20px;    }

 

 

注意項:

1、bootstrap依賴於jquery,引入bootstrap前需要引入jquery

 

js彈框3秒後自動消失

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.