我有一个网站与中心对齐的DIV。现在,一些页面需要滚动,一些不需要。当我从一种类型移动到另一种类型时,滚动条的出现将页面移向一侧几个像素。有没有什么方法可以避免这种情况,而不显式地在每个页面上显示滚动条?
当前回答
我的方法是使轨道透明。滚动条拇指颜色为#C1C1C1,以匹配默认的滚动条拇指颜色。你可以做任何你喜欢的东西:)
试试这个:
html {
overflow-y: scroll;
}
body::-webkit-scrollbar {
width: 0.7em;
background-color: transparent;
}
body::-webkit-scrollbar-thumb {
background: #C1C1C1;
height:30px;
}
body::-webkit-scrollbar-track-piece
{
display:none;
}
其他回答
我试图修复可能由twitter bootstrap .modal-open类应用于body引起的相同问题。解决方案html {overflow-y: scroll}不起作用。我发现的一个可能的解决方案是添加{width: 100%;宽度:100vw}到HTML元素。
与公认的答案相反,即使内容没有溢出屏幕,也建议在浏览器窗口上使用永久滚动条,我更喜欢使用:
html{
height:101%;
}
这是因为如果内容溢出,滚动条的外观更有意义。
这个比这个更有意义。
如果表格的宽度不会改变,你可以设置包含滚动条> 100%的元素(比如tbody)的宽度(允许滚动条的额外空间),并将overflow-y设置为“overlay”(这样滚动条保持固定,并且当它出现时不会将表格向左移动)。同时为带有滚动条的元素设置一个固定的高度,这样一旦超过高度,滚动条就会出现。像这样:
tbody {
height: 100px;
overflow-y: overlay;
width: 105%
}
注意:你必须手动调整宽度%,因为滚动条占用的空间%将相对于你的表格宽度(即:更小的表格宽度,更多的%需要适合滚动条,因为它的像素大小是恒定的)
一个动态表的例子:
function addRow(tableID) { var table = document.getElementById(tableID); var rowCount = table.rows.length; var row = table.insertRow(rowCount); var colCount = table.rows[0].cells.length; for(var i=0; i<colCount; i++) { var newRow = row.insertCell(i); newRow.innerHTML = table.rows[0].cells[i].innerHTML; newRow.childNodes[0].value = ""; } } function deleteRow(row) { var table = document.getElementById("data"); var rowCount = table.rows.length; var rowIndex = row.parentNode.parentNode.rowIndex; document.getElementById("data").deleteRow(rowIndex); } .scroll-table { border-collapse: collapse; } .scroll-table tbody { display:block; overflow-y:overlay; height:60px; width: 105% } .scroll-table tbody td { color: #333; padding: 10px; text-shadow: 1px 1px 1px #fff; } .scroll-table thead tr { display:block; } .scroll-table td { border-top: thin solid; border-bottom: thin solid; } .scroll-table td:first-child { border-left: thin solid; } .scroll-table td:last-child { border-right: thin solid; } .scroll-table tr:first-child { display: none; } .delete_button { background-color: red; color: white; } .container { display: inline-block; } body { text-align: center; } <!DOCTYPE html> <html lang="en"> <head> <link rel="stylesheet" href="test_table.css"> </head> <body> <h1>Dynamic Table</h1> <div class="container"> <table id="data" class="scroll-table"> <tbody> <tr> <td><input type="text" /></td> <td><input type="text" /></td> <td><input type="button" class="delete_button" value="X" onclick="deleteRow(this)"></td> </tr> </tbody> </table> <input type="button" value="Add" onclick="addRow('data')" /> </div> <script src="test_table.js"></script> </body> </html>
我已经在我的一个网站上解决了这个问题,通过显式地在javascript中设置主体的宽度为视口大小减去滚动条的宽度。我使用这里记录的基于jQuery的函数来确定滚动条的宽度。
<body id="bodyid>
var bodyid = document.getElementById('bodyid');
bodyid.style.width = window.innerWidth - scrollbarWidth() + "px";
我不知道这是否是一个旧帖子,但我有同样的问题,如果你想垂直滚动,你应该尝试溢出-y:滚动
推荐文章
- 使伸缩项目正确浮动
- 如何取消最大高度?
- 形式内联内的形式水平在twitter bootstrap?
- 自定义元素在HTML5中有效吗?
- 如何选择在最后一个子元素之前的元素?
- 如何触发自动填充在谷歌Chrome?
- CSS变换,锯齿边缘在铬
- 创建圈div比使用图像更容易的方法?
- 强迫孩子服从父母的弯曲边界在CSS
- 为什么Chrome浏览器不正确地确定页面是在不同的语言,并提供翻译?
- 在CSS中@apply是什么?
- 在网页上用鼠标模拟震颤(例如帕金森病)?
- Bootstrap抛出Uncaught错误:Bootstrap的JavaScript需要jQuery
- 如何改变文本区域的边框颜色:焦点
- 我如何设置背景颜色为文本的宽度,而不是整个元素的宽度,使用CSS?