当前位置: 移动技术网 > IT编程>开发语言>JavaScript > javascript右下角弹层及自动隐藏实现方法

javascript右下角弹层及自动隐藏实现方法

2019年05月08日  | 移动技术网IT编程  | 我要评论

知己交友,巴金 鸟的天堂,tara组合成员

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

弹层示例图:
\ 
实现代码如下:

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 p{ float: left; margin: 0px; padding: 10px 14px;height: 100%;position:relative;}
.message_content p p.message_txt{ float: left;width: 100%;height: 80%;margin: 0px; padding: 0px;min-height:60px;}
.message_content p 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 = '<p 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 += ' <p class="message_content">';
_html += ' <p class="msg_txt">' + text + '</p>';
_html += ' <b class="bright"></b>';
_html += ' </p>';
_html += ' <small><b></b><span class="msg_bg_middle"></span><em></em></small>';
_html += '</p>';
$(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);

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网