toast.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // toast
  2. var toastTimeout;
  3. $('[data-toggle="toast"]').tooltip({
  4. animation: false,
  5. container: '.toast',
  6. html: true,
  7. placement: 'bottom',
  8. template: '<div class="tooltip"><div class="toast-inner tooltip-inner"></div></div>',
  9. trigger: 'manual'
  10. });
  11. // toast dismiss
  12. $(document).on('click', '[data-dismiss="toast"]', function(e) {
  13. e.preventDefault();
  14. toastHide(0);
  15. });
  16. function toastHide(timer, toast) {
  17. clearTimeout(toastTimeout);
  18. toastTimeout = setTimeout(function() {
  19. $('.toast').removeClass('toast-show');
  20. if ($('.fbtn-container').length) {
  21. $('.fbtn-container').css('margin-bottom', '');
  22. };
  23. $('.toast-inner').one('webkitTransitionEnd otransitionend oTransitionEnd msTransitionEnd transitionend', function(e) {
  24. $('.toast-toggled').tooltip('hide').removeClass('toast-toggled');
  25. if (toast !== null && toast !== undefined) {
  26. toast.tooltip('show').addClass('toast-toggled');
  27. } else {
  28. $('.toast').remove();
  29. }
  30. });
  31. }, timer);
  32. }
  33. // toast hover
  34. $(document).on('mouseenter', '.toast', function() {
  35. clearTimeout(toastTimeout);
  36. });
  37. $(document).on('mouseleave', '.toast', function() {
  38. toastHide(6000);
  39. });
  40. // toast show
  41. $(document).on('click', '[data-toggle="toast"]', function() {
  42. var $this = $(this);
  43. if (!$('.toast').length) {
  44. $('body').append('<div class="toast"></div>');
  45. };
  46. if (!$this.hasClass('toast-toggled')) {
  47. if ($('.toast').hasClass('toast-show')) {
  48. toastHide(0, $this);
  49. } else {
  50. $this.tooltip('show').addClass('toast-toggled');
  51. }
  52. };
  53. });
  54. $(document).on('shown.bs.tooltip', '[data-toggle="toast"]', function() {
  55. var $this = $(this);
  56. $('.toast').addClass('toast-show');
  57. if ($(window).width() < 768 && $('.fbtn-container').length) {
  58. $('.fbtn-container').css('margin-bottom', $('.toast').outerHeight());
  59. };
  60. $('.toast-inner').one('webkitTransitionEnd otransitionend oTransitionEnd msTransitionEnd transitionend', function(e) {
  61. toastHide(6000);
  62. });
  63. });