我正在開發我的
頂部顏色應該與導航欄其余部分的顏色相同。
// ham.js
// For mobile hamburger
const mobNav = document.querySelector("nav#mobile");
const deskNav = document.querySelector("nav.desktop");
const navColor = window.getComputedStyle(mobNav).getPropertyValue("background-color");
document.getElementsByClassName("ham")[0].addEventListener("click", (event) => {
document.getElementsByClassName("ham")[0].classList.toggle("cross");
document.getElementById("line2").classList.toggle("none");
mobNav.classList.toggle("on");
mobNav.classList.toggle("off");
if(mobNav.className === "on"){
deskNav.style.backgroundColor = navColor;
} else {
deskNav.style.backgroundColor = "";
}
event.stopPropagation();
});
// To close the navbar on clicking a link.
const mobileLis = document.querySelectorAll("nav#mobile a");
Array.from(mobileLis).forEach(link => {
link.addEventListener("click", event => {
document.getElementsByClassName("ham")[0].classList.toggle("cross");
document.getElementById("line2").classList.toggle("none");
mobNav.classList.toggle("on");
mobNav.classList.toggle("off");
if(mobNav.className === "on"){
deskNav.style.backgroundColor = navColor;
} else {
deskNav.style.backgroundColor = "";
}
event.stopPropagation();
})
})
提前致謝!
uj5u.com熱心網友回復:
而不是這個代碼
const navColor = window.getComputedStyle(mobNav).getPropertyValue("background-color");
試試這個
const navColor = () => window.getComputedStyle(mobNav).getPropertyValue("background-color");
并navColor作為函式呼叫navColor()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/328974.html
標籤:javascript css 谷歌浏览器 导航栏
