我有一个侧边栏,可在桌面视图上使用javscript滑出250像素。对于移动视图,我希望边栏占据100%的宽度。我正在尝试在Javascript中使用媒体查询,但是无论我做了什么更改,似乎都覆盖了桌面视图上侧边栏的样式。
HTML
<nav class="navbar">
<div id="toggle-btn" class="sidemenu-btn">
<i class="fas fa-bars"></i>
</div>
<div id="toggle-nav" class="navbar-items animated fadeInLeft delay-1s">
<a href="#home">Home</a>
<a href="#about-me">About</a>
<a href="#my-skills">Skills</a>
<a href="#my-portfolio">Portfolio</a>
<a href="#contact-me">Contact</a>
</div>
</nav>
CSS
.navbar {
height: 100%;
width: 0;
position: fixed;
background: #141313;
}
.navbar .sidemenu-btn {
font-size: 2.5rem;
padding: 3rem 0;
text-align: center;
margin-left: 1rem;
cursor: pointer;
color: #141313;
}
.navbar .navbar-items {
display: flex;
flex-direction: column;
text-align: center;
display: none;
margin-left: 1rem;
}
.navbar .navbar-items a {
text-decoration: none;
color: white;
padding: 1.2rem 0;
font-weight: 400;
font-size: 1.2rem;
text-transform: uppercase;
}
.navbar .navbar-items a:hover {
text-decoration: line-through;
}
@media screen and (max-width: 768px) {
.navbar {
position: relative;
}
}
JS
const toggleBtn = document.querySelector("#toggle-btn");
const toggleNav = document.querySelector(".navbar");
const togglenavItems = document.querySelector('.navbar-items');
toggleBtn.addEventListener("click", sideMenu);
toggleBtn.addEventListener("click", mediaQuery);
function sideMenu() {
if (toggleNav.style.width === "250px") {
toggleNav.style.width = "0px";
} else {
toggleNav.style.width = "250px";
}
}
function mediaQuery() {
const x = window.matchMedia('(max-width: 768px)');
const y = document.querySelector('.navbar');
if (x.matches && y.style.width === "100%") {
y.style.width = "0px";
} else {
y.style.width = "100%";
}
}
答案 0 :(得分:0)
媒体查询可以完成这项工作。您不需要js来更改navbar-items
的宽度。看到这个
const toggleBtn = document.querySelector("#toggle-btn");
const toggleNav = document.querySelector(".navbar");
const togglenavItems = document.querySelector('.navbar-items');
toggleBtn.addEventListener("click", sideMenu);
function sideMenu() {
toggleNav.classList.toggle('open');
}
* {
box-sizing: border-box;
}
body {
margin: 0;
}
.navbar {
height: 100%;
width: 100%;
position: fixed;
}
.sidemenu-btn {
font-size: 2.5rem;
padding: 0;
text-align: center;
cursor: pointer;
color: green;
position: absolute;
right: 20px;
top: 20px;
}
.navbar-items {
display: flex;
flex-direction: column;
text-align: center;
width: 250px;
height: 100%;
padding-left: 1rem;
background: #141313;
transition: all .5s ease;
}
.navbar .navbar-items a {
text-decoration: none;
color: white;
padding: 1.2rem 0;
font-weight: 400;
font-size: 1.2rem;
text-transform: uppercase;
}
.navbar .navbar-items a:hover {
text-decoration: line-through;
}
@media (max-width: 768px) {
.navbar-items {
width: 100%;
}
}
.navbar.open .navbar-items {
width: 0;
}
<nav class="navbar">
<div id="toggle-btn" class="sidemenu-btn">
<i class="fas fa-bars"></i>
Toggle
</div>
<div id="toggle-nav" class="navbar-items animated fadeInLeft delay-1s">
<a href="#home">Home</a>
<a href="#about-me">About</a>
<a href="#my-skills">Skills</a>
<a href="#my-portfolio">Portfolio</a>
<a href="#contact-me">Contact</a>
</div>
</nav>
只需添加媒体查询,您就可以更改元素的宽度,而无需签入js
@media (max-width: 768px) {
.navbar-items {
width: 100%;
}
}
添加一个.open
类,该类将添加到.navbar
上并触发转换
.navbar.open .navbar-items {
width: 0;
}
您的js现在已简化
const toggleBtn = document.querySelector("#toggle-btn");
const toggleNav = document.querySelector(".navbar");
toggleBtn.addEventListener("click", sideMenu);
function sideMenu() {
toggleNav.classList.toggle('open');
}
答案 1 :(得分:0)
使用CSS可以更轻松地控制CSS在移动设备/桌面上的外观,而JS只需处理逻辑即可。以下内容将解决样式差异,并使您的JS易于浏览。
CSS
.navbar{
width: 250px;
height: 100%;
position: fixed;
background: #141313;
}
.navHidden{
width: 0px !important;
}
/* smaller screen size */
@media screen and (max-width:768px) {
.navbar{
width: 100%;
}
}
HTML
<nav class="navbar navHidden">
JS
function sideMenu() {
toggleNav.classList.toggle("navHidden")
}
侧注
在您包含的代码中,单击切换按钮,将导航栏的宽度设置为 250px ,但随后会将其传递给您的下一个函数,然后将其交换不论屏幕大小如何,都可以在每种情况下中 100%,因此您编写的切换功能将永远无法使用。