1 回答

TA貢獻1848條經(jīng)驗 獲得超6個贊
在head標簽中插入此 jquery 參考腳本
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
.
.
.
</head>
需要做的是添加鼠標單擊event,單擊時代碼應檢查單擊是否在容器外部nav,如果為真,active將從容器中刪除:
在您的js腳本文件中,將js您發(fā)布的代碼替換為:
function toggle() {
var navButton = document.querySelector('.nav-btn-container');
navButton.classList.toggle('active')
}
$(document).mouseup(function (e) {
var container = $("header");
// if the target of the click isn't the container nor a descendant of the container
if (!container.is(e.target) && container.has(e.target).length === 0) {
document.querySelector('.nav-btn-container').classList.remove('active');
}
});
此外,不應將您的放在<script src="script.js"></script>中head,而應該放在 的底部body
- 1 回答
- 0 關注
- 177 瀏覽
添加回答
舉報