如何在移动视图中切换我的导航栏以便滑动显示?

huangapple go评论70阅读模式
英文:

how to toggle my navbar to slide over in mobile view?

问题

I want my navbar to slide over by clicking on its icon in mobile view, but I'm having some problems with that. It's not sliding over by clicking. I'm sure I have followed the right js syntax for it.

let navbar = document.querySelector(".navbar");
document.querySelector("#menu-btn").onclick = () => {
  navbar.classList.toggle("active");
}
.header .navbar {
  position: absolute;
  top: 100%;
  left: -100%;
  background: #13131a;
  width: 30rem;
  height: calc(100vh - 9.5rem);
  align-items: flex-start;
  flex-direction: column;
  justify-content: flex-start;
  overflow: hidden;
}

.header .navbar.active {
  left: 0;
}
<div class="navbar mx-auto ">
  <a asp-action="Index">صفحه اصلی</a>
  <a href="#products">محصولات</a>
  <a href="#about">درباره ما</a>
  <a href="#contact">ارتباط با ما</a>
</div>
<div class="icons">
  <i class="fa-solid fa-cart-shopping "></i>
  <i class="fa-solid fa-magnifying-glass "></i>
  <i class="fa-solid fa-right-to-bracket "></i>
  <i class="fa-solid fa-bars" id="menu-btn"></i>
</div>

I just don't know where I'm going wrong.
the CSS class is changing when I click on the bar icon but not sliding over the navbar.

英文:

I want my navbar to slide over by clicking on its icon in mobile view, but I'm having some problems with that. It's not sliding over by clicking. I'm sure I have followed the right js syntax for it.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

let navbar = document.querySelector(&quot;.navbar&quot;);
document.querySelector(&quot;#menu-btn&quot;).onclick = () =&gt; {
  navbar.classList.toggle(&quot;active&quot;);
}

<!-- language: lang-css -->

.header .navbar {
  position: absolute;
  top: 100%;
  left: -100%;
  background: #13131a;
  width: 30rem;
  height: calc(100vh - 9.5rem);
  align-items: flex-start;
  flex-direction: column;
  justify-content: flex-start;
  overflow: hidden;
}

.header .navbar .active {
  left: 0;
}

<!-- language: lang-html -->

&lt;div class=&quot;navbar mx-auto &quot;&gt;
  &lt;a asp-action=&quot;Index&quot;&gt;صفحه اصلی&lt;/a&gt;
  &lt;a href=&quot;#products&quot;&gt;محصولات&lt;/a&gt;
  &lt;a href=&quot;#about&quot;&gt;درباره ما&lt;/a&gt;
  &lt;a href=&quot;#contact&quot;&gt;ارتباط با ما&lt;/a&gt;
&lt;/div&gt;
&lt;div class=&quot;icons&quot;&gt;
  &lt;i class=&quot;fa-solid fa-cart-shopping &quot;&gt;&lt;/i&gt;
  &lt;i class=&quot;fa-solid fa-magnifying-glass &quot;&gt;&lt;/i&gt;
  &lt;i class=&quot;fa-solid fa-right-to-bracket &quot;&gt;&lt;/i&gt;
  &lt;i class=&quot;fa-solid fa-bars&quot; `id=&quot;menu-btn&quot;`&gt;&lt;/i&gt;
&lt;/div&gt;

<!-- end snippet -->

I just don't know where I'm going wrong.
the CSS class is changing when I click on the bar icon but not sliding over the navbar.

答案1

得分: 0

I think the main problem with your css is:

.header .navbar .active {
  left: 0;
}

Since there is a space between .active it will look for an element inside the navbar div with the class active, but there isn't one.

So instead this should be

.header .navbar.active {
  left: 0;
}

Other minor things:
id=&quot;menu-btn&quot; had some backticks around it in your example which should be removed.

You haven't included the header class in your example, but I think that should be ok as long as it's in an element wrapping everything.

top:100% looks a bit odd, but maybe it's ok.

英文:

I think the main problem with your css is:

.header .navbar .active {
  left: 0;
}

Since there is a space between .active it will look for an element inside the navbar div with the class active, but there isn't one.

So instead this should be

.header .navbar.active {
  left: 0;
}

Other minor things:
id=&quot;menu-btn&quot; had some backticks around it in your example which should be removed.

You haven't included the header class in your example, but I think that should be ok as long as it's in an element wrapping everything.

top:100% looks a bit odd, but maybe it's ok.

huangapple
  • 本文由 发表于 2023年4月6日 19:57:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75949261.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定