仿安卓的手機網頁版toast

來源:互聯網
上載者:User

標籤:html   javascript   手機   jquery   css   

1.先寫好toast的js代碼:

/**
 * 模仿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();
    }

2.先在你的html首頁中添加jquery庫和一下樣式:

<style type="text/css">
     #toastMessage{
        position: absolute;
        border-radius: 15px;
        cursor:pointer;
     }
</style>

3.如何調用:

toastFunction("成功調用!");


仿安卓的手機網頁版toast

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.