toast.script

消息提示

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.gf.qytechs.cn/scripts/447214/1065649/toastscript.js

  1. (function(){
  2. "use strict";
  3. $.Toast = function(title, message, type, options){
  4. var defaultOptions = {
  5. appendTo: "body",
  6. stack: false,
  7. position_class: "toast-bottom-right",
  8. fullscreen:false,
  9. width: 250,
  10. spacing:20,
  11. timeout: 4000,
  12. has_close_btn:true,
  13. has_icon:true,
  14. sticky:false,
  15. border_radius:6,
  16. has_progress:false,
  17. rtl:false
  18. }
  19.  
  20. var $element = null;
  21.  
  22. var $options = $.extend(true, {}, defaultOptions, options);
  23.  
  24. var spacing = $options.spacing;
  25.  
  26. var css = {
  27. "position":($options.appendTo == "body") ? "fixed" : "absolute",
  28. "min-width":$options.width,
  29. "display":"none",
  30. "border-radius":$options.border_radius,
  31. "z-index":99999
  32. }
  33.  
  34. $element = $('<div class="toast-item-wrapper ' + type + ' ' + $options.position_class + '"></div>');
  35. $('<p class="toast-title">' + title + '</p>').appendTo($element);
  36. $('<p class="toast-message">' + message + '</p>').appendTo($element);
  37.  
  38. if($options.fullscreen){
  39. $element.addClass( "fullscreen" );
  40. }
  41.  
  42. if($options.rtl){
  43. $element.addClass( "rtl" );
  44. }
  45.  
  46. if($options.has_close_btn){
  47. $('<span class="toast-close">&times;</span>').appendTo($element);
  48. if( $options.rtl){
  49. css["padding-left"] = 20;
  50. } else {
  51. css["padding-right"] = 20;
  52. }
  53. }
  54.  
  55. if($options.has_icon){
  56. $('<i class="toast-icon toast-icon-' + type + '"></i>').appendTo($element);
  57. if( $options.rtl){
  58. css["padding-right"] = 50;
  59. } else {
  60. css["padding-left"] = 50;
  61. }
  62. }
  63.  
  64. if($options.has_progress && $options.timeout > 0){
  65. $('<div class="toast-progress"></div>').appendTo($element);
  66. }
  67.  
  68. if($options.sticky){
  69. $options.spacing = 0;
  70. spacing = 0;
  71.  
  72. switch($options.position_class){
  73. case "toast-top-left" : {
  74. css["top"] = 0;
  75. css["left"] = 0;
  76. break;
  77. }
  78. case "toast-top-right" : {
  79. css["top"] = 0;
  80. css["left"] = 0;
  81. break;
  82. }
  83. case "toast-top-center" : {
  84. css["top"] = 0;
  85. css["left"] = css["right"] = 0;
  86. css["width"] = "100%";
  87. break;
  88. }
  89. case "toast-bottom-left" : {
  90. css["bottom"] = 0;
  91. css["left"] = 0;
  92. break;
  93. }
  94. case "toast-bottom-right" : {
  95. css["bottom"] = 0;
  96. css["right"] = 0;
  97. break;
  98. }
  99. case "toast-bottom-center" : {
  100. css["bottom"] = 0;
  101. css["left"] = css["right"] = 0;
  102. css["width"] = "100%";
  103. break;
  104. }
  105. default : {
  106. break;
  107. }
  108. }
  109. }
  110.  
  111. if($options.stack){
  112. if($options.position_class.indexOf("toast-top") !== -1 ){
  113. $($options.appendTo).find('.toast-item-wrapper').each(function(){
  114. css["top"] = parseInt($(this).css("top")) + this.offsetHeight + spacing;
  115. });
  116. } else if($options.position_class.indexOf("toast-bottom") !== -1 ){
  117. $($options.appendTo).find('.toast-item-wrapper').each(function(){
  118. css["bottom"] = parseInt($(this).css("bottom")) + this.offsetHeight + spacing;
  119. });
  120. }
  121. }
  122.  
  123. $element.css(css);
  124.  
  125. $element.appendTo($options.appendTo);
  126.  
  127. if($element.fadeIn) {
  128. $element.fadeIn();
  129. }else {
  130. $alert.css({display: 'block', opacity: 1});
  131. }
  132.  
  133. function removeToast(){
  134. $.Toast.remove( $element );
  135. }
  136.  
  137. if($options.timeout > 0){
  138. setTimeout(removeToast, $options.timeout);
  139. if($options.has_progress){
  140. $(".toast-progress", $element).animate({"width":"100%"}, $options.timeout);
  141. }
  142. }
  143.  
  144. $(".toast-close", $element).click(removeToast)
  145.  
  146. return $element;
  147. }
  148.  
  149. $.Toast.remove = function( $element ){
  150. "use strict";
  151. if($element.fadeOut)
  152. {
  153. $element.fadeOut(function(){
  154. return $element.remove();
  155. });
  156. }
  157. else{
  158. $element.remove();
  159. }
  160. }
  161. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址