本文主要是介绍通过Jquery实现轻量型吐丝消息框,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
js代码
var luckToast = function () {
context = "body";
left = 0;
toppx = 0;
time = 3000;
msgEntity = {};
width = 100;
innerinit = function () {
left = $(context).width() / 2 - msgEntity.find('span').width() / 2;
toppx = window.screen.availHeight / 2;
}
initWith = function (str) {
width = str.length * 20
}
appendDiv = function (message) {
var msgDIV = new Array();
msgDIV.push('<div id="toastMessage">');
msgDIV.push('<span>' + message + '</span>');
msgDIV.push('</div>');
msgEntity = $(msgDIV.join('')).appendTo(context + "").hide();
}
show = function (message) {
var toastObj = $("#toastMessage");
if (toastObj) {
toastObj.remove();
}
appendDiv(message);
innerinit();
initWith(message);
//设置消息样式
toastObj = $("#toastMessage");
toastObj.css({
position: 'absolute', top: '0px', 'z-index': '19999', left: '0px', 'background-color': 'black', color: 'white', 'font-size': '18px', margin: 'auto', right: "0px", bottom: "0px", width: width + "px", height: "20px", padding: "10px", "border-radius": ".5em",
"text-align": "center", "vertical-align": " middle;", "line-height": " 20px;"
});
toastObj.hide();
toastObj.fadeIn(time / 2);
toastObj.fadeOut(time / 2);
}
return { show: show };
}
function luckToast(msg) {
var M_toastMsg = new luckToast();
M_toastMsg.show(msg);
}
页面效果:
代码上传
https://download.csdn.net/download/qq_25744257/10645384
修改:
position: 'absolute',改为position: 'fixed',适应移动端
这篇关于通过Jquery实现轻量型吐丝消息框的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!