倒计时条和平滑动画创建Toast通知

  • 源码大小:11.31KB
  • 所需积分:1积分
  • 源码编号:19JP-3571
  • 浏览次数:564次
  • 最后更新:2023年06月27日
  • 所属栏目:其他
本站默认解压密码:19jp.com 或 19jp_com

简介

toast通知是根据时间和上下文条件显示在当前应用程序外部的对用户的警告。

jQuery插件的目的是提供一种快速的方法来创建带有倒计时条和平滑滑入/滑出动画的自定义Android风格的吐司通知。

它非常易于使用,可以轻松地在移动网站甚至单页应用程序中使用。

如何使用它:

1.在文档的头部分加载主样式表。

  1. <link rel="stylesheet" href="css/styles.css">

2.为toast通知编写HTML代码。

  1. <div id="toast" class="toast">
  2. <div class="toast-content">
  3. <!-- Notification Icon -->
  4. <i class="icon">
  5. </i>
  6. <div class="menssagem">
  7. <!-- Default Message -->
  8. <span class="text text-1">sucess</span>
  9. <span class="text text-2">Salvo com sucesso</span>
  10. </div>
  11. </div>
  12. <!-- Close Button -->
  13. <i class="icon-close">
  14. <svg stroke="currentColor" fill="none" stroke-width="2" viewBox="0 0 24 24" stroke-linecap="round" stroke-linejoin="round" height="1em" width="1em" xmlns="http://www.w3.org/2000/svg"><line x1="18" y1="6" x2="6" y2="18"></line><line x1="6" y1="6" x2="18" y2="18"></line></svg>
  15. </i>
  16. <!-- Progress Bar -->
  17. <div class="progress">
  18. </div>
  19. </div>

3.在文档末尾加载最新的jQuery JavaScript库。

  1. <script src="/path/to/cdn/jquery.slim.min.js"></script>

4.toast通知的主要JavaScript。

  1. var toast_notification = {
  2. // show toast
  3. show_toast:function ({title, message, time, type}) {
  4. var container_icon = $('#toast .icon');
  5. var progress = $('#toast .progress');
  6. var toast = $('#toast')
  7. $(toast).addClass('visible');
  8. $(progress).addClass('visible');
  9. toast_notification.addMessage(title, message);
  10. var root = document.documentElement;
  11. switch (type) {
  12. case 'success':
  13. var existMoreone = $(container_icon).find('svg').length;
  14. if (existMoreone > 0) {
  15. $(container_icon).find('svg').remove();
  16. }
  17. $(container_icon).prepend('<svg stroke="currentColor" fill="currentColor" stroke-width="0" viewBox="0 0 16 16" height="1em" width="1em" xmlns="http://www.w3.org/2000/svg"><path d="M16 8A8 8 0 1 1 0 8a8 8 0 0 1 16 0zm-3.97-3.03a.75.75 0 0 0-1.08.022L7.477 9.417 5.384 7.323a.75.75 0 0 0-1.06 1.06L6.97 11.03a.75.75 0 0 0 1.079-.02l3.992-4.99a.75.75 0 0 0-.01-1.05z"></path></svg>')
  18. $(toast).addClass('type-success');
  19. $(toast).removeClass('type-error');
  20. $(toast).removeClass('type-warning');
  21. break
  22. case 'error':
  23. var existMoreone = $(container_icon).find('svg').length;
  24. if (existMoreone > 0) {
  25. $(container_icon).find('svg').remove();
  26. }
  27. $(container_icon).prepend('<svg stroke="currentColor" fill="currentColor" stroke-width="0" viewBox="0 0 24 24" height="1em" width="1em" xmlns="http://www.w3.org/2000/svg"><path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm1 15h-2v-2h2v2zm0-4h-2V7h2v6z"></path></svg>')
  28. $(toast).addClass('type-error');
  29. $(toast).removeClass('type-success');
  30. $(toast).removeClass('type-warning');
  31. break
  32. case 'warning':
  33. var existMoreone = $(container_icon).find('svg').length;
  34. if (existMoreone > 0) {
  35. $(container_icon).find('svg').remove();
  36. }
  37. $(container_icon).prepend('<svg stroke="currentColor" fill="currentColor" stroke-width="0" viewBox="0 0 512 512" height="1em" width="1em" xmlns="http://www.w3.org/2000/svg"><path d="M228.9 79.9L51.8 403.1C40.6 423.3 55.5 448 78.9 448h354.3c23.3 0 38.2-24.7 27.1-44.9L283.1 79.9c-11.7-21.2-42.5-21.2-54.2 0zM273.6 214L270 336h-28l-3.6-122h35.2zM256 402.4c-10.7 0-19.1-8.1-19.1-18.4s8.4-18.4 19.1-18.4 19.1 8.1 19.1 18.4-8.4 18.4-19.1 18.4z"></path></svg>')
  38. $(toast).removeClass('type-error');
  39. $(toast).removeClass('type-success');
  40. $(toast).addClass('type-warning');
  41. break
  42. }
  43. root.style.setProperty('--time-animation', time+'s');
  44. var timeNormal = time * 1000;
  45. var timeWithdoutAnimation = time * 1000 + 300;
  46. setTimeout(function() {
  47. $(toast).removeClass('visible');
  48. }, timeNormal)
  49. setTimeout(function() {
  50. $(progress).removeClass('visible');
  51. },timeWithdoutAnimation)
  52. },
  53. // add toast message
  54. addMessage: function (titleP, messageP) {
  55. var title = $('#toast .menssagem .text-1');
  56. // console.log($(title).text())
  57. var message = $('#toast .menssagem .text-2');
  58. $(title).text(titleP);
  59. $(message).text(messageP);
  60. },
  61. // close toast message
  62. close_toast:function () {
  63. var progress = $('#toast .progress');
  64. $('.toast .icon-close').click(function() {
  65. var taost_atual = $(this).closest('.toast')
  66. $(taost_atual).removeClass('visible');
  67. setTimeout(function() {
  68. $(progress).removeClass('visible');
  69. },300)
  70. })
  71. }
  72. }

5.在页面上显示吐司通知。

  1. // Error Toast
  2. toast_notification.show_toast({
  3.  
  4. // title
  5. title: 'Error',
  6.  
  7. // toast message
  8. message: 'Error Message',
  9.  
  10. // auto dismiss after 10 seconds
  11. time: 10,
  12.  
  13. // notification type
  14. type: 'error',
  15.  
  16. });
  17.  
  18. // Success Toast
  19. toast_notification.show_toast({
  20. title: 'Success',
  21. message: 'Success Message',
  22. time: 3,
  23. type: 'success',
  24. })
  25.  
  26. // Warning Toast
  27. toast_notification.show_toast({
  28. title: 'Warning',
  29. message: 'Warning Message',
  30. time: 30,
  31. type: 'warning',
  32. })

预览截图