原生js中添加定时消失的提示框

方法一:推荐使用 弹层组件文档 - layui.layer,他家的弹窗类似于自定义内容,上手也很容易,js和css引入,直接粘贴添加自己的内容即可用,例如自定义弹窗:

代码:

layer.open({
  title: '提示'
  ,content: 'hello'
});     
  

一段时间后自动消失且不需要任何操作的提示框:

代码:

layer.msg('hello'); 

官网给的示例代码:

//信息框-例1

layer.alert('见到你真的很高兴', {icon: 6});
 
//信息框-例2

layer.msg('你确定你很帅么?', {
  time: 0 //不自动关闭
  ,btn: ['必须啊', '丑到爆']
  ,yes: function(index){
    layer.close(index);
    layer.msg('雅蠛蝶 O.o', {
      icon: 6
      ,btn: ['嗷','嗷','嗷']
    });
  }
});
//信息框-例3

layer.msg('这是最常用的吧');
//信息框-例4

layer.msg('不开心。。', {icon: 5});
//信息框-例5

layer.msg('玩命卖萌中', function(){
//关闭后的操作
});
//页面层-自定义

layer.open({
  type: 1,
  title: false,
  closeBtn: 0,
  shadeClose: true,
  skin: 'yourclass',
  content: '自定义HTML内容'
});
//页面层-图片

layer.open({
  type: 1,
  title: false,
  closeBtn: 0,
  area: ['auto'],
  skin: 'layui-layer-nobg', //没有背景色
  shadeClose: true,
  content: $('#tong')
});
//iframe层-父子操作

layer.open({
  type: 2,
  area: ['700px', '450px'],
  fixed: false, //不固定
  maxmin: true,
  content: 'test/iframe.html'
});
//iframe层-多媒体

layer.open({
  type: 2,
  title: false,
  area: ['630px', '360px'],
  shade: 0.8,
  closeBtn: 0,
  shadeClose: true,
  content: '//player.youku.com/embed/XMjY3MzgzODg0'
});
layer.msg('点击任意处关闭');
//iframe层-禁滚动条

layer.open({
  type: 2,
  area: ['360px', '500px'],
  skin: 'layui-layer-rim', //加上边框
  content: ['mobile/', 'no']
});
//加载层-默认风格

layer.load();
//此处演示关闭
setTimeout(function(){
  layer.closeAll('loading');
}, 2000);
//加载层-风格2

layer.load(1);
//此处演示关闭
setTimeout(function(){
  layer.closeAll('loading');
}, 2000);
//加载层-风格3

layer.load(2);
//此处演示关闭
setTimeout(function(){
  layer.closeAll('loading');
}, 2000);
//加载层-风格4

layer.msg('加载中', {
  icon: 16
  ,shade: 0.01
});
//打酱油

layer.msg('尼玛,打个酱油', {icon: 4});
//tips层-上

layer.tips('上', '#id或者.class', {
  tips: [1, '#0FA6D8'] //还可配置颜色
});
//tips层-右

layer.tips('默认就是向右的', '#id或者.class');
//tips层-下

layer.tips('下', '#id或者.class', {
  tips: 3
});
//tips层-左

layer.tips('左边么么哒', '#id或者.class', {
  tips: [4, '#78BA32']
});
//tips层-不销毁之前的

layer.tips('不销毁之前的', '#id或者.class', {
  tipsMore: true
});
//默认prompt

layer.prompt(function(val, index){
  layer.msg('得到了'+val);
  layer.close(index);
});
//屏蔽浏览器滚动条

layer.open({
  content: '浏览器滚动条已锁',
  scrollbar: false
});
//弹出即全屏

var index = layer.open({
  type: 2,
  content: 'http://layim.layui.com',
  area: ['320px', '195px'],
  maxmin: true
});
layer.full(index);
//正上方

layer.msg('灵活运用offset', {
  offset: 't',
  anim: 6
});
//更多例子 
layer.msg('Hi');
  

这里,面应该有你需要的那一款。

方法二:自定义一段时间后自己消失的提示框:

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<body>
    <!-- 点击按钮,传递显示数据 -->
	<button id="btn" onclick="func('修改成功')" οnclick>btn</button>
</body>
<script>
    // 创建一个div
    var lunbo=document.createElement("div");
    // 设置div的id值
    lunbo.id="lunbo";
    // 上面按钮点击事件
    function func(date){
        /* 创建div的样式,宽200px,高80px,下面的是css样式居中,
         * css样式居中具体了解链接:https://blog.csdn.net/A_Bear/article/details/80546181
         */
        var style={
            background:"#f00",
            position:"absolute",
            zIndex:10,
            width:"200px",
            height:"80px",
            left:"50%",
            top:"50%",
            marginLeft:"-100px",
            marginTop:"-40px",
			color:"white"
        }
        for(var i in style)
            lunbo.style[i]=style[i];   
        // 当找不到id为lunbo的控件时
        if(document.getElementById("lunbo")==null){
            // 在body中添加lunbo控件(lunbo在上面创建的)
            document.body.appendChild(lunbo);
            // 显示内容
            lunbo.innerHTML=date;
            // 文本居中
            lunbo.style.textAlign="center";
            lunbo.style.lineHeight="80px"; // 作用是调节字体行高与div同高,使其保持水平居中
            // 设置2s后去掉弹出窗
            setTimeout("document.body.removeChild(lunbo)",2000)
        }
    }
</script>
</html>

效果:

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

尔嵘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值