Skip to content

Commit

Permalink
[fix] DOM Ready bug based on WebCell utility
Browse files Browse the repository at this point in the history
  • Loading branch information
TechQuery committed Jul 22, 2020
1 parent e0fd8a2 commit 4f0d6cb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
"[email protected]"
],
"main": "source/index.html",
"dependencies": {
"web-cell": "^2.1.2"
},
"devDependencies": {
"husky": "^4.2.5",
"lint-staged": "^10.2.11",
Expand Down
21 changes: 13 additions & 8 deletions source/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { documentReady, delegate } from "web-cell/source/utility/event";

import { ping } from "./data/config";

document.addEventListener("DOMContentLoaded", () => {
documentReady.then(() => {
const navbarCollapse = document.querySelector<HTMLDivElement>(
".navbar-collapse"
),
Expand All @@ -26,14 +28,17 @@ document.addEventListener("DOMContentLoaded", () => {
/**
* 导航项目点击切换
*/
navbarCollapse.onclick = (event) => {
const item = (event.target as HTMLElement).closest(".nav-item");

if (!item || !item.querySelector("a").href.startsWith("http")) return;
navbarCollapse.addEventListener(
"click",
delegate(".nav-item", (_, item) => {
if (!item.querySelector("a").href.startsWith("http")) return;

navbarCollapse.querySelector(".nav-item.active").classList.remove("active");
item.classList.add("active");
};
navbarCollapse
.querySelector(".nav-item.active")
.classList.remove("active");
item.classList.add("active");
})
);
/**
* 导航项目加载切换
*/
Expand Down

0 comments on commit 4f0d6cb

Please sign in to comment.