【问题标题】:Unable to use position:sticky; while adding element through .innerHTML无法使用位置:粘性;通过 .innerHTML 添加元素时
【发布时间】:2020-03-24 12:39:56
【问题描述】:

我想让菜单贴在页面顶部。 如果我只是将代码添加到页面,代码就可以正常工作。 但是,我想使用 javascript,因为我希望能够更改每个页面上的菜单,而不必在每个页面上手动进行。

document.getElementById('menu-js').innerHTML = `<div id="menu">
	<div class="dropdown">
			<button class="dropbtn">Button_1</button>
			<div class="dropdown-content">
				<a href="/src">Link 1</a>
				<a href="/src">Link 2</a>
				<a href="/src">Link 3</a>
			</div>
		</div>
	<div class="dropdown">
		<button class="dropbtn">Button_2</button>
		<div class="dropdown-content">
			<a href="/src">Link 4</a>
			<a href="/src">Link 5</a>
			<a href="/src">Link 6</a>
		</div>
	</div>
</div>`
@import url('https://fonts.googleapis.com/css?family=Abril+Fatface|Arsenal|Rubik&display=swap');
#menu {
  background-color: white;
  top: 0px;
  left: 0px;
  position: sticky;
  padding-left: 10px;
  margin-top: -8px;
  margin-left: -30px;
  margin-right: -0.51%;
  border-bottom-color: black;
  border-bottom-style: solid;
  border-bottom: 5px;
}

.dropbtn {
  background-color: white;
  color: black;
  padding: 16px;
  font-size: 16px;
  border: none;
  margin: none;
  font-family: 'Arsenal';
}

.dropdown {
  display: inline-block;
}

.dropdown-content {
  display: none;
  position: absolute;
  background-color: #f2d3d8;
  min-width: 200px;
  z-index: 1;
  font-family: 'Arsenal';
}

.dropdown-content a {
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
}

.dropdown-content a:hover {
  background-color: #ffd3b6;
}

.dropdown:hover .dropdown-content {
  display: block;
}

.dropdown:hover .dropbtn {
  background-color: #c06014;
  color: white;
}

#page-container {
  position: relative;
  min-height: 100vh;
}

#content-wrap {
  padding-bottom: 2.5rem;
}

footer {
  position: absolute;
  bottom: 0;
  width: 100%;
  height: 2.5rem;
}

hr {
  margin-left: -20px;
  color: #c06014
}
<html>

<head>
  <title>TITLE</title>
  <link rel="stylesheet" type="text/css" href="style.css">
</head>

<body>
  <div id='page-container'>
    <div id='content-wrap'>
      <h1>Content above menu</h1>
      <div id='menu-js'></div>
      <h1>body</h1>
      <h2>body</h2>
    </div>
    <footer>
      <hr>FOOTER</footer>
  </div>
  <script src='menu.js'></script>
</body>

</html>

如果可能,我只想使用这三种语言。 谢谢!

【问题讨论】:

  • 你的问题到底是什么?
  • @Olian04 我想让我的菜单栏贴在页面顶部,但是当我使用 JS (.innerHTML) 时我没有成功。但是,我只需将代码粘贴到menu-js div 所在的位置就可以做到这一点。我不想添加原始代码,因为我想保留菜单的来源,所以如果我更改菜单,我不需要单独更新每个页面

标签: javascript html css css-position innerhtml


【解决方案1】:

就粘性属性而言,您的代码看起来几乎是完美的。 尝试将max-height 属性(像素值)添加到menu-js div。

【讨论】:

  • 抱歉,这有什么帮助?我尝试使用不同的值,但它要么什么也没做,要么(使用小值)在菜单栏下推送了一些文本
  • 菜单 div 需要有一些固定的高度,因为我们设置的是“top: 0 & position: sticky”。我刚刚检查并在 menu-js 中添加了“max-height: 10px”,所以它起作用了。有关更多详细信息,您可以通过链接:medium.com/@elad/… 在页面末尾有一个示例,该示例也具有一些固定的菜单高度。
  • 我的代码在普通 HTML 中运行良好。它使用破坏它的JS添加代码
猜你喜欢
  • 1970-01-01
  • 2019-11-13
  • 1970-01-01
  • 2019-09-30
  • 1970-01-01
  • 2019-02-28
  • 2021-08-24
  • 2017-11-04
  • 1970-01-01
相关资源
最近更新 更多