javascript右下角弹层及自动隐藏(自己编写)


Posted in Javascript onNovember 20, 2013

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

弹层示例图:
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部分:

自定义右下角弹层函数

//右下角弹层 
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 相关文章推荐
extjs中grid中嵌入动态combobox的应用
Jan 01 Javascript
关闭浏览器输入框自动补齐 兼容IE,FF,Chrome等主流浏览器
Feb 11 Javascript
javascript实现节点(div)名称编辑
Dec 17 Javascript
轻松学习jQuery插件EasyUI EasyUI创建RSS Feed阅读器
Nov 30 Javascript
JavaScript 模块的循环加载实现方法
Dec 13 Javascript
4种JavaScript实现简单tab选项卡切换的方法
Jan 06 Javascript
Express之get,pos请求参数的获取
May 02 Javascript
微信小程序 图片上传实例详解
May 05 Javascript
react-native 圆弧拖动进度条实现的示例代码
Apr 12 Javascript
详解webpack引入第三方库的方式以及注意事项
Jan 15 Javascript
layer ui 导入文件之前传入数据的实例
Sep 23 Javascript
JS回调函数简单易懂的入门实例分析
Sep 29 Javascript
jQuery 快速结束当前正在执行的动画
Nov 20 #Javascript
js中的布尔运算符使用介绍
Nov 20 #Javascript
浅析hasOwnProperty方法的应用
Nov 20 #Javascript
鼠标滚轮改变图片大小的示例代码
Nov 20 #Javascript
JS画线(实例代码)
Nov 20 #Javascript
解析offsetHeight,clientHeight,scrollHeight之间的区别
Nov 20 #Javascript
JS事件在IE与FF中的区别详细解析
Nov 20 #Javascript
You might like
PHP 数组实例说明
2008/08/18 PHP
php include,include_once,require,require_once
2008/09/05 PHP
php 小乘法表实现代码
2009/07/16 PHP
PHP更新购物车数量(表单部分/PHP处理部分)
2013/05/03 PHP
使用PHPMailer实现邮件发送代码分享
2014/10/23 PHP
浅谈PHP中Stream(流)
2015/06/08 PHP
PHP框架laravel的.env文件配置教程
2017/06/07 PHP
laravel-admin 管理平台获取当前登陆用户信息的例子
2019/10/08 PHP
js实现iframe动态调整高度的代码
2008/01/06 Javascript
jquery基础知识第一讲之认识jquery
2016/03/17 Javascript
jQuery实现表格隔行及滑动,点击时变色的方法【测试可用】
2016/08/20 Javascript
Vue 页面状态保持页面间数据传输的一种方法(推荐)
2018/11/01 Javascript
详解Vue SSR( Vue2 + Koa2 + Webpack4)配置指南
2018/11/13 Javascript
利用Dectorator分模块存储Vuex状态的实现
2019/02/05 Javascript
Vue程序化的事件监听器(实例方案详解)
2020/01/07 Javascript
[06:20]2015国际邀请赛第三日top10
2015/08/08 DOTA
python中mechanize库的简单使用示例
2014/01/10 Python
Python Web框架Flask中使用新浪SAE云存储实例
2015/02/08 Python
python实现简单socket程序在两台电脑之间传输消息的方法
2015/03/13 Python
Python的Twisted框架中使用Deferred对象来管理回调函数
2016/05/25 Python
解决python3在anaconda下安装caffe失败的问题
2017/06/15 Python
Python中反射和描述器总结
2018/09/23 Python
python 函数中的内置函数及用法详解
2019/07/02 Python
Python tkinter之ComboBox(下拉框)的使用简介
2021/02/05 Python
CSS3实现的闪烁跳跃进度条示例(附源码)
2013/08/19 HTML / CSS
花园仓库建筑:Garden Buildings Direct
2018/02/16 全球购物
计算机通信工程专业毕业生推荐信
2013/12/24 职场文书
学生实习介绍信
2014/01/15 职场文书
辩论赛主持词
2014/03/18 职场文书
网络技术专业求职信
2014/07/13 职场文书
2014年双拥工作总结
2014/11/21 职场文书
工程部主管岗位职责
2015/02/12 职场文书
校园安全学习心得体会
2016/01/18 职场文书
小学语文教学反思范文
2016/03/03 职场文书
浅谈Python类的单继承相关知识
2021/05/12 Python
聊聊SpringBoot自动装配的魔力
2021/11/17 Java/Android