Исправлено, что div прокручивается за flexbox при прокрутке - PullRequest
0 голосов
/ 09 июля 2019

Я новичок в "программировании" сайтов (просто для удовольствия), и у меня возникла проблема, для которой я не нашел решения.

Код в конце или codeopen / Tisire / pen / wLRvdR

Кнопка меню должна быть фиксированной и никогда не скрываться, но при прокрутке вниз кнопка меню идет за flexbox

Я уже нашел здесь похожие проблемы, но без рабочего решения для меня ... flex и positionкажется, не очень хорошая комбинация.

У кого-нибудь есть идея?

Код:

function openNav() {
  document.getElementById("myNav").style.height = "100%";
}
function closeNav() {
  document.getElementById("myNav").style.height = "0%";
}
/* Menu */
#topnav {
  margin-left: auto;
  margin-right: auto; 
  padding-left: 1em; 
  padding-right: 1em;
  max-width: 70em;
}

#logo {
  font-size:2em;
  float:left;
  padding-top: 0.5em;
}

#menubutton {
  font-size:80px;
  cursor:pointer;
  position:fixed;
}

#menuright {
  float: right;
  padding-right:5em;
}

.overlay {
  height: 0%;
  width: 100%;
  position: fixed;
  z-index: 1;
  top: 0;
  left: 0;
  background-color: rgb(0,0,0);
  background-color: rgba(0,0,0, 0.9);
  overflow-y: hidden;
  transition: 0.5s;
}

.overlay-content {
  position: relative;
  top: 25%;
  width: 100%;
  text-align: center;
  margin-top: 30px;
}

.overlay a {
  padding: 8px;
  text-decoration: none;
  font-size: 36px;
  color: #818181;
  display: block;
  transition: 0.3s;
}

.overlay a:hover, .overlay a:focus {
  color: #f1f1f1;
}

.overlay .closebtn {
  position: absolute;
  top: 20px;
  right: 45px;
  font-size: 60px;
}

@media screen and (max-height: 450px) {
  .overlay {overflow-y: auto;}
  .overlay a {font-size: 20px}
  .overlay .closebtn {
  font-size: 40px;
  top: 15px;
  right: 35px;
  }
}

/*Just to fill space...*/
.somespaceforthistest {
  padding-top: 100px;
  text-align: center;
}

/*The Flexbox*/
	.flex-container {
	  display: flex;
	  flex-wrap: wrap;
	  justify-content: center;
	  max-width: 80em;
	  margin-left: auto;
	  margin-right: auto;
	}

	.flex-container > div {
	  background-color: #f1f1f1;
	  width: 25em;
	  margin: 10px;
	  height: 25em;
	  position: relative;
	}
	
	img.flex {
		height: 100%;
		width: 100%;
	}

  
  
<div id="myNav" class="overlay">
		  <a href="javascript:void(0)" class="closebtn" onclick="closeNav()">&times;</a>
		  <div class="overlay-content">
			<a href="#">Just</a>
			<a href="#">some</a>
			<a href="#">random</a>
			<a href="#">points</a>
		  </div>
		</div>

<div id="topnav">
			<div id="logo">MyLogo</div>
			<div id="menuright">
				<div id="menubutton" onclick="openNav()">&#9776;</div>
			</div>
		</div>

<div class="somespaceforthistest">
  Just some Text to fill space...<br>
  Just some Text to fill space...<br>
  Just some Text to fill space...<br>
  Just some Text to fill space...<br>
  Just some Text to fill space...<br>
  Just some Text to fill space...<br>
  Just some Text to fill space...<br>
</div>

<div class="flex-container">
    <div>1</div>
	  <div>2</div>
	  <div>3</div>  
	  <div>4</div>
	  <div>5</div>
	  <div>6</div>  
	  <div>7</div>
	  <div>8</div>
	  <div>9</div>  
	  <div>10</div>
	  <div>11</div>
	  <div>12</div>  
	</div>

Ответы [ 2 ]

2 голосов
/ 09 июля 2019

просто добавьте свойство z-index к вашей кнопке

function openNav() {
  document.getElementById("myNav").style.height = "100%";
}
function closeNav() {
  document.getElementById("myNav").style.height = "0%";
}
/* Menu */
#topnav {
  margin-left: auto;
  margin-right: auto; 
  padding-left: 1em; 
  padding-right: 1em;
  max-width: 70em;
}

#logo {
  font-size:2em;
  float:left;
  padding-top: 0.5em;
}

#menubutton {
  font-size:80px;
  cursor:pointer;
 z-index:3;
  position:fixed;
}

#menuright {
  float: right;
  padding-right:5em;
}

.overlay {
  height: 0%;
  width: 100%;
  position: fixed;
  z-index: 1;
  top: 0;
  left: 0;
  background-color: rgb(0,0,0);
  background-color: rgba(0,0,0, 0.9);
  overflow-y: hidden;
  transition: 0.5s;
}

.overlay-content {
  position: relative;
  top: 25%;
  width: 100%;
  text-align: center;
  margin-top: 30px;
}

.overlay a {
  padding: 8px;
  text-decoration: none;
  font-size: 36px;
  color: #818181;
  display: block;
  transition: 0.3s;
}

.overlay a:hover, .overlay a:focus {
  color: #f1f1f1;
}

.overlay .closebtn {
  position: absolute;
  top: 20px;
  right: 45px;
  font-size: 60px;
}

@media screen and (max-height: 450px) {
  .overlay {overflow-y: auto;}
  .overlay a {font-size: 20px}
  .overlay .closebtn {
  font-size: 40px;
  top: 15px;
  right: 35px;
  }
}

/*Just to fill space...*/
.somespaceforthistest {
  padding-top: 100px;
  text-align: center;
}

/*The Flexbox*/
	.flex-container {
	  display: flex;
	  flex-wrap: wrap;
	  justify-content: center;
	  max-width: 80em;
	  margin-left: auto;
	  margin-right: auto;
	}

	.flex-container > div {
	  background-color: #f1f1f1;
	  width: 25em;
	  margin: 10px;
	  height: 25em;
	  position: relative;
	}
	
	img.flex {
		height: 100%;
		width: 100%;
	}

  
  
<div id="myNav" class="overlay">
		  <a href="javascript:void(0)" class="closebtn" onclick="closeNav()">&times;</a>
		  <div class="overlay-content">
			<a href="#">Just</a>
			<a href="#">some</a>
			<a href="#">random</a>
			<a href="#">points</a>
		  </div>
		</div>

<div id="topnav">
			<div id="logo">MyLogo</div>
			<div id="menuright">
				<div id="menubutton" onclick="openNav()">&#9776;</div>
			</div>
		</div>

<div class="somespaceforthistest">
  Just some Text to fill space...<br>
  Just some Text to fill space...<br>
  Just some Text to fill space...<br>
  Just some Text to fill space...<br>
  Just some Text to fill space...<br>
  Just some Text to fill space...<br>
  Just some Text to fill space...<br>
</div>

<div class="flex-container">
    <div>1</div>
	  <div>2</div>
	  <div>3</div>  
	  <div>4</div>
	  <div>5</div>
	  <div>6</div>  
	  <div>7</div>
	  <div>8</div>
	  <div>9</div>  
	  <div>10</div>
	  <div>11</div>
	  <div>12</div>  
	</div>
1 голос
/ 09 июля 2019

Добавьте z-index: 1; к вашему #menubutton

См. Это скрипка

...