如何去除不需要的位置间隙:粘性

Krz*_*icz 5 html javascript css position sticky

我有一个具有以下属性的小元素:“显示:无”和“位置:粘性”。该元素将其显示属性从无更改为阻止按钮单击。问题是当元素出现时,我的表格上方也会出现一个白色间隙(我相信这是粘性元素在页面上占据的空间)。

有没有可能在不牺牲元素的粘性的情况下删除空白?我曾尝试将父元素的位置设置为相对/绝对,但没有产生任何积极的结果。

这是代码:

function showSettings() {
    var sidebar = document.getElementById("sidebar");
    sidebar.style.display = "block";
}

function closeSettings() {
    var sidebar = document.getElementById("sidebar");
    sidebar.style.display = "none";
}
Run Code Online (Sandbox Code Playgroud)
 #sidebar {
    display: none;
    position: sticky;
    position: -webkit-sticky;
    left: 10px;
    top: 50%;
    max-width: 150px;
    background: rgba(204, 204, 204, 0.493);
}

.my-table {
    margin-top: 50px;
    margin-left: 250px;
    margin-bottom: 3rem;
}

ul {
    text-align: start;
}

.my-btn {
  background: red;
  border: none;
  color: white;
}

#sidebar h4 {
  text-align: center;
}

.table-div {
  height: 900px;
}
Run Code Online (Sandbox Code Playgroud)
<div class="table-div">
  <nav id="sidebar">
      <h4>Quick menu:</h4>
        <ul>
          <a href="#">Secondary nav</a>
          <a href="#">Secondary nav</a>
          <a href="#">Secondary nav</a>
          <a href="#">Secondary nav</a>
          <button class="my-btn" onclick="closeSettings()">close</button>
        </ul>
      </nav>
<table class="my-table" id="myTable">
    <tr>
        <th class="button"><button id="settings" type="button" onclick="showSettings()">Quick Menu</button></th>
        <th>Fill input here:</th>
        <th></th>
    </tr>

    <tr>
        <td>Example text</td>
        <td><input type="text"></td>
        <td>Example end.</td>
       <td><button  type="button">
            Button 1</button></td>
        <td><button  type="button">
            Button 2</button></td>
        <td><button  type="button">
            Button 3</button></td>
      </tr>
  <tr>

  </tr>
  </table>
</div>

   
Run Code Online (Sandbox Code Playgroud)

代码笔:https ://codepen.io/Delest/pen/RwbyGLK

“为什么不使用 position: fixed 来代替?”

好吧,这里的问题是元素不会停在页脚处。我试图寻找一个答案,如何使用一些“onscroll”选项让元素停在那里,但没有任何普通的 JS 解决方案,因此我选择了 position:sticky 选项。

如果我的问题的答案涉及 JS/jQuery,我更喜欢 vanilla JavaScript 选项。

小智 6

您可以简单地添加height: 0到粘性元素。但不要忘记为内部元素添加高度值以避免消失。另外,如果您需要将元素移回来,您可以添加transform: translateY(-elHeight)


小智 3

您可以查看一下吗?

我只是添加float:left;进来#sidebar

function showSettings() {
    var sidebar = document.getElementById("sidebar");
    sidebar.style.display = "block";
}

function closeSettings() {
    var sidebar = document.getElementById("sidebar");
    sidebar.style.display = "none";
}
Run Code Online (Sandbox Code Playgroud)
#sidebar {
    display: none;
  float:left;
    position: sticky;
    position: -webkit-sticky;
    left: 10px;
    top:50%;
    max-width: 150px;
    background: rgba(204, 204, 204, 0.493);
}

.my-table {
        margin-top: 50px;
        margin-left: 250px;
        margin-bottom: 3rem;
    }

ul {
  text-align: start;
}

.my-btn {
  background: red;
  border: none;
  color: white;
}

#sidebar h4 {
  text-align: center;
}

.table-div {
  height: 500px;
}
Run Code Online (Sandbox Code Playgroud)
<div class="table-div">
  <nav id="sidebar">
      <h4>Quick menu:</h4>
        <ul>
          <a href="#">Secondary nav</a>
          <a href="#">Secondary nav</a>
          <a href="#">Secondary nav</a>
          <a href="#">Secondary nav</a>
          <button class="my-btn" onclick="closeSettings()">close</button>
        </ul>
      </nav>
<table class="my-table" id="myTable">
    <tr>
        <th class="button"><button id="settings" type="button" onclick="showSettings()">Quick Menu</button></th>
        <th>Fill input here:</th>
        <th></th>
    </tr>

    <tr>
        <td>Example text</td>
        <td><input type="text"></td>
        <td>Example end.</td>
       <td><button  type="button">
            Button 1</button></td>
        <td><button  type="button">
            Button 2</button></td>
        <td><button  type="button">
            Button 3</button></td>
      </tr>
  <tr>

  </tr>
  </table>
</div>
Run Code Online (Sandbox Code Playgroud)