js弹框3秒后自动消失
开发中有时候会需要最出弹框后,过几秒自动消失的效果,下面给大家分享一下我自己做的一个小案例。
案例中的弹框使用的是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'>×</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秒后自动消失的更多相关文章
- jq弹框 (1)内容自适应宽度 2(内容框显示,几秒后自动消失)
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&q ...
- js实现网页多少秒后自动跳转到指定网址
在网上搜了一下,关于这个技术处理有多种方法,我只记下我在视频里学到的三种: 1.用一个response.sendRedirect("目标页面.jsp\.htm");实现直接跳转: ...
- Axure实现提示文本单击显示后自动消失的效果
Axure实现提示文本单击显示后自动消失的效果 方法/步骤 如图所示,框出的部分为提示文本(已经命名为tooltip),希望达到的效果是默认加载时不显示,点击帮助图标后显示,且2秒后自动消失. ...
- 【JS】【3】标签显示几秒后自动隐藏
$("#XXX").show().delay(2000).hide(0); 2000,0:可选,速度,(毫秒:"slow":"fast") ...
- JS n秒后自动跳转实例
<p><a href="<?php echo base_url();?>usercenter/index" id="message" ...
- js弹框处理
# -*- coding:utf-8 -*- """ js弹框处理 """ from selenium import webdriver d ...
- 两种方法实现js页面隔几秒后跳转,及区别
这里需要用到window的两个对象方法,setInterval()和setTimeout() 一. 区别: 1. setInterval(code,millisec) 周期性(millisec单位 ...
- axure如何实现提示框3s后自动消失
本示例基于axure8 实现 1.先做两个元件,一个按钮,一个提示框 2.将弹框“发布成功提示”设置为,页面载入时隐藏,这样预览页面时,该弹框是隐藏状态 3.给按钮添加交互样式,如下: 4.预览,点击 ...
- 3秒后自动跳转页面【js】
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
随机推荐
- BestCoder Round #90 A.Kblack loves flag(随机数生成种子)
A.Kblack loves flag [题目链接]A.Kblack loves flag [题目类型]水题 &题意: kblack喜欢旗帜(flag),他的口袋里有无穷无尽的旗帜. 某天,k ...
- Index was outside the bounds of the array. (Microsoft.SqlServer.Smo)
本地ssms是 安装Sqlserver 2008 r2 自带的 远端的server是sqlserver2014 可以连接,可以执行查询语句.但是,不能使用ssms生成对象的脚本.推测ssms 2008 ...
- day9mysql操作
#!/usr/bin/env python #coding:utf8 import MySQLdb pip install MySQL-python 先创建一个表 mysql> use xym; ...
- Converter转换器使用
package com.xu.javabean; import java.lang.reflect.InvocationTargetException; import java.util.Date; ...
- AE开发示例之RunGPAsync
using System; using System.Collections.Generic;using System.ComponentModel;using System.Data;using S ...
- poj1113
http://poj.org/problem?id=1113 完全时copy大神给的模版哦,结果再加一个小圆的周长就好啦 #include<stdio.h> #include<mat ...
- win7操作系统32位或是64位系统上安装 sql2005 点滴
在 win7 上这安装 sql2005 ,需要 .netFramewori2.0 框架,更需要 IIS 支持.否则 sqlserver2005 安装 到最后,就会失败,但此时已经在机器上安装了 .ne ...
- javascript的地基
有了良好的基础,才能在其上创造有价值的东西. 回顾一下以往自己javascript应用的开发经历,似乎很少去思考和总结js的运行机制.现在我就来整理整理 1. 以<编译原理>的一段话开头: ...
- mvc图片上传到服务器
链接:http://www.lanhusoft.com/Article/406.html
- 网页手机wap2.0网页的head里加入下面这条元标签......
网页手机wap2.0网页的head里加入下面这条元标签,在iPhone的浏览器中页面将以原始大小显示,并不允许缩放. <meta name="viewport" conten ...