toast.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. 'use strict';
  2. (function($,window){
  3. //动态加载animate
  4. var loadStyles = function(url) {
  5. var hasSameStyle = false;
  6. var links = $('link');
  7. for(var i = 0;i<links.length;i++){
  8. if(links.eq(i).attr('href') == url){
  9. hasSameStyle = true;
  10. return
  11. }
  12. }
  13. if(!hasSameStyle){
  14. var link = document.createElement("link");
  15. link.type = "text/css";
  16. link.rel = "stylesheet";
  17. link.href = url;
  18. document.getElementsByTagName("head")[0].appendChild(link);
  19. }
  20. }
  21. loadStyles('css/animate.css');
  22. //显示提示信息 toast
  23. $.fn.toast = function(options){
  24. var $this = $(this);
  25. var _this = this;
  26. return this.each(function(){
  27. $(this).css({
  28. position:'relative'
  29. });
  30. var top = ''; //bottom的位置
  31. var translateInfo = ''; //居中和不居中时的tarnslate
  32. var box = ''; //消息元素
  33. var defaults = {
  34. position: "absolute", //不是body的话就absolute
  35. animateIn: "fadeIn", //进入的动画
  36. animateOut: "fadeOut", //结束的动画
  37. padding: "10px 20px", //padding
  38. background: "rgba(7,17,27,0.66)", //背景色
  39. borderRadius: "6px", //圆角
  40. duration: 3000, //定时器时间
  41. animateDuration: 500, //执行动画时间
  42. fontSize: 14, //字体大小
  43. content: "这是一个提示信息", //提示内容
  44. color: "#fff", //文字颜色
  45. top: "80%", //bottom底部的位置 具体的数值 或者center 垂直居中
  46. zIndex: 1000001, //层级
  47. isCenter: true, //是否垂直水平居中显示
  48. closePrev: true, //在打开下一个toast的时候立即关闭上一个toast
  49. }
  50. var opt = $.extend(defaults,options||{});
  51. var t = '';
  52. // setTimeout(function(){
  53. // box.addClass('show');
  54. // },10);
  55. top = opt.isCenter===true? '50%':opt.top;
  56. defaults.isLowerIe9 = function(){
  57. return (!window.FormData);
  58. }
  59. // translateY(-50%)
  60. // translateInfo = opt.isCenter===true? 'translate3d(-50%,0,0)':'translate3d(-50%,-50%,0)';
  61. defaults.createMessage = function(){
  62. if(opt.closePrev){
  63. $('.cpt-toast').remove();
  64. }
  65. box = $("<span class='animated "+opt.animateIn+" cpt-toast'></span>").css({
  66. "position":opt.position,
  67. "padding":opt.padding,
  68. "background":opt.background,
  69. "font-size":opt.fontSize,
  70. "-webkit-border-radius":opt.borderRadius,
  71. "-moz-border-radius":opt.borderRadius,
  72. "border-radius":opt.borderRadius,
  73. "color":opt.color,
  74. "top":top,
  75. "z-index":opt.zIndex,
  76. "-webkit-transform":'translate3d(-50%,-50%,0)',
  77. "-moz-transform":'translate3d(-50%,-50%,0)',
  78. "transform":'translate3d(-50%,-50%,0)',
  79. '-webkit-animation-duration':opt.animateDuration/1000+'s',
  80. '-moz-animation-duration':opt.animateDuration/1000+'s',
  81. 'animation-duration':opt.animateDuration/1000+'s',
  82. }).html(opt.content).appendTo($this);
  83. defaults.colseMessage();
  84. }
  85. defaults.colseMessage = function(){
  86. var isLowerIe9 = defaults.isLowerIe9();
  87. if(!isLowerIe9){
  88. t = setTimeout(function(){
  89. box.removeClass(opt.animateIn).addClass(opt.animateOut).on('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend',function(){
  90. box.remove();
  91. });
  92. box.remove();
  93. },opt.duration);
  94. }else{
  95. t = setTimeout(function(){
  96. box.remove();
  97. },opt.duration);
  98. }
  99. }
  100. defaults.createMessage();
  101. })
  102. };
  103. })(jQuery,window);
  104. var showMessage = function(content,duration,isCenter,animateIn,animateOut){
  105. var animateIn = animateIn || 'fadeIn';
  106. var animateOut = animateOut || 'fadeOut';
  107. var content = content || '这是一个提示信息';
  108. var duration = duration || '2000';
  109. var isCenter = isCenter || false;
  110. $('body').toast({
  111. position:'fixed',
  112. animateIn:animateIn,
  113. animateOut:animateOut,
  114. content:content,
  115. duration:duration,
  116. isCenter:isCenter,
  117. });
  118. }