iis服务器助手广告广告
返回顶部
首页 > 资讯 > 前端开发 > JavaScript >JS实现定时自动消失的弹出窗口
  • 713
分享到

JS实现定时自动消失的弹出窗口

2024-04-02 19:04:59 713人浏览 安东尼
摘要

一、Demo.html <!DOCTYPE html> <html> <head> <meta charset="U

一、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" rel="external nofollow" />
        <link rel="stylesheet" type="text/css" href="./css/demo.css" rel="external nofollow" />            
    </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{
    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;
}



.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;
    
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程网。

--结束END--

本文标题: JS实现定时自动消失的弹出窗口

本文链接: https://www.lsjlt.com/news/147295.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作