javascript右下角弹层及自动隐藏
在编写项目中总会需要有个右下角弹层提示公告的需求,怎么用更简单方面,更简洁代码,实现更好用户体验这个就是我们的所要做的内容。市场这块弹层很多,但功能不尽如人意。下面分享早些时候自己编写,以及现在还在应用的自动弹层。
弹层示例图:
实现代码如下:
Css样式:
- /*通知提示层*/
- .msg_info{ font-size: 12px; text-align: left; z-index: 100; position: absolute; display: none; bottom: 0; right: 0; overflow: hidden;}
- .msg_info h3{float: left;margin: 0px;height: 0px;width: 100%; color: #fff; height: 30px;}
- .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);}
- .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;}
- .msg_info h3 b{ background-position: 0px 0px;}
- .msg_info h3 em{ background-position: 0px -32px;}
- .msg_info h3 span{background-position: 0px -64px;float: left;line-height: 30px;}
- .msg_info h3 span font{float: left;text-align: left;overflow: hidden; margin-left: 12px;}
- .msg_info h3 span i{ float: right; margin-right: 10px; cursor: pointer;font-style:normal;}
- .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;}
- .message_content div{ float: left; margin: 0px; padding: 10px 14px;height: 100%;position:relative;}
- .message_content div p.message_txt{ float: left;width: 100%;height: 80%;margin: 0px; padding: 0px;min-height:60px;}
- .message_content div i{float: left; font-style: normal; margin-top: 2px;text-align:right;position:fixed;bottom:2px;right:4px;}
- .message_content b.bright{ float: right; width: 1px; font-size: 1px;background-color: #C2C2C2; border-right: solid 1px #828282;height: 100%;}
- .msg_info small{float: left; width: 100%; height: 5px; font-size: 5px;}
- .msg_info small span{ background-position: 0px -101px;height: 5px; float: left;}
- .msg_info small b{height: 5px; background-position: 0px -96px;}
- .msg_info small em{ height: 5px; background-position: 0px -106px; float: right;}
Js部分:
1。自定义右下角弹层函数
- //右下角弹层
- function Messager() {
- this.layer = { 'width': 200, 'height': 100 };
- this.title = '信息提示';
- this.time = 4000;
- this.anims = { 'type': 'slide', 'speed': 600 };
- this.timer1 = null;
- this.isTiming = false;
- this.obj_id = "msg_" + $(document.body).find('msg_info').length;
- var _obj, _title, _anims, _time;
- _timer2 = null;
- //初始化
- this.inits = function (title, text) {
- _anims = this.anims;
- _title = title;
- var _html = '<div class="msg_info ' + this.obj_id + '">';
- _html += ' <h3>';
- _html += ' <b></b>';
- _html += ' <span class="msg_bg_middle">';
- _html += ' <font>' + title + '</font>';
- _html += ' <i class="message_close">×</i>';
- _html += ' </span>';
- _html += ' <em></em>';
- _html += ' </h3>';
- _html += ' <div class="message_content">';
- _html += ' <div class="msg_txt">' + text + '</div>';
- _html += ' <b class="bright"></b>';
- _html += ' </div>';
- _html += ' <small><b></b><span class="msg_bg_middle"></span><em></em></small>';
- _html += '</div>';
- $(document.body).prepend(_html);
- _obj = $("." + this.obj_id);
- if ($.browser.msie) {
- _obj.css('bottom', -5);
- }
- _obj.css('width', this.layer.width);
- _obj.find('.msg_bg_middle').css('width', this.layer.width - 12);
- _obj.find('.message_content').css('width', this.layer.width - 2);
- _obj.find('.msg_txt').css('width', this.layer.width - 34);
- _obj.find(".message_close").click(function () {
- setTimeout(function () { closeMsg(); }, 1);
- });
- _obj.hover(function () {
- clearTimeout(timer1);
- clearInterval(_timer2);
- _timer2 = timer1 = null;
- }, function () {
- timer1 = setTimeout(function () { closeMsg(); }, _time * 1000);
- timing(_time * 1000);
- });
- };
- //显示
- this.show = function (title, text, time) {
- if (title == 0 || !title) title = this.title;
- this.inits(title, text);
- if (time >= 0) this.time = time;
- switch (this.anims.type) {
- case 'slide': _obj.slideDown(this.anims.speed); break;
- case 'fade': _obj.fadeIn(this.anims.speed); break;
- case 'show': _obj.show(this.anims.speed); break;
- default: _obj.slideDown(this.anims.speed); break;
- }
- this.rmmessage(this.time);
- };
- //设置宽高
- this.lays = function (width, height) {
- if (width != 0 && width) this.layer.width = width;
- if (height != 0 && height) this.layer.height = height;
- };
- //呈现属性
- this.anim = function (type, speed) {
- if (type != 0 && type) this.anims.type = type;
- if (speed != 0 && speed) {
- switch (speed) {
- case 'slow': ; break;
- case 'fast': this.anims.speed = 200; break;
- case 'normal': this.anims.speed = 400; break;
- default: this.anims.speed = speed; break;
- }
- }
- };
- //移除层时间
- this.rmmessage = function (time) {
- if (time > 0) {
- timer1 = setTimeout(function () { closeMsg(); }, time);
- if (this.isTiming) {
- timing(time);
- }
- }
- };
- //计时
- timing = function (time) {
- _time = time / 1000;
- _timer2 = setInterval(function () {
- _obj.find('.msg_bg_middle').find('font').html(_title + ' [' + (--_time) + '秒后自动关闭]');
- }, 1000);
- }
- //关闭层
- closeMsg = function () {
- switch (_anims.type) {
- case 'slide': _obj.slideUp(_anims.speed); break;
- case 'fade': _obj.fadeOut(_anims.speed); break;
- case 'show': _obj.hide(_anims.speed); break;
- default: _obj.slideUp(_anims.speed); break;
- }
- setTimeout(function () { _obj.remove(); }, _anims.speed);
- }
- }
示例函数:
- var msg = '<p class="message_txt">当前有' + json.total + '个待审核用户等待您审核。</p><i>' + json.stadate + '</i>';
- var msgDiv = new Messager();
- msgDiv.isTiming = true;
- msgDiv.lays(300, 180);
- msgDiv.show("用户审核提醒", msg, 10000);
javascript右下角弹层及自动隐藏的更多相关文章
- javascript实现弹层效果
首先,需要有一个按钮来模拟登录: <button id="btnLogin"class="login-btn">登录</button> ...
- IOS 微信返回按钮事件控制弹层关闭还是返回上一页
在微信公共号内绑定域名后或微信内打开第三方链接跳转非单页面网站时,经常会有弹层Modal的需求,此时如果用户习惯性点击微信自带的返回“<”按钮,就会跳转回上一页或退出网站,而为了避免这种不好的误 ...
- 弹层,iframe页面
前台页面: <img src="chb/老玩家 好礼送.jpg" border="0" width="202" height=&quo ...
- mui popover 自定义 弹出位置 显示 隐藏
mui popover 一.要显示.隐藏弹出菜单插件,mui推荐使用锚点方式. 1.页面顶部导航栏.底部工具栏固定位置 <header class="mui-bar mui-bar-n ...
- 弹层组件-layer
layer是Layui的一个弹层组建,功能强大,总之我很喜欢,下面介绍这个组件的基本用法. 首先如果只需要使用layer而不想使用Layui可以单独下载layer组件包,页面引入jquery1.8以上 ...
- 富文本编辑器UEditor自定义工具栏(二、插入图片、音频、视频个性化功能按钮和弹层及自定义分页符)
导读:本篇将简单探讨插入图片.音频.视频的功能按钮实现方式 传送门:富文本编辑器UEditor自定义工具栏(一.基础配置与字体.背景色.行间距.超链接实现) 一.效果图 1.UEditor自定义工具栏 ...
- layer,一个可以让你想到即可做到的javascript弹窗(层)解决方案
学习网址:http://layer.layui.com/ 下载地址:http://res.layui.com/download/layer-v2.1.zip 我们提到的基础参数主要指调用方法时用到的配 ...
- FineUI window弹层设置
需在页面先设置 <f:Window ID="Window1" runat="server" Height="600px" Widt ...
- Bootstrap~大叔封装的弹层
回到目录 对于Bootstrap的弹层,插件有很多,今天主要用的是它自带的功能,通过bootstrap提供的模式窗口来实现的,而大叔主要对使用方法进行了封装,开发人员可以自己动态传入弹层的HTML内容 ...
随机推荐
- bzoj 2795 [Poi2012]A Horrible Poem hash+数论
2795: [Poi2012]A Horrible Poem Time Limit: 50 Sec Memory Limit: 128 MBSubmit: 640 Solved: 322[Subm ...
- APP的数据采集与埋点方式分析
前言: 神策数据写过几篇分析APP前后端埋点的文章,原文在此: https://sensorsdata.cn/blog/shu-ju-jie-ru-yu-mai-dian/ http://www.wo ...
- maven 依赖、聚合和继承 (转)
Maven 插件和仓库 Maven 本质上是一个插件框架,它的核心并不执行任何具体的构建任务,仅仅定义了抽象的生命周期,所有这些任务都交给插件来完成的.每个插件都能完成至少一个任务,每个任务即是一个功 ...
- CS48 D BIT
统计一个点对应的和它严格右下方的点,点对数量.由于数据规模很大,不能直接上二维的前缀和,先排一维序,然后用BIT维护前缀和即可. /** @Date : 2017-09-14 20:17:30 * @ ...
- 【IDEA】 Can't Update No tracked branch configured for branch master or the branch doesn't exist. To make your branch track a remote branch call, for example, git branch --set-upstream-to origin/master
IDEA点击GIT更新按钮时,报错如下: Can't UpdateNo tracked branch configured for branch master or the branch doesn' ...
- JavaScript 数组操作:slice vs splice
在 JavaScript 中,对于数组的操作有两个很容易混淆的方法 splice, slice ,这里给大家推荐一篇介绍 splice, slice 二者区别的文章. In JavaScript, m ...
- 在C++11中实现监听者模式
参考文章:https://coderwall.com/p/u4w9ra/implementing-signals-in-c-11 最近在完成C++大作业时,碰到了监听者模式的需求. 尽管C++下也可以 ...
- 在Unity中实现屏幕空间反射Screen Space Reflection(3)
本篇讲一下相交检测的优化.有两个措施. 线段相交检测 之前的检测都是检测光线的终点是否在物体内.我们可以尝试检测光线的线段是否与物体相交. 比如说有一个非常薄的物体,光线差不多垂直于它的表面.如果用普 ...
- python模块之imghdr检测图片类型
1. imghdr是什么 imghdr是一个用来检测图片类型的模块,传递给它的可以是一个文件对象,也可以是一个字节流. 能够支持的图片格式: 2. 如何使用 提供了一个api叫做imghdr.what ...
- Django框架<一>
Django框架 Python的WEB框架有Django.Tornado.Flask 等多种,Django相较与其他WEB框架其优势为:大而全,框架本身集成了ORM.模型绑定.模板引擎.缓存.Sess ...