Задать вопрос
@Storos
Начинающий web разработчик

Как скрывать бургер меню при клике вне блока и по его пунктам?

Всем доброго времени суток! Я изучаю веб разработку, до JavaScript добрался лишь недавно. Поэтому прошу помощи у опытных людей в моём вопросе.
На созданном мною сайте есть меню, при ширине менее 768 оно становится бургер меню, я смог реализовать его открытие/закрытие по кнопке, но по пустой области и по клику по его пунктам не могу никак. То, что получилось, криво работает, так как при 768 всё работает, но при переходе на бОльшую ширину, начинает происходить какая-то дичь(
Помогите, пожалуйста, пятый день мучаюсь, бросить не могу!
Для наглядности Ссылка

<!DOCTYPE html>
<html lang="en">

<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<link rel="stylesheet" href="css/style.css">
	<title>Agency</title>
</head>

<body>

	<header class="header" id="header">
		<div class="header__container container">

			<div class="header__inner">
				<nav class="menu" id="menu">
					<ul class="menu__list">
						<li class="menu__item"><a href="#intro">home</a></li>
						<li class="menu__item"><a href="#portfolio">portfolio</a></li>
						<li class="menu__item"><a href="#team">about us</a></li>
						<li class="menu__item"><a href="#contact">contact</a></li>
					</ul>
				</nav>

				<button class="burger" id="navToggle">
					<span class="burger__item"></span>
				</button>

			</div>

		</div>
	</header>

	<div class="wrapper">

		<section class="intro" id="intro">
			<div class="container">

				<div class="intro__inner">
					<div class="intro__read">
						<h1 class="intro__title">
							our strong
							organaisation
						</h1>
						<div class="intro__text text">
							Lorem ipsum dolor sit amet, consectetur adipis cing elit, sed do eiusmod tempor incididunt
							ut labore et. ectetur adig
							ipis cing elit, sed do eiusmod tempor incididunt.
						</div>
						<button class="intro__button button">Contact Us</button>
					</div>

					<div class="intro__video video">
						<video loop muted autoplay controls class="video__item" preload="metadata">
							<source src="video/Tunnel.webm" type="video/webm">
							<source src="video/Tunnel.mp4" type="video/mp4">
						</video>
					</div>
				</div>

			</div>
		</section>

	</div>


	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
	<script src="js/index.js"></script>

</body>

</html>


@import url("https://fonts.googleapis.com/css2?family=Open+Sans:ital,wght@0,400;0,600;0,700;0,800;1,400&display=swap");
* {
  margin: 0;
  padding: 0;
  border: 0;
}

*,
*::before,
*::after {
  -webkit-box-sizing: border-box;
          box-sizing: border-box;
}

:focus,
:active {
  outline: none;
}

a:focus,
a:active {
  outline: none;
}

nav,
footer,
header,
aside {
  display: block;
}

html,
body {
  width: 100%;
  height: 100%;
  font-size: 100%;
  line-height: 1;
  font-size: 16px;
  -ms-text-size-adjust: 100%;
  -moz-text-size-adjust: 100%;
  -webkit-text-size-adjust: 100%;
}

input,
button,
textarea {
  font-family: inherit;
}

input::-ms-clear {
  display: none;
}

button {
  cursor: pointer;
}

button::-moz-focus-inner {
  padding: 0;
  border: 0;
}

a,
a:visited {
  text-decoration: none;
}

a:hover {
  text-decoration: none;
}

ul li {
  list-style: none;
}

img {
  vertical-align: top;
}

h1, h2, h3, h4, h5, h6 {
  font-weight: 400;
  font-size: inherit;
}

body {
  font-family: "Open Sans", sans-serif;
  font-weight: 400;
}

.wrapper {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-orient: vertical;
  -webkit-box-direction: normal;
      -ms-flex-direction: column;
          flex-direction: column;
  overflow: hidden;
  min-height: 100%;
}

.container {
  margin: 0 auto;
  max-width: 1279px;
  padding: 0 15px;
  width: 100%;
}

.text {
  opacity: 0.7;
  line-height: 1.75;
  color: #000000;
}

.button {
  display: inline-block;
  background-color: #ff3f40;
  border-radius: 5px;
  font-weight: 700;
  letter-spacing: 0.025em;
  color: #ffffff;
  -webkit-transition: all .2s ease;
  -o-transition: all .2s ease;
  transition: all .2s ease;
}

.button:hover {
  background-color: #fff;
  outline: 2px solid #ff3f40;
  color: #ff3f40;
}

.title {
  opacity: 0.8;
  font-size: 42px;
  line-height: 1.2;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.01em;
  color: #000000;
}

input:hover,
input:focus,
textarea:hover,
textarea:focus {
  -webkit-box-shadow: 2px 2px 17px rgba(0, 0, 0, 0.6);
          box-shadow: 2px 2px 17px rgba(0, 0, 0, 0.6);
}

.hide {
  display: none;
}

.header {
  margin-top: 29px;
  margin-left: -50%;
  width: 100%;
  position: absolute;
  top: 0;
  left: 50%;
  z-index: 1000;
}

.header__inner {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-pack: center;
      -ms-flex-pack: center;
          justify-content: center;
  background-color: #000;
}

.header__fixed {
  position: fixed;
  margin-top: 10px;
}

@media (max-width: 768px) {
  .header {
    margin-top: 19px;
  }
  .header .container {
    margin: 0 auto;
    max-width: 100%;
    padding: 0 15px;
    width: 100%;
    background-color: #000;
  }
}

.menu {
  display: block;
}

.menu__list {
  width: 100%;
}

.menu__item {
  float: left;
  position: relative;
  width: auto;
}

.menu a {
  display: block;
  padding: 37px 24px;
  text-align: center;
  background-color: #000;
  font-size: 14px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.035em;
  color: #e0e0e0;
  -webkit-transition: all .25s ease;
  -o-transition: all .25s ease;
  transition: all .25s ease;
}

.menu .active {
  background-color: #ff3f40;
}

.menu__item:hover a {
  background-color: #ff3f40;
}

@media (max-width: 768px) {
  .menu {
    display: none;
    width: 100%;
    -webkit-box-orient: vertical;
    -webkit-box-direction: normal;
        -ms-flex-direction: column;
            flex-direction: column;
    background-color: #000;
    text-align: right;
    position: absolute;
    top: 100%;
    right: 0;
  }
  .menu__show {
    display: -webkit-box;
    display: -ms-flexbox;
    display: flex;
  }
  .menu__item {
    width: 100%;
  }
}

.burger {
  display: none;
  padding: 15px 2px;
  background: none;
  border: 0;
  cursor: pointer;
}

.burger__item {
  display: block;
  width: 28px;
  height: 3px;
  background-color: #fff;
  font-size: 0;
  color: transparent;
  position: relative;
  z-index: 30;
}

.burger__item:before, .burger__item:after {
  content: "";
  height: 100%;
  background-color: #fff;
  position: absolute;
  right: 0;
  z-index: 1;
}

.burger__item:before {
  width: 30px;
  top: -8px;
}

.burger__item:after {
  width: 26px;
  bottom: -8px;
}

@media (max-width: 768px) {
  .burger {
    display: block;
    height: 50px;
  }
}

.intro {
  padding: 123px 0;
  background-color: #f6f2f2;
  position: relative;
}

.intro__inner {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -ms-flex-wrap: wrap;
      flex-wrap: wrap;
}

.intro__read {
  width: 50%;
  padding-top: 88px;
}

.intro__title {
  margin-bottom: 17px;
  font-size: 60px;
  line-height: 72px;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 0.025em;
}

.intro__text {
  margin-right: 112px;
  margin-bottom: 52px;
}

.intro__button {
  width: 227px;
  height: 55px;
  font-size: 18.2px;
}

.intro__video {
  width: 50%;
  padding-top: 98px;
  padding-left: 78px;
  -webkit-box-pack: center;
      -ms-flex-pack: center;
          justify-content: center;
}

.video__item {
  max-width: 551px;
  max-height: 356px;
}

.video video {
  -o-object-fit: cover;
     object-fit: cover;
  width: 100%;
}


$(function () {


	/* Fixed Header */
	let header = $("#header");
	let intro = $("#intro");
	let introH = intro.innerHeight();
	let scrollPos = $(window).scrollTop();
	let nav = $("#menu");
	let navToggle = $("#navToggle");

	//Header fixed

	checkScroll(scrollPos, introH);

	$(window).on("scroll resize", function () {
		introH = intro.innerHeight();
		scrollPos = $(this).scrollTop();

		checkScroll(scrollPos, introH);
	});

	function checkScroll(scrollPos, introH) {
		if (scrollPos > introH) {
			header.addClass("header__fixed");
		} else {
			header.removeClass("header__fixed");
		}
	}
	
	//Active

	var sections = $('section')
		
		, nav_height = nav.outerHeight();

	$(window).on('scroll', function () {
		var cur_pos = $(this).scrollTop();

		sections.each(function () {
			var top = $(this).offset().top - nav_height,
				bottom = top + $(this).outerHeight();

			if (cur_pos >= top && cur_pos <= bottom) {
				nav.find('a').removeClass('active');
				sections.removeClass('active');

				$(this).addClass('active');
				nav.find('a[href="#' + $(this).attr('id') + '"]').addClass('active');
			}
		});
	});

	//Smooth scroll

	nav.find('a').on('click', function () {
		var $el = $(this)
			, id = $el.attr('href');

		$('html, body').animate({
			scrollTop: $(id).offset().top - 70
		}, 500);

		return false;
	});

	//Toggle menu

	navToggle.on('click', function () {
		if ($(this).hasClass('menu__show')) {
			$(this).removeClass('menu__show');
			nav.slideUp();
		} else {
			$(this).addClass('menu__show');
			nav.slideDown();
		}
	});

	$(window).resize(function() {
		if ($(window).width() <= 768) {
			$(".menu__item a").click(function () {
				nav.slideToggle(500, function () {
					$(this).hide();
					navToggle.removeClass('menu__show');
				});
			});
		} else {
			nav.show();
		}
	});

});
  • Вопрос задан
  • 714 просмотров
Подписаться 1 Простой Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы