toast.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. window.toast={
  2. container : function (){
  3. let div = $("<div style='position: fixed;top:0;width: 100%;z-index: 9999'></div>");
  4. $("body").append(div);
  5. return div;
  6. }(),
  7. setIndex : function (num){
  8. this.container.css("z-index",num);
  9. },
  10. build:function (model,msg,duration){
  11. let div = $("<div class='d-flex justify-content-center align-items-center p-2'></div>");
  12. let toast = $("<div class='toast hide'></div>");
  13. toast.attr("role","alert");
  14. toast.attr("aria-live","assertive");
  15. toast.attr("aria-atomic","true");
  16. toast.attr("data-delay",duration);
  17. let info = $("<div style='min-width: 400px'></div>")
  18. let txt = $("<div class='ml-3'></div>");
  19. let i = $("<i></i>");
  20. switch (model){
  21. case "success":
  22. info.css("background","RGB(240,249,235)");
  23. info.addClass("toast-body text-success");
  24. i.addClass("fa fa-chevron-circle-down text-success");
  25. break;
  26. case "fail":
  27. info.css("background","RGB(255,240,240)");
  28. info.addClass("toast-body text-danger");
  29. i.addClass("fa fa-times-circle text-danger");
  30. }
  31. txt.append(i);
  32. txt.append(msg);
  33. info.append(txt);
  34. toast.append(info);
  35. div.append(toast);
  36. this.container.append(div);
  37. toast.toast("show");
  38. toast.on("hidden.bs.toast",function (){
  39. div.remove();
  40. });
  41. },
  42. success : function (msg = "SUCCESS!",duration = 2000){
  43. this.build("success",msg,duration)
  44. },
  45. error : function (msg = "ERROR!",duration = 3000){
  46. this.build("fail",msg,duration)
  47. },
  48. };