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

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

Sticky element overlaps absolute element

问题

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

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

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

  1. <!-- begin snippet: js hide: false console: true babel: false -->
  2. <!-- language: lang-js -->
  3. var open = false;
  4. function toggle() {
  5. open = !open;
  6. if (open) {
  7. document.getElementById('menu').classList.add('visible')
  8. } else {
  9. document.getElementById('menu').classList.remove('visible')
  10. }
  11. }
  12. <!-- language: lang-css -->
  13. .parent {
  14. display: grid;
  15. grid-template-areas: "navbar navbar" "sidebar content";
  16. position: relative;
  17. }
  18. .navbar {
  19. height: 50px;
  20. background: #0f0;
  21. grid-area: navbar;
  22. position: sticky;
  23. top: 0;
  24. }
  25. .menu {
  26. display: none;
  27. width: 300px;
  28. height: 300px;
  29. background: #00f;
  30. position: absolute;
  31. }
  32. .visible {
  33. display: block !important;
  34. }
  35. .sidebar {
  36. /* width: 200px; */
  37. height: 100px;
  38. background: #000;
  39. grid-area: sidebar;
  40. position: sticky;
  41. top: 50px;
  42. }
  43. .content {
  44. min-height: 1000px;
  45. background-image: linear-gradient(red,orange,yellow,green,blue,indigo,violet);
  46. grid-area: content;
  47. }
  48. <!-- language: lang-html -->
  49. <div class="parent">
  50. <div class="navbar">
  51. <button onclick="toggle()">Dropdown</button>
  52. <div id="menu" class="menu"></div>
  53. </div>
  54. <div class="sidebar"></div>
  55. <div class="content">
  56. </div>
  57. </div>
  58. <!-- 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 -->

  1. var open = false;
  2. function toggle() {
  3. open = !open;
  4. if (open) {
  5. document.getElementById(&#39;menu&#39;).classList.add(&#39;visibile&#39;)
  6. } else {
  7. document.getElementById(&#39;menu&#39;).classList.remove(&#39;visibile&#39;)
  8. }
  9. }

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

  1. .parent {
  2. display: grid;
  3. grid-template-areas: &quot;navbar navbar&quot; &quot;sidebar content&quot;;
  4. position: relative;
  5. }
  6. .navbar {
  7. height: 50px;
  8. background: #0f0;
  9. grid-area: navbar;
  10. position: sticky;
  11. top: 0;
  12. }
  13. .menu {
  14. display: none;
  15. width: 300px;
  16. height: 300px;
  17. background: #00f;
  18. position: absolute;
  19. }
  20. .visibile {
  21. display: block !important;
  22. }
  23. .sidebar {
  24. /* width: 200px; */
  25. height: 100px;
  26. background: #000;
  27. grid-area: sidebar;
  28. position: sticky;
  29. top: 50px;
  30. }
  31. .content {
  32. min-height: 1000px;
  33. background-image: linear-gradient(red,orange,yellow,green,blue,indigo,violet);
  34. grid-area: content;
  35. }

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

  1. &lt;div class=&quot;parent&quot;&gt;
  2. &lt;div class=&quot;navbar&quot;&gt;
  3. &lt;button onclick=&quot;toggle()&quot;&gt;Dropdown&lt;/button&gt;
  4. &lt;div id=&quot;menu&quot; class=&quot;menu&quot;&gt;&lt;/div&gt;
  5. &lt;/div&gt;
  6. &lt;div class=&quot;sidebar&quot;&gt;&lt;/div&gt;
  7. &lt;div class=&quot;content&quot;&gt;
  8. &lt;/div&gt;
  9. &lt;/div&gt;

<!-- end snippet -->

答案1

得分: 2

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

请参考下面的代码片段。

代码片段 1:

  1. .navbar {
  2. height: 50px;
  3. background: #0f0;
  4. grid-area: navbar;
  5. position: sticky;
  6. top: 0;
  7. z-index: 1; /* 添加 */
  8. }

额外说明

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

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

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

现在,还要添加以下CSS:

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

代码片段 2:

  1. .navbar {
  2. height: 50px;
  3. background: #0f0;
  4. grid-area: navbar;
  5. position: sticky;
  6. top: 0;
  7. z-index: 1; /* 添加 */
  8. }
  9. /* 添加 */
  10. .as-console-wrapper {
  11. z-index: 2;
  12. }

如果运行代码片段 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 -->

  1. var open = false;
  2. function toggle() {
  3. open = !open;
  4. if (open) {
  5. document.getElementById(&#39;menu&#39;).classList.add(&#39;visibile&#39;)
  6. } else {
  7. document.getElementById(&#39;menu&#39;).classList.remove(&#39;visibile&#39;)
  8. }
  9. }
  10. /* Added */
  11. const link = document.querySelector(&quot;a&quot;);
  12. link.addEventListener(&quot;click&quot;, () =&gt; {
  13. console.log(&quot;Clicked&quot;);
  14. });

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

  1. .parent {
  2. display: grid;
  3. grid-template-areas: &quot;navbar navbar&quot; &quot;sidebar content&quot;;
  4. position: relative;
  5. }
  6. .navbar {
  7. height: 50px;
  8. background: #0f0;
  9. grid-area: navbar;
  10. position: sticky;
  11. top: 0;
  12. z-index: 1; /* Added */
  13. }
  14. .menu {
  15. display: none;
  16. width: 300px;
  17. height: 300px;
  18. background: #00f;
  19. position: absolute;
  20. }
  21. /* Added */
  22. .menu a {
  23. color: white;
  24. }
  25. .visibile {
  26. display: block !important;
  27. }
  28. .sidebar {
  29. height: 100px;
  30. background: #000;
  31. grid-area: sidebar;
  32. position: sticky;
  33. top: 50px;
  34. }
  35. .content {
  36. min-height: 1000px;
  37. background-image: linear-gradient(red, orange, yellow, green, blue, indigo, violet);
  38. grid-area: content;
  39. }

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

  1. &lt;div class=&quot;parent&quot;&gt;
  2. &lt;div class=&quot;navbar&quot;&gt;
  3. &lt;button onclick=&quot;toggle()&quot;&gt;Dropdown&lt;/button&gt;
  4. &lt;div id=&quot;menu&quot; class=&quot;menu&quot;&gt;
  5. &lt;a href=&quot;#&quot;&gt;Click me&lt;/a&gt;
  6. &lt;/div&gt;
  7. &lt;/div&gt;
  8. &lt;div class=&quot;sidebar&quot;&gt;&lt;/div&gt;
  9. &lt;div class=&quot;content&quot;&gt;&lt;/div&gt;
  10. &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:

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

Snippet 2:

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

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

  1. var open = false;
  2. function toggle() {
  3. open = !open;
  4. if (open) {
  5. document.getElementById(&#39;menu&#39;).classList.add(&#39;visibile&#39;)
  6. } else {
  7. document.getElementById(&#39;menu&#39;).classList.remove(&#39;visibile&#39;)
  8. }
  9. }
  10. /* Added */
  11. const link = document.querySelector(&quot;a&quot;);
  12. link.addEventListener(&quot;click&quot;, () =&gt; {
  13. console.log(&quot;Clicked&quot;);
  14. });

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

  1. .parent {
  2. display: grid;
  3. grid-template-areas: &quot;navbar navbar&quot; &quot;sidebar content&quot;;
  4. position: relative;
  5. }
  6. .navbar {
  7. height: 50px;
  8. background: #0f0;
  9. grid-area: navbar;
  10. position: sticky;
  11. top: 0;
  12. z-index: 1; /* Added */
  13. }
  14. /* Added */
  15. .as-console-wrapper {
  16. z-index: 2;
  17. }
  18. .menu {
  19. display: none;
  20. width: 300px;
  21. height: 300px;
  22. background: #00f;
  23. position: absolute;
  24. }
  25. /* Added */
  26. .menu a {
  27. color: white;
  28. }
  29. .visibile {
  30. display: block !important;
  31. }
  32. .sidebar {
  33. height: 100px;
  34. background: #000;
  35. grid-area: sidebar;
  36. position: sticky;
  37. top: 50px;
  38. }
  39. .content {
  40. min-height: 1000px;
  41. background-image: linear-gradient(red, orange, yellow, green, blue, indigo, violet);
  42. grid-area: content;
  43. }

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

  1. &lt;div class=&quot;parent&quot;&gt;
  2. &lt;div class=&quot;navbar&quot;&gt;
  3. &lt;button onclick=&quot;toggle()&quot;&gt;Dropdown&lt;/button&gt;
  4. &lt;div id=&quot;menu&quot; class=&quot;menu&quot;&gt;
  5. &lt;a href=&quot;#&quot;&gt;Click me&lt;/a&gt;
  6. &lt;/div&gt;
  7. &lt;/div&gt;
  8. &lt;div class=&quot;sidebar&quot;&gt;&lt;/div&gt;
  9. &lt;div class=&quot;content&quot;&gt;&lt;/div&gt;
  10. &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:

确定