简体   繁体   English

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

[英]How to remove an unwanted gap with position: sticky

I have a small element with the following properties: "display: none" and "position: sticky".我有一个具有以下属性的小元素:“显示:无”和“位置:粘性”。 The element changes its display property from none to block on a button click.该元素将其显示属性从无更改为阻止按钮单击。 The problem is that when the element appears a white gap above my table also appears (I believe it is the space the sticky element occupies on the page).问题是当元素出现时,我的表格上方也会出现一个白色间隙(我相信这是粘性元素在页面上占据的空间)。

Is there any possibility to remove the white space without sacrificing the sticky property of the element?有没有可能在不牺牲元素的粘性的情况下删除空白? I have tried making the parent element's position to relative/absolute but it didn't yield any positive results.我曾尝试将父元素的位置设置为相对/绝对,但没有产生任何积极的结果。

Here is the code:这是代码:

 function showSettings() { var sidebar = document.getElementById("sidebar"); sidebar.style.display = "block"; } function closeSettings() { var sidebar = document.getElementById("sidebar"); sidebar.style.display = "none"; }
 #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; }
 <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>

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

"Why not use position: fixed instead?" “为什么不使用 position: fixed 来代替?”

Well, the problem here is that the element won't stop at the footer.好吧,这里的问题是元素不会停在页脚处。 I have tried to look for an answer how to make the element stop there with some "onscroll" options but there aren't any vanilla JS solutions, hence why I opt for the position: sticky option.我试图寻找一个答案,如何使用一些“onscroll”选项让元素停在那里,但没有任何普通的 JS 解决方案,因此我选择了 position:sticky 选项。

If an answer to my question involves JS/jQuery, I very much more prefer vanilla JavaScript option.如果我的问题的答案涉及 JS/jQuery,我更喜欢 vanilla JavaScript 选项。

You can simply add height: 0 to you sticky element.But don't forget to add height value to the inner elements to avoid disappearance.您可以简单地将height: 0添加到您的粘性元素。但不要忘记为内部元素添加高度值以避免消失。 Also if you need move the element back you can add the transform: translateY(-elHeight)此外,如果您需要将元素移回,您可以添加transform: translateY(-elHeight)

can you please review this?你能检查一下吗?

i just add float:left;我只是添加float:left; in #sidebar .#sidebar

 function showSettings() { var sidebar = document.getElementById("sidebar"); sidebar.style.display = "block"; } function closeSettings() { var sidebar = document.getElementById("sidebar"); sidebar.style.display = "none"; }
 #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; }
 <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>

可能是“粘性”元素的父元素或祖先元素有“填充”,尝试删除它,可能没问题

I have a small element with the following properties: "display: none" and "position: sticky".我有一个具有以下属性的小元素:“显示:无”和“位置:粘性”。 The element changes its display property from none to block on a button click.元素将其显示属性从无更改为单击按钮时阻塞。 The problem is that when the element appears a white gap above my table also appears (I believe it is the space the sticky element occupies on the page).问题在于,当该元素出现在我的表格上方时,也会出现白色缝隙(我相信这是该粘性元素在页面上占据的空间)。

Is there any possibility to remove the white space without sacrificing the sticky property of the element?有可能在不牺牲元素的粘性的情况下删除空白吗? I have tried making the parent element's position to relative/absolute but it didn't yield any positive results.我尝试将父元素的位置设为相对/绝对,但未产生任何积极结果。

Here is the code:这是代码:

 function showSettings() { var sidebar = document.getElementById("sidebar"); sidebar.style.display = "block"; } function closeSettings() { var sidebar = document.getElementById("sidebar"); sidebar.style.display = "none"; }
 #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; }
 <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>

Codepen: https://codepen.io/Delest/pen/RwbyGLK Codepen: https ://codepen.io/Delest/pen/RwbyGLK

"Why not use position: fixed instead?" “为什么不使用位置:固定?”

Well, the problem here is that the element won't stop at the footer.好吧,这里的问题是元素不会在页脚停下来。 I have tried to look for an answer how to make the element stop there with some "onscroll" options but there aren't any vanilla JS solutions, hence why I opt for the position: sticky option.我试图寻找一个答案,该如何使用一些“ onsroll”选项使元素停在那里,但是没有任何普通的JS解决方案,因此为什么我选择了position:sticky选项。

If an answer to my question involves JS/jQuery, I very much more prefer vanilla JavaScript option.如果我的问题的答案涉及JS / jQuery,则我更喜欢使用普通JavaScript选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM