bootstrap.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. //判断系统深色模式变化,修改切换按钮
  2. var matchMedia = window.matchMedia(('(prefers-color-scheme: dark)'));
  3. matchMedia.addEventListener('change', function () {
  4. var mode = this.matches ? 'dark' : 'light';
  5. //只有当cookie中无手动定义值时才进行操作
  6. if (document.cookie.indexOf("thememode=") === -1 && Config.darktheme.mode === 'auto') {
  7. $("body").toggleClass("darktheme", mode === "dark");
  8. }
  9. });
  10. if (typeof Config.darktheme !== 'undefined' && Config.darktheme.switchbtn) {
  11. // 切换模式
  12. var switchMode = function (mode) {
  13. // 获取当前深色模式
  14. if (mode === 'auto') {
  15. var isDarkMode = window.matchMedia('(prefers-color-scheme: dark)').matches;
  16. mode = isDarkMode ? 'dark' : 'light';
  17. }
  18. if (mode === 'auto') {
  19. } else if (mode === 'dark') {
  20. $("body").addClass("darktheme");
  21. $(".darktheme-link").removeAttr("media");
  22. } else {
  23. $("body").removeClass("darktheme");
  24. $(".darktheme-link").attr("media", "(prefers-color-scheme: dark)");
  25. }
  26. };
  27. // 创建Cookie
  28. var createCookie = function (name, value) {
  29. var date = new Date();
  30. date.setTime(date.getTime() + (365 * 24 * 60 * 60 * 1000));
  31. var url = Config.moduleurl.replace(location.origin, "");
  32. var path = url ? url.substring(url.lastIndexOf("/")) : "/";
  33. document.cookie = encodeURIComponent(Config.cookie.prefix + name) + "=" + encodeURIComponent(value) + "; path=" + path + "; expires=" + date.toGMTString();
  34. };
  35. if (Config.controllername === 'index' && Config.actionname === 'index') {
  36. var mode = Config.darktheme.mode;
  37. if (mode === 'auto') {
  38. var isDarkMode = window.matchMedia('(prefers-color-scheme: dark)').matches;
  39. mode = isDarkMode ? 'dark' : 'light';
  40. }
  41. var html = '<li class="theme-li">' +
  42. '<button type="button" title="切换' + (mode === 'dark' ? '浅色' : '深色') + '模式" data-mode="' + (mode === 'dark' ? 'light' : 'dark') + '" class="theme-toggle">' +
  43. '<svg class="sun-and-moon" aria-hidden="true" width="24" height="24" viewBox="0 0 24 24">\n' +
  44. ' <circle class="sun" cx="12" cy="12" r="6" mask="url(#moon-mask)" fill="currentColor" />\n' +
  45. ' <g class="sun-beams" stroke="currentColor">\n' +
  46. ' <line x1="12" y1="1" x2="12" y2="3" />\n' +
  47. ' <line x1="12" y1="21" x2="12" y2="23" />\n' +
  48. ' <line x1="4.22" y1="4.22" x2="5.64" y2="5.64" />\n' +
  49. ' <line x1="18.36" y1="18.36" x2="19.78" y2="19.78" />\n' +
  50. ' <line x1="1" y1="12" x2="3" y2="12" />\n' +
  51. ' <line x1="21" y1="12" x2="23" y2="12" />\n' +
  52. ' <line x1="4.22" y1="19.78" x2="5.64" y2="18.36" />\n' +
  53. ' <line x1="18.36" y1="5.64" x2="19.78" y2="4.22" />\n' +
  54. ' </g>\n' +
  55. ' <mask class="moon" id="moon-mask">\n' +
  56. ' <rect x="0" y="0" width="100%" height="100%" fill="white" />\n' +
  57. ' <circle cx="24" cy="10" r="6" fill="black" />\n' +
  58. ' </mask>\n' +
  59. ' </svg>' +
  60. '</button>' +
  61. '</li>';
  62. $(html).prependTo("#firstnav > div > ul");
  63. //点击切换按钮
  64. $(document).on("click", ".theme-toggle", function () {
  65. var mode = $(this).attr("data-mode");
  66. switchMode(mode);
  67. createCookie("thememode", mode);
  68. $("iframe").each(function () {
  69. try {
  70. $(this)[0].contentWindow.$("body").trigger("swithmode", [mode]);
  71. } catch (e) {
  72. }
  73. });
  74. $(this).attr("data-mode", mode === 'dark' ? 'light' : 'dark').attr("title", '切换' + (mode === 'dark' ? '浅色' : '深色') + '模式');
  75. });
  76. //判断系统深色模式变化,修改切换按钮
  77. var matchMedia = window.matchMedia(('(prefers-color-scheme: dark)'));
  78. matchMedia.addEventListener('change', function () {
  79. var mode = this.matches ? 'dark' : 'light';
  80. //只有当cookie中无手动定义值时才切换
  81. if (document.cookie.indexOf("thememode=") === -1 && Config.darktheme.mode === 'auto') {
  82. $(".theme-toggle").attr("data-mode", mode === 'dark' ? 'light' : 'dark').attr("title", '切换' + (mode === 'dark' ? '浅色' : '深色') + '模式');
  83. }
  84. });
  85. } else {
  86. //添加事件
  87. $("body").on("swithmode", function (e, mode) {
  88. switchMode(mode);
  89. $("iframe").each(function () {
  90. try {
  91. $(this)[0].contentWindow.$("body").trigger("swithmode", [mode]);
  92. } catch (e) {
  93. }
  94. });
  95. });
  96. }
  97. }