在编写项目中总会需要有个右下角弹层提示公告的需求,怎么用更简单方面,更简洁代码,实现更好用户体验这个就是我们的所要做的内容。市场这块弹层很多,但功能不尽如人意。下面分享早些时候自己编写,以及现在还在应用的自动弹层。

弹层示例图:

实现代码如下:

Css样式:

  1. /*通知提示层*/
  2. .msg_info{ font-size: 12px; text-align: left; z-index: 100; position: absolute; display: none; bottom: 0; right: 0; overflow: hidden;}
  3. .msg_info h3{float: left;margin: 0px;height: 0px;width: 100%; color: #fff; height: 30px;}
  4. .msg_info h3 span, .msg_info h3 b, .msg_info h3 em, .msg_info small span, .msg_info small b, .msg_info small em{ background-image: url(/img/msg_bg.png);}
  5. .msg_info h3 b, .msg_info h3 em, .msg_info small b, .msg_info small em{ float: left;font-size: 1px; width: 6px; height: 30px;}
  6. .msg_info h3 b{ background-position: 0px 0px;}
  7. .msg_info h3 em{ background-position: 0px -32px;}
  8. .msg_info h3 span{background-position: 0px -64px;float: left;line-height: 30px;}
  9. .msg_info h3 span font{float: left;text-align: left;overflow: hidden; margin-left: 12px;}
  10. .msg_info h3 span i{ float: right; margin-right: 10px; cursor: pointer;font-style:normal;}
  11. .message_content{ float: left;color: #515F62;overflow: hidden;border-left: solid 1px #C2C2C2; background-color: #F1F2F7; margin-top: -1px; min-height: 145px; height: auto !important; height: 145px;}
  12. .message_content div{ float: left; margin: 0px; padding: 10px 14px;height: 100%;position:relative;}
  13. .message_content div p.message_txt{ float: left;width: 100%;height: 80%;margin: 0px; padding: 0px;min-height:60px;}
  14. .message_content div i{float: left; font-style: normal; margin-top: 2px;text-align:right;position:fixed;bottom:2px;right:4px;}
  15. .message_content b.bright{ float: right; width: 1px; font-size: 1px;background-color: #C2C2C2; border-right: solid 1px #828282;height: 100%;}
  16. .msg_info small{float: left; width: 100%; height: 5px; font-size: 5px;}
  17. .msg_info small span{ background-position: 0px -101px;height: 5px; float: left;}
  18. .msg_info small b{height: 5px; background-position: 0px -96px;}
  19. .msg_info small em{ height: 5px; background-position: 0px -106px; float: right;}

Js部分:

1。自定义右下角弹层函数

  1. //右下角弹层
  2. function Messager() {
  3. this.layer = { 'width': 200, 'height': 100 };
  4. this.title = '信息提示';
  5. this.time = 4000;
  6. this.anims = { 'type': 'slide', 'speed': 600 };
  7. this.timer1 = null;
  8. this.isTiming = false;
  9. this.obj_id = "msg_" + $(document.body).find('msg_info').length;
  10. var _obj, _title, _anims, _time;
  11. _timer2 = null;
  12. //初始化
  13. this.inits = function (title, text) {
  14. _anims = this.anims;
  15. _title = title;
  16. var _html = '<div class="msg_info ' + this.obj_id + '">';
  17. _html += '  <h3>';
  18. _html += '      <b></b>';
  19. _html += '      <span class="msg_bg_middle">';
  20. _html += '          <font>' + title + '</font>';
  21. _html += '          <i class="message_close">×</i>';
  22. _html += '      </span>';
  23. _html += '      <em></em>';
  24. _html += '  </h3>';
  25. _html += '  <div class="message_content">';
  26. _html += '      <div class="msg_txt">' + text + '</div>';
  27. _html += '      <b class="bright"></b>';
  28. _html += '  </div>';
  29. _html += '  <small><b></b><span class="msg_bg_middle"></span><em></em></small>';
  30. _html += '</div>';
  31. $(document.body).prepend(_html);
  32. _obj = $("." + this.obj_id);
  33. if ($.browser.msie) {
  34. _obj.css('bottom', -5);
  35. }
  36. _obj.css('width', this.layer.width);
  37. _obj.find('.msg_bg_middle').css('width', this.layer.width - 12);
  38. _obj.find('.message_content').css('width', this.layer.width - 2);
  39. _obj.find('.msg_txt').css('width', this.layer.width - 34);
  40. _obj.find(".message_close").click(function () {
  41. setTimeout(function () { closeMsg(); }, 1);
  42. });
  43. _obj.hover(function () {
  44. clearTimeout(timer1);
  45. clearInterval(_timer2);
  46. _timer2 = timer1 = null;
  47. }, function () {
  48. timer1 = setTimeout(function () { closeMsg(); }, _time * 1000);
  49. timing(_time * 1000);
  50. });
  51. };
  52. //显示
  53. this.show = function (title, text, time) {
  54. if (title == 0 || !title) title = this.title;
  55. this.inits(title, text);
  56. if (time >= 0) this.time = time;
  57. switch (this.anims.type) {
  58. case 'slide': _obj.slideDown(this.anims.speed); break;
  59. case 'fade': _obj.fadeIn(this.anims.speed); break;
  60. case 'show': _obj.show(this.anims.speed); break;
  61. default: _obj.slideDown(this.anims.speed); break;
  62. }
  63. this.rmmessage(this.time);
  64. };
  65. //设置宽高
  66. this.lays = function (width, height) {
  67. if (width != 0 && width) this.layer.width = width;
  68. if (height != 0 && height) this.layer.height = height;
  69. };
  70. //呈现属性
  71. this.anim = function (type, speed) {
  72. if (type != 0 && type) this.anims.type = type;
  73. if (speed != 0 && speed) {
  74. switch (speed) {
  75. case 'slow': ; break;
  76. case 'fast': this.anims.speed = 200; break;
  77. case 'normal': this.anims.speed = 400; break;
  78. default: this.anims.speed = speed; break;
  79. }
  80. }
  81. };
  82. //移除层时间
  83. this.rmmessage = function (time) {
  84. if (time > 0) {
  85. timer1 = setTimeout(function () { closeMsg(); }, time);
  86. if (this.isTiming) {
  87. timing(time);
  88. }
  89. }
  90. };
  91. //计时
  92. timing = function (time) {
  93. _time = time / 1000;
  94. _timer2 = setInterval(function () {
  95. _obj.find('.msg_bg_middle').find('font').html(_title + ' [' + (--_time) + '秒后自动关闭]');
  96. }, 1000);
  97. }
  98. //关闭层
  99. closeMsg = function () {
  100. switch (_anims.type) {
  101. case 'slide': _obj.slideUp(_anims.speed); break;
  102. case 'fade': _obj.fadeOut(_anims.speed); break;
  103. case 'show': _obj.hide(_anims.speed); break;
  104. default: _obj.slideUp(_anims.speed); break;
  105. }
  106. setTimeout(function () { _obj.remove(); }, _anims.speed);
  107. }
  108. }

示例函数:

  1. var msg = '<p class="message_txt">当前有' + json.total + '个待审核用户等待您审核。</p><i>' + json.stadate + '</i>';
  2. var msgDiv = new Messager();
  3. msgDiv.isTiming = true;
  4. msgDiv.lays(300, 180);
  5. msgDiv.show("用户审核提醒", msg, 10000);

javascript右下角弹层及自动隐藏的更多相关文章

  1. javascript实现弹层效果

    首先,需要有一个按钮来模拟登录: <button id="btnLogin"class="login-btn">登录</button> ...

  2. IOS 微信返回按钮事件控制弹层关闭还是返回上一页

    在微信公共号内绑定域名后或微信内打开第三方链接跳转非单页面网站时,经常会有弹层Modal的需求,此时如果用户习惯性点击微信自带的返回“<”按钮,就会跳转回上一页或退出网站,而为了避免这种不好的误 ...

  3. 弹层,iframe页面

    前台页面: <img src="chb/老玩家 好礼送.jpg" border="0" width="202" height=&quo ...

  4. mui popover 自定义 弹出位置 显示 隐藏

    mui popover 一.要显示.隐藏弹出菜单插件,mui推荐使用锚点方式. 1.页面顶部导航栏.底部工具栏固定位置 <header class="mui-bar mui-bar-n ...

  5. 弹层组件-layer

    layer是Layui的一个弹层组建,功能强大,总之我很喜欢,下面介绍这个组件的基本用法. 首先如果只需要使用layer而不想使用Layui可以单独下载layer组件包,页面引入jquery1.8以上 ...

  6. 富文本编辑器UEditor自定义工具栏(二、插入图片、音频、视频个性化功能按钮和弹层及自定义分页符)

    导读:本篇将简单探讨插入图片.音频.视频的功能按钮实现方式 传送门:富文本编辑器UEditor自定义工具栏(一.基础配置与字体.背景色.行间距.超链接实现) 一.效果图 1.UEditor自定义工具栏 ...

  7. layer,一个可以让你想到即可做到的javascript弹窗(层)解决方案

    学习网址:http://layer.layui.com/ 下载地址:http://res.layui.com/download/layer-v2.1.zip 我们提到的基础参数主要指调用方法时用到的配 ...

  8. FineUI window弹层设置

    需在页面先设置   <f:Window ID="Window1" runat="server" Height="600px" Widt ...

  9. Bootstrap~大叔封装的弹层

    回到目录 对于Bootstrap的弹层,插件有很多,今天主要用的是它自带的功能,通过bootstrap提供的模式窗口来实现的,而大叔主要对使用方法进行了封装,开发人员可以自己动态传入弹层的HTML内容 ...

随机推荐

  1. 清除localstorage

    h5本地存储localStorage,sessionStorage. localStorage是没有失效时间的,sessionStorage的声明周期是浏览器的生命周期. 当浏览器关闭时,sessio ...

  2. 面试的角度诠释Java工程师

    原文出处: locality 一.基础篇 1.面向对象的三大特性 继承.封装.多态 什么是继承?①继承是面向对象程序设计能够提高软件开发效率的重要原因之一.②继承是具有传递性的,就像现实中孙子不仅长得 ...

  3. npm 的使用指南

    npm 使用指南 因为有写关于node.js的配置的博客,还有node和gulp的前端信息配置使用,其中有很多命令都用到了npm.所以这里要着重介绍一下npm. 1 npm介绍 npm(mode pa ...

  4. 使用Java代码发送SMTP邮件

    package cn.Douzi.send; import javax.mail.Session; import javax.mail.Transport; import javax.mail.int ...

  5. 区分IE8 、IE9 的专属css hack

    一般来说,我们写的结构比较好的时候,IE8/9下是没区别的.所以可能很少人关注只有IE8或只有IE9才识别的css hack. 因为IE8及以下版本是不支持CSS3的,但是我们如果使用css3,在IE ...

  6. 【NOI2017】游戏 2-sat算法

    [题目]LibreOJ [题意]n场游戏,有三种车ABC,给定长度为n的字符串,'a'表示不能选A,'b''c'同理,'x'表示不限,至多d个'x'.有m个限制(i,hi,j,hj)表示如果第i场选择 ...

  7. 【CodeForces】899 E. Segments Removal

    [题目]E. Segments Removal [题意]给定n个数字,每次操作删除最长的连续相同数字(等长删最左),求全部删完的最少次数.n<=2*10^6,1<=ai<=10^9. ...

  8. laravel前台html代码不显示

    后天向前台传输变量,如果能取到变量数据,还有代码,但是不显示图片 可以把{{}}换成{!!     !!}试试.

  9. Django【设计】同功能不同实现模式的兼容性

    需求: 当我们采集硬件信息时,客户端可以有多种方式,具体方式取决于客户机,CMDB项目中,我们有三种方式可选,AGENT/SSH/SALT,根据客户机具体情况和“SALT>>SSH> ...

  10. Redis—初探Redis

    一.什么是Redis? 学习Redis最好的是看官网了,下面是Redis的官网对Redis的介绍 可见,Redis是一个内存存储的数据结构服务器,可以用作数据库.缓存等.支持的数据结构也很丰富,有字符 ...