jquery-toastmessage-plugin(仿安卓消息提示插件)
【下载地址】---》jquery-toastmessage-plugin.zip
【应用范围】
网页中的消息提示,哪个角度都可以,而且效果非常漂亮。
【使用体验】
啥也不说,先添加JS库
<script type="text/javascript" src="javascript/lib/jquery-1.5.min.js"></script>
<script type="text/javascript" src="javascript/jquery.toastmessage.js"></script>
同时,我们也添加一个toasmessage已经为我么准备好的css样式
<link href="resources/css/jquery.toastmessage.css" type="text/css" rel="stylesheet"/>
这里面已经为我们设置了四种toasmessage状态:
Notice、success、warning、Error,我们可以直接使用。
下面看看toastmessage的调用函数是什么:
$().toastmessage('showNoticeToast', 'some message here'); //调用Notice状态
$().toastmessage('showSuccessToast', "some message here"); //调用Success状态
$().toastmessage('showWarningToast', "some message here"); //调用Warning状态
$().toastmessage('showErrorToast', "some message here"); //调用Error状态
这是最基本的调用方式,调用的toastmessage会在浏览器的右上方(多个message不会重叠),并且在你不关闭的情况下会自动消失。
再看看$().toastmessage('removeToast', toastObject);
通过这句话,可以移除特定的toastmessage。
我们写一个例子看看这两种方式的效果:
<script type="text/javascript">
<!--
var toastObj;
$().toastmessage({sticky:true});//为了方便测试,我们让toast不自动消失
function showToast()
{
toastObj = $().toastmessage("showNoticeToast","Hello,Notice!");
}
function removeToast()
{
if(toastObj)
{
$().toastmessage("removeToast",toastObj);
}
}
-->
</script>
<a href="#" οnclick="showToast()">显示一个Toast</a>
<a href="#" οnclick="removeToast()">移除一个Toast</a>
OK,就这么简单我们就可以用toastmessage了,那么下面看看如何详细的配置我们自己的toastmessage显示方式
$().toastmessage({
sticky:true
});
这就是配置toastmessage的方式,下面我们就详细的看看有些选项可以配置:
inEffectDuration:动画效果显示的时间,毫秒为单位,默认为600;
stayTime:toast停留的时间,毫秒为单位,默认为3000;
text:toastmessage的显示内容,默认为空;
sticky:是否始终显示,默认为false;
type:显示的类型,默认为notice;
postion:显示的位置,可以选择top-left|top-center|top-right|middle-left|middle-center|middle-right,默认为top-right,而且这里的位置是第一个toastmessage显示的位置,后面的位置要根据第一个的位置计算,等待前一个结束显示后自动上移;
closeText:显示在关闭按钮处的文字,默认为空,个人意见最好为空;
close:关闭触发的事件,默认为null。
简单的写一个Demo
$().toastmessage({
text : 'Hello World',
sticky : true,
position : 'top-right',
type : 'success',
close : function () {console.log("toast is closed ...");}
});
上面我们看到的是全局设置,我们也可以在显示toastmessage时进行单独的设置:
$().toastmessage('showToast', {
text : 'Some information for you ...',
sticky : true,
type : 'notice'
});
下面我们看看如何创建自己的toast
首先我们先来定义我们的toast的显示样式,找到resources/css/jquery-toastmessage.css
添加这么两个类的定义
.toast-item-image-yourToastName {
background:url(../images/notice.png);
}//设置显示的图标
.toast-type- yourToastName {
color: white;
}//设置背景色、字体等
好的下面我们在找到javascript/jquery-toastmessage.js
showWarningToast :function...下面添加我们自己的显示函数
showYourToastNameToast : function (message)
{
var options = {text : message, type : 'YourToastName'};
return $().toastmessage('showToast', options);
},
OK,替换掉yourToastName为你设置的名称,注意名称要完全一直,就可以在代码中使用了,非常简单使用。
【体验感受】
很Happy的一个plugin,使用简单的要命啊,但效果十分强大,而且自定义效果很好,强烈建议使用,并且建立自己的toastmessage标签库。