javascript 如何使用Django制作黑暗与光明主题?

cbjzeqam  于 2023-11-15  发布在  Java
关注(0)|答案(1)|浏览(101)

每个人我都有一个冲突与Django静态文件与JavaScript和jQuery.我做了一个网站与两个主题黑暗和光明与我的基本HTML,JS,和CSS它的工作非常好,但当我把它移动到Django模板与静态文件我得到了一个错误,虽然完整的风格和JS功能工作得很好.
无法读取null的属性(阅读“querySelector”)
这是我的HTML

<div class="color-modes position-relative ps-5">
                    <a class="bd-theme btn btn-link nav-link dropdown-toggle d-inline-flex align-items-center justify-content-center text-primary p-0 position-relative rounded-circle"
                        href="#" aria-expanded="true" data-bs-toggle="dropdown" data-bs-display="static"
                        aria-label="Toggle theme (light)">
                        <svg class="bi my-1 theme-icon-active">
                            <use href="#sun-fill"></use>
                        </svg>
                    </a>
                    <ul class="dropdown-menu dropdown-menu-end fs-14px" data-bs-popper="static">
                        <li>
                            <button type="button" class="dropdown-item d-flex align-items-center active"
                                data-bs-theme-value="light" aria-pressed="true">
                                <svg class="bi me-4 opacity-50 theme-icon">
                                    <use href="#sun-fill"></use>
                                </svg>
                                Light
                                <svg class="bi ms-auto d-none">
                                    <use href="#check2"></use>
                                </svg>
                            </button>
                        </li>
                        <li>
                            <button type="button" class="dropdown-item d-flex align-items-center"
                                data-bs-theme-value="dark" aria-pressed="false">
                                <svg class="bi me-4 opacity-50 theme-icon">
                                    <use href="#moon-stars-fill"></use>
                                </svg>
                                Dark
                                <svg class="bi ms-auto d-none">
                                    <use href="#check2"></use>
                                </svg>
                            </button>
                        </li>
                    </ul>
                </div>

字符串
这是我的jQuery和JS代码:

}(jQuery), APP = APP || {}, function (e) {
  'use strict';

  APP.colorModes = {
    init: function () {
      this.colorModes();
    },
    colorModes: function () {
      if (!document.documentElement.hasAttribute("data-bs-theme")) return;

      const e = localStorage.getItem("theme"),
            t = () => e ? e : window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light",
            n = function (e) {
        e === "auto" && window.matchMedia("(prefers-color-scheme: dark)").matches ? document.documentElement.setAttribute("data-bs-theme", "dark") : (e === "auto" && (e = "light"), document.documentElement.setAttribute("data-bs-theme", e));
      };

      n(t());

      const s = (t, n = !1) => {
        const e = document.querySelector(".color-modes");
        if (!e) return;
        document.querySelectorAll("[data-bs-theme-value]").forEach(e => {
          e.classList.remove("active"), e.setAttribute("aria-pressed", "false");
        }), document.querySelectorAll(".color-modes").forEach(n => {
          const s = n.querySelector(".theme-icon-active use"),
                e = n.querySelector('[data-bs-theme-value="' + t + '"]'),
                o = e.querySelector("svg use").getAttribute("href");
          e.classList.add("active"), e.setAttribute("aria-pressed", "true"), s.setAttribute("href", o);
        }), n && e.focus();
      };

      window.matchMedia("(prefers-color-scheme: dark)").addEventListener("change", () => {
        (e !== "light" || e !== "dark") && n(t());
      });

      const o = function () {
        s(t()), document.querySelectorAll("[data-bs-theme-value]").forEach(e => {
          e.addEventListener("click", () => {
            const t = e.getAttribute("data-bs-theme-value");
            localStorage.setItem("theme", t), n(t), s(t, !0);
          });
        });
      };

      o();
    }
  }, e(document).ready(function () {
    APP.colorModes.init();
  });


这是我的CSS示例:

[data-bs-theme=light] {
    --bs-blue: #0d6efd;
    --bs-indigo: #6610f2;
    --bs-purple: #6f42c1;
    --bs-pink: #d63384;
}

[data-bs-theme=dark] {
    --bs-body-color: #ababab;
    --bs-body-color-rgb: 171, 171, 171;
    --bs-body-bg: #1F261D;
}

[data-bs-theme=dark] .navbar {
    --bs-navbar-toggler-icon-bg: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 30 30'%3e%3cpath stroke='%23fff' stroke-linecap='round' stroke-miterlimit='10' stroke-width='2' d='M4 7h22M4 15h22M4 23h22'/%3e%3c/svg%3e")
}

wvyml7n5

wvyml7n51#

我用JavaScript做了另一个脚本函数,删除了旧的

<script>
    
    function myFunction() {
      var element = document.body;
      var themeValue = event.target.getAttribute('data-bs-theme-value');
      
          // Remove the "active" class from all buttons
            document.querySelectorAll('.dropdown-item').forEach(function (btn) {
            btn.classList.remove('active');
            btn.setAttribute('aria-pressed', 'false');
        });

        // Add the "active" class to the clicked button
        event.target.classList.add('active');
        event.target.setAttribute('aria-pressed', 'true');

        // Conditionally toggle the value of the data-bs-theme attribute
        element.dataset.bsTheme = (themeValue === 'dark') ? 'dark' : 'light';

    }

  </script>

字符串

相关问题