🏠 Home 

Toastnew

Toast1

Dette script bør ikke installeres direkte. Det er et bibliotek, som andre scripts kan inkludere med metadirektivet // @require https://update.greasyfork.org/scripts/498897/1404834/Toastnew.js

  1. function Toast(msg, duration = 3000, backgroundColor = 'rgba(0, 0, 0, 0.7)', textColor = 'rgb(255, 255, 255)', position = 'bottom-right') {
  2. const toast = document.createElement('div');
  3. toast.className = 'toast';
  4. toast.innerHTML = msg;
  5. toast.style.cssText = `
  6. max-width: 80%;
  7. min-width: 150px;
  8. padding: 0 14px;
  9. height: auto;
  10. color: ${textColor};
  11. line-height: 1.5;
  12. text-align: center;
  13. border-radius: 12px;
  14. position: fixed;
  15. z-index: 2147483647;
  16. background: ${backgroundColor};
  17. font-size: 16px;
  18. transition: opacity 0.5s ease-in, transform 0.5s ease-in;
  19. word-wrap: break-word;
  20. `;
  21. // 使用位置参数设置位置样式
  22. const positions = {
  23. 'top': 'top: 50%; left: 50%; transform: translate(-50%, 0);',
  24. 'bottom': 'bottom: 10%; left: 50%; transform: translate(-50%, 0);',
  25. 'left': 'top: 50%; left: 10%; transform: translate(0, -50%);',
  26. 'right': 'top: 50%; right: 10%; transform: translate(0, -50%);',
  27. 'top-left': 'top: 10%; left: 10%; transform: translate(0, 0);',
  28. 'top-right': 'top: 10%; right: 10%; transform: translate(0, 0);',
  29. 'bottom-left': 'bottom: 10%; left: 10%; transform: translate(0, 0);',
  30. 'bottom-right': 'bottom: 10%; right: 10%; transform: translate(0, 0);',
  31. 'center': 'top: 50%; left: 50%; transform: translate(-50%, -50%);',
  32. };
  33. toast.style.cssText += positions[position] || positions['bottom-right'];
  34. document.body.appendChild(toast);
  35. setTimeout(() => {
  36. toast.style.opacity = '0';
  37. setTimeout(() => document.body.removeChild(toast), 500);
  38. }, duration);
  39. // 增加媒体查询来优化移动设备上的样式
  40. const mediaQuery = `
  41. @media only screen and (max-width: 600px) {
  42. .toast {
  43. font-size: 14px;
  44. padding: 10px;
  45. max-width: 90%;
  46. min-width: 0;
  47. }
  48. }
  49. `;
  50. const style = document.createElement('style');
  51. style.textContent = mediaQuery;
  52. document.head.appendChild(style);
  53. }