亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

當#links(同頁鏈接)被點擊時,如何隱藏菜單?

當#links(同頁鏈接)被點擊時,如何隱藏菜單?

三國紛爭 2022-09-02 21:06:33
有人可以告訴我,如果任何(同一頁面鏈接)被點擊或有人點擊菜單區域之外,我如何使我的漢堡菜單再次消失?我認為我需要在我的JS腳本代碼中添加一些東西,但我似乎找不到正確的東西來改變或添加。在JavaScript方面,我完全是新手。#linksconst menuBtn = document.querySelector('.menu-btn');let menuOpen = false;menuBtn.addEventListener('click', () => {  if (!menuOpen) {    menuBtn.classList.add('open');    document.getElementById("mymobilemenu").style.display = "block";    menuOpen = true;  } else {    menuBtn.classList.remove('open');    document.getElementById("mymobilemenu").style.display = "none";    menuOpen = false;  }});
查看完整描述

2 回答

?
湖上湖

TA貢獻2003條經驗 獲得超2個贊

您好,您可以通過將 foreach 添加到 來執行此操作,因此每次有人單擊該內容時,腳本都會將打開的類刪除到菜單。<li>


const menuBtn = document.querySelector('.menu-btn');

let menuOpen = false;

menuBtn.addEventListener('click', () => {

  if (!menuOpen) {

    menuBtn.classList.add('open');

    document.getElementById("mymobilemenu").style.display = "block";

    menuOpen = true;

  } else {

    menuBtn.classList.remove('open');

    document.getElementById("mymobilemenu").style.display = "none";

    menuOpen = false;

  }

});

let  menuli = document.querySelectorAll('#mymobilemenu li');

menuli.forEach((item, index) => {

 item.addEventListener('click', () => {

 menuBtn.classList.remove('open');

    document.getElementById("mymobilemenu").style.display = "none";

    menuOpen = false;

 

});


});

.menu-btn {

  position: relative;

  float: right;

  display: flex;

  justify-content: center;

  align-items: center;

  width: 80px;

  height: 80px;

  cursor: pointer;

  transition: all .5s ease-in-out;

  border: 3px solid blueviolet;

  background: pink;

}


.menu-btn-burger {

  width: 50px;

  height: 6px;

  background: yellow;

  border-radius: 5px;

  transition: all .5s ease-in-out;

}


.menu-btn-burger::before,

.menu-btn-burger::after {

  content: '';

  position: absolute;

  width: 50px;

  height: 6px;

  background: grey;

  border-radius: 5px;

  transition: all .5s ease-in-out;

}


.menu-btn-burger::before {

  transform: translateY(-16px);

}


.menu-btn-burger::after {

  transform: translateY(16px);

}



/* animation of burger */


.menu-btn.open .menu-btn-burger {

  transform: translateX(-50px);

  background: transparent;

  box-shadow: none;

}


.menu-btn.open .menu-btn-burger::before {

  transform: rotate(45deg) translate(35px, -35px);

}


.menu-btn.open .menu-btn-burger::after {

  transform: rotate(-45deg) translate(35px, 35px);

}


#mymobilemenu {

  display: none;

}

<div class="menu-btn">

  <div class="menu-btn-burger">

  </div>

</div>


<div id="mymobilemenu">

  <li><a href="#section1">section1</a></li>

  <li><a href="#section2">section2</a></li>

  <li><a href="#section3">section3</a></li>

  <li><a href="#section4">section4</a></li>

</div>


<a id="section1">SECTION 1</a>

<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has

  survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing

  software like Aldus PageMaker including versions of Lorem Ipsum. Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a

  galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets

  containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's

  standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.

  It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>


<a id="section2">SECTION 2</a>

<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has

  survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing

  software like Aldus PageMaker including versions of Lorem Ipsum. Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a

  galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets

  containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's

  standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.

  It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>


<a id="section3">SECTION 3</a>

<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has

  survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing

  software like Aldus PageMaker including versions of Lorem Ipsum. Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a

  galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets

  containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's

  standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.

  It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>


<a id="section4">SECTION 4</a>

<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has

  survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing

  software like Aldus PageMaker including versions of Lorem Ipsum. Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a

  galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets

  containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's

  standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.

  It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>


查看完整回答
反對 回復 2022-09-02
?
明月笑刀無情

TA貢獻1828條經驗 獲得超4個贊

嘗試將其添加到您的js腳本中:


const a = document.querySelectorAll('.a');

a.forEach(element => {

  element.addEventListener('click', () => {

    menuBtn.classList.remove('open');

    document.getElementById("mymobilemenu").style.display = "none";

    console.log("removed");

  })


});


查看完整回答
反對 回復 2022-09-02
  • 2 回答
  • 0 關注
  • 95 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號