固定div滚动时div落后于flexbox

时间:2019-07-09 11:57:01

标签: css flexbox position

我是“编程”网站的新手(只是为了好玩),遇到了一个我找不到解决方案的问题。

代码末尾或打开代码/ Tisire / pen / wLRvdR

菜单按钮应该固定并且永远不会隐藏 但是向下滚动时,菜单按钮位于弹性框后面

我已经在这里发现了类似的问题,但是对我来说却没有可行的解决方案……弯曲和位置似乎不是一个很好的组合。

有人知道吗?

代码:

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 个答案:

答案 0 :(得分:2)

只需将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 :(得分:1)

z-index: 1;添加到您的#menubutton

查看此fiddle