仿安卓的手機網頁版toast
阿新 • • 發佈:2019-02-16
/**
* 模仿android裡面的Toast效果,主要是用於在不打斷程式正常執行的情況下顯示提示資料
* @param config
* @return
*/
var Toast = function(config){
this.context = config.context==null?$('body'):config.context;//上下文
this.message = config.message;//顯示內容
this.time = config.time==null?6000:config.time;//持續時間
this.left = config.left;//距容器左邊的距離
this.top = config.top;//距容器上方的距離
this.init();
}
var msgEntity;
Toast.prototype = {
//初始化顯示的位置內容等
init : function(){
$("#toastMessage").remove();
//設定訊息體
var msgDIV = new Array();
msgDIV.push('<div id="toastMessage">');
msgDIV.push('<span>'+this.message+'</span>');
msgDIV.push('</div>');
msgEntity = $(msgDIV.join('')).appendTo(this.context);
//設定訊息樣式
var toastMessageWidth = $('#toastMessage').innerWidth();
var toastMessageHeight = $('#toastMessage').innerHeight();
var windowWidth = $(window).width();
var windowHeight = $(window).height();
var newWidth = (windowWidth - toastMessageWidth - 80) / 2 + "px";
var newHeight = (windowHeight - toastMessageHeight - 80) / 2 + "px";
msgEntity.css({'left':newWidth,'z-index':'999999','top':newHeight,'background-color':'black','color':'white',
'padding':'30px','font-size':'18px','border':'3px solid #f8c26d'});
},
//顯示動畫
show :function(){
msgEntity.fadeIn(this.time/2);
msgEntity.fadeOut(this.time/2);
}
}
function toastFunction(messageString){
new Toast({context:$('body'),message:messageString}).show();
}
* 模仿android裡面的Toast效果,主要是用於在不打斷程式正常執行的情況下顯示提示資料
* @param config
* @return
*/
var Toast = function(config){
this.context = config.context==null?$('body'):config.context;//上下文
this.message = config.message;//顯示內容
this.time = config.time==null?6000:config.time;//持續時間
this.left = config.left;//距容器左邊的距離
this.top = config.top;//距容器上方的距離
this.init();
}
var msgEntity;
Toast.prototype = {
//初始化顯示的位置內容等
init : function(){
$("#toastMessage").remove();
//設定訊息體
var msgDIV = new Array();
msgDIV.push('<div id="toastMessage">');
msgDIV.push('<span>'+this.message+'</span>');
msgDIV.push('</div>');
msgEntity = $(msgDIV.join('')).appendTo(this.context);
//設定訊息樣式
var toastMessageWidth = $('#toastMessage').innerWidth();
var toastMessageHeight = $('#toastMessage').innerHeight();
var windowWidth = $(window).width();
var windowHeight = $(window).height();
var newWidth = (windowWidth - toastMessageWidth - 80) / 2 + "px";
var newHeight = (windowHeight - toastMessageHeight - 80) / 2 + "px";
msgEntity.css({'left':newWidth,'z-index':'999999','top':newHeight,'background-color':'black','color':'white',
'padding':'30px','font-size':'18px','border':'3px solid #f8c26d'});
},
//顯示動畫
show :function(){
msgEntity.fadeIn(this.time/2);
msgEntity.fadeOut(this.time/2);
}
}
function toastFunction(messageString){
new Toast({context:$('body'),message:messageString}).show();
}