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

 更新时间:2022年04月25日 15:22:42   作者:農碼一生  
这篇文章介绍了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" 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;
}

/*已下为选用*/
/*弹框主题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;
    
}

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

相关文章

  • QQ强制聊天功能代码(加强版,兼容QQ2010)

    QQ强制聊天功能代码(加强版,兼容QQ2010)

    QQ强制聊天功能代码,脚本之家以前也发布过,但已经不能用了,这个是新版本,经过测试,完全兼容新版本的qq.
    2010-06-06
  • window.print打印指定div指定网页指定区域的方法

    window.print打印指定div指定网页指定区域的方法

    这篇文章主要介绍了window.print打印指定div指定网页指定区域的方法,需要的朋友可以参考下
    2014-08-08
  • 微信小程序实现自动回复图片消息

    微信小程序实现自动回复图片消息

    这篇文章主要介绍了微信小程序实现客服消息自动回复图片消息,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习吧
    2023-01-01
  • 微信小程序自定义弹窗wcPop插件

    微信小程序自定义弹窗wcPop插件

    平时在开发小程序的时候,弹窗应用场景还是蛮广泛的,但是微信官方提供的弹窗比较有局限性,不能自定义修改。下面通过本文给大家带来了微信小程序自定义弹窗wcPop插件,感兴趣的朋友跟随小编一起看看吧
    2018-11-11
  • JS实现同一DOM元素上onClick事件与onDblClick事件并存的解决方法

    JS实现同一DOM元素上onClick事件与onDblClick事件并存的解决方法

    这篇文章主要介绍了JS实现同一DOM元素上onClick事件与onDblClick事件并存的解决方法,结合实例形式分析了javascript通过针对单击onclick事件增加定时器进行onClick事件与onDblClick事件的区别判定操作,需要的朋友可以参考下
    2018-06-06
  • JavaScript 跳出iframe框架示例详解

    JavaScript 跳出iframe框架示例详解

    这篇文章主要为大家介绍了JavaScript跳出iframe框架示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-11-11
  • JavaScript 冒泡排序和选择排序的实现代码

    JavaScript 冒泡排序和选择排序的实现代码

    本文通过实例代码给大家介绍了js冒泡排序和选择排序的实现代码,代码简单易懂,非常不错,具有参考借鉴价值,感兴趣的朋友一起学习吧
    2016-09-09
  • JavaScript实现翻页功能(附效果图)

    JavaScript实现翻页功能(附效果图)

    这篇文章主要介绍了JavaScript实现翻页功能(附效果图),在项目需求中经常遇到,今天小编抽时间给大家分享JavaScript实现翻页功能实例代码,需要的朋友参考下吧
    2017-02-02
  • js中取得变量绝对值的方法

    js中取得变量绝对值的方法

    这篇文章主要介绍了js中取得变量绝对值的方法,较为详细的分析了js中abs方法取绝对值的使用技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-01-01
  • js实现点击左右按钮轮播图片效果实例

    js实现点击左右按钮轮播图片效果实例

    这篇文章主要介绍了js实现点击左右按钮轮播图片效果的方法,涉及click事件相应、animate方法等使用技巧,需要的朋友可以参考下
    2015-01-01

最新评论