粘性元素重叠绝对定位元素

huangapple go评论69阅读模式
英文:

Sticky element overlaps absolute element

问题

我想创建一个带有粘性导航栏和侧边栏的页面。只有内容区域不应该是粘性的。

导航栏应该有一个下拉按钮。问题是粘性侧边栏现在会重叠在下拉菜单上。如果下拉菜单有多个项目,您就无法单击其中一个项目。

有没有一种方法可以强制下拉菜单重叠在粘性侧边栏上?

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->
var open = false;
function toggle() {
  open = !open;
  if (open) {
    document.getElementById('menu').classList.add('visible')
  } else {
    document.getElementById('menu').classList.remove('visible')
  }
}

<!-- language: lang-css -->
.parent {
  display: grid;
  grid-template-areas: "navbar navbar" "sidebar content";
  position: relative;
}

.navbar {
  height: 50px;
  background: #0f0;
  grid-area: navbar;
  position: sticky;
  top: 0;
}

.menu {
  display: none;
  width: 300px;
  height: 300px;
  background: #00f;
  position: absolute;
}

.visible {
    display: block !important;
}

.sidebar {
  /* width: 200px; */
  height: 100px;
  background: #000;
  grid-area: sidebar;
  position: sticky;
  top: 50px;
}

.content {
  min-height: 1000px;
  background-image: linear-gradient(red,orange,yellow,green,blue,indigo,violet);
  grid-area: content;
}

<!-- language: lang-html -->
<div class="parent">
  <div class="navbar">
    <button onclick="toggle()">Dropdown</button>
    <div id="menu" class="menu"></div>
  </div>
  <div class="sidebar"></div>
  <div class="content">

  </div>
</div>

<!-- end snippet -->
英文:

I want to create a page with a sticky navbar and sidebar. Only the content area shouldn't be sticky.

The navbar should have a dropdown button. The problem now is that the sticky sidebar overlaps the dropdown menu. And if the dropdown has multiple items, you can't click on an item.

Is there a way to force the dropdown menu to overlap the sticky sidebar?

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

var open = false;
function toggle() {
  open = !open;
  if (open) {
    document.getElementById(&#39;menu&#39;).classList.add(&#39;visibile&#39;)
  } else {
    document.getElementById(&#39;menu&#39;).classList.remove(&#39;visibile&#39;)
  }
}

<!-- language: lang-css -->

.parent {
  display: grid;
  grid-template-areas: &quot;navbar navbar&quot; &quot;sidebar content&quot;;
  position: relative;
}

.navbar {
  height: 50px;
  background: #0f0;
  grid-area: navbar;
  position: sticky;
  top: 0;
}

.menu {
  display: none;
  width: 300px;
  height: 300px;
  background: #00f;
  position: absolute;
}

.visibile {
    display: block !important;
}

.sidebar {
/*   width: 200px; */
  height: 100px;
  background: #000;
  grid-area: sidebar;
  position: sticky;
  top: 50px;
}

.content {
  min-height: 1000px;
  background-image: linear-gradient(red,orange,yellow,green,blue,indigo,violet);
  grid-area: content;
}

<!-- language: lang-html -->

&lt;div class=&quot;parent&quot;&gt;
  &lt;div class=&quot;navbar&quot;&gt;
    &lt;button onclick=&quot;toggle()&quot;&gt;Dropdown&lt;/button&gt;
    &lt;div id=&quot;menu&quot; class=&quot;menu&quot;&gt;&lt;/div&gt;
  &lt;/div&gt;
  &lt;div class=&quot;sidebar&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;content&quot;&gt;

  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

答案1

得分: 2

你需要将navbarz-index设置为1。了解更多关于z-index CSS属性的信息。因此,navbar将变得可点击。

请参考下面的代码片段。

代码片段 1:

.navbar {
  height: 50px;
  background: #0f0;
  grid-area: navbar;
  position: sticky;
  top: 0;
  z-index: 1; /* 添加 */
}

额外说明

你的代码非常适合演示z-index

如果运行代码片段 1,打开下拉菜单并单击链接,你将看到以下效果:

粘性元素重叠绝对定位元素

现在,还要添加以下CSS:

.as-console-wrapper {
  z-index: 2;
}

代码片段 2:

.navbar {
  height: 50px;
  background: #0f0;
  grid-area: navbar;
  position: sticky;
  top: 0;
  z-index: 1; /* 添加 */
}

/* 添加 */
.as-console-wrapper {
  z-index: 2;
}

如果运行代码片段 2,打开下拉菜单并单击链接,你将看到以下效果:

粘性元素重叠绝对定位元素

为什么?因为具有较大z-index的元素会覆盖具有较小z-index的元素。

英文:

You have to set z-index: 1; to the navbar. Read more about the z-index CSS property. Consequently, the navbar becomes clickable.

See the snippet below.

Snippet 1:

<!-- begin snippet: js hide: true console: true babel: false -->

<!-- language: lang-js -->

var open = false;

function toggle() {
  open = !open;
  if (open) {
    document.getElementById(&#39;menu&#39;).classList.add(&#39;visibile&#39;)
  } else {
    document.getElementById(&#39;menu&#39;).classList.remove(&#39;visibile&#39;)
  }
}

/* Added */
const link = document.querySelector(&quot;a&quot;);
link.addEventListener(&quot;click&quot;, () =&gt; {
  console.log(&quot;Clicked&quot;);
});

<!-- language: lang-css -->

.parent {
  display: grid;
  grid-template-areas: &quot;navbar navbar&quot; &quot;sidebar content&quot;;
  position: relative;
}

.navbar {
  height: 50px;
  background: #0f0;
  grid-area: navbar;
  position: sticky;
  top: 0;
  z-index: 1; /* Added */
}

.menu {
  display: none;
  width: 300px;
  height: 300px;
  background: #00f;
  position: absolute;
}

/* Added */
.menu a {
  color: white;
}

.visibile {
  display: block !important;
}

.sidebar {
  height: 100px;
  background: #000;
  grid-area: sidebar;
  position: sticky;
  top: 50px;
}

.content {
  min-height: 1000px;
  background-image: linear-gradient(red, orange, yellow, green, blue, indigo, violet);
  grid-area: content;
}

<!-- language: lang-html -->

&lt;div class=&quot;parent&quot;&gt;
  &lt;div class=&quot;navbar&quot;&gt;
    &lt;button onclick=&quot;toggle()&quot;&gt;Dropdown&lt;/button&gt;
    &lt;div id=&quot;menu&quot; class=&quot;menu&quot;&gt;
      &lt;a href=&quot;#&quot;&gt;Click me&lt;/a&gt;
    &lt;/div&gt;
  &lt;/div&gt;
  &lt;div class=&quot;sidebar&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;content&quot;&gt;&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

Additional explanation

Your code is perfect to demonstrate the z-index.

If you run Snippet 1, open the dropdown and click on the link, you'll see the following:

粘性元素重叠绝对定位元素

Now, also add the following CSS:

.as-console-wrapper {
  z-index: 2;
}

Snippet 2:

<!-- begin snippet: js hide: true console: true babel: false -->

<!-- language: lang-js -->

var open = false;

function toggle() {
  open = !open;
  if (open) {
    document.getElementById(&#39;menu&#39;).classList.add(&#39;visibile&#39;)
  } else {
    document.getElementById(&#39;menu&#39;).classList.remove(&#39;visibile&#39;)
  }
}

/* Added */
const link = document.querySelector(&quot;a&quot;);
link.addEventListener(&quot;click&quot;, () =&gt; {
  console.log(&quot;Clicked&quot;);
});

<!-- language: lang-css -->

.parent {
  display: grid;
  grid-template-areas: &quot;navbar navbar&quot; &quot;sidebar content&quot;;
  position: relative;
}

.navbar {
  height: 50px;
  background: #0f0;
  grid-area: navbar;
  position: sticky;
  top: 0;
  z-index: 1; /* Added */
}

/* Added */
.as-console-wrapper {
  z-index: 2;
}

.menu {
  display: none;
  width: 300px;
  height: 300px;
  background: #00f;
  position: absolute;
}

/* Added */
.menu a {
  color: white;
}

.visibile {
  display: block !important;
}

.sidebar {
  height: 100px;
  background: #000;
  grid-area: sidebar;
  position: sticky;
  top: 50px;
}

.content {
  min-height: 1000px;
  background-image: linear-gradient(red, orange, yellow, green, blue, indigo, violet);
  grid-area: content;
}

<!-- language: lang-html -->

&lt;div class=&quot;parent&quot;&gt;
  &lt;div class=&quot;navbar&quot;&gt;
    &lt;button onclick=&quot;toggle()&quot;&gt;Dropdown&lt;/button&gt;
    &lt;div id=&quot;menu&quot; class=&quot;menu&quot;&gt;
      &lt;a href=&quot;#&quot;&gt;Click me&lt;/a&gt;
    &lt;/div&gt;
  &lt;/div&gt;
  &lt;div class=&quot;sidebar&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;content&quot;&gt;&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

If you run Snippet 2, open the dropdown and click on the link, you'll see the following:

粘性元素重叠绝对定位元素

Why? Because elements with a larger z-index overlap those with a smaller one.

huangapple
  • 本文由 发表于 2023年6月1日 17:37:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76380540.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定