menu

Materialize provides an easy way for you to send unobtrusive alerts to your users through toasts. These toasts are also placed and sized responsively, try it out by clicking the button below on different device sizes.

Toast!

To do this, call the M.toast() function programatically in JavaScript.


  M.toast({html: 'I am a toast!'})
        

One way to add this into your application is to add this as an onclick event to a button.


  <a onclick="M.toast({html: 'I am a toast'})" class="btn">Toast!</a>
        

选项

You can customize the behavior of each Toast using these options.

名称 类型 默认 描述
html String '' The HTML content of the Toast.
displayLength Number 4000 Length in ms the Toast stays before dismissal.
inDuration Number 300 Transition in duration in milliseconds.
outDuration Number 375 Transition out duration in milliseconds.
classes String '' Classes to be added to the toast element.
completeCallback Function null Callback function called when toast is dismissed.
activationPercent Number 0.8 The percentage of the toast's width it takes for a drag to dismiss a Toast.

属性

名称 类型 描述
el Element The toast element.
options Object 初始化实例时使用的选项。
panning Boolean Describes the current pan state of the Toast.
timeRemaining Number The remaining amount of time in ms that the toast will stay before dismissal.

自定义HTML结构

You can pass in an HTML String as the first argument as well. Take a look at the example below, where we pass in text as well as a flat button. If you call an external function instead of in-line JavaScript, you will not need to escape quotation marks.

Toast with Action

  var toastHTML = '<span>I am toast content</span><button class="btn-flat toast-action">Undo</button>';
  M.toast({html: toastHTML});
        

回调

You can have the toast callback a function when it has been dismissed.

Toast!

  <a class="btn" onclick="M.toast({html: 'I am a toast', completeCallback: function(){alert('Your toast was dismissed')}})">Toast!</a>
        

吐司样式

We've added the ability to customize your toasts easily. You can pass in classes as an optional parameter into the toast function. We've added a rounded class for you, but you can create your own CSS classes and apply them to toasts. Checkout out our full example below.

Round Toast!

  // 'rounded' is the class I'm applying to the toast
  M.toast({html: 'I am a toast!', classes: 'rounded'});
        

以程序方式取消吐司

To remove a specific toast using JavaScript, access the M_Toast toast HTML element and call the dismiss function


  // Get toast DOM Element, get instance, then call dismiss function
  var toastElement = document.querySelector('.toast');
  var toastInstance = M.Toast.getInstance(toastElement);
  toastInstance.dismiss();
        
移除所有吐司

  M.Toast.dismissAll();