我有一个网站与中心对齐的DIV。现在,一些页面需要滚动,一些不需要。当我从一种类型移动到另一种类型时,滚动条的出现将页面移向一侧几个像素。有没有什么方法可以避免这种情况,而不显式地在每个页面上显示滚动条?
当前回答
像这样简单地设置容器元素的宽度就可以了
width: 100vw;
这将使该元素忽略滚动条,并与背景颜色或图像一起工作。
其他回答
我使用一些jquery来解决这个问题
$('html').css({
'overflow-y': 'hidden'
});
$(document).ready(function(){
$(window).load(function() {
$('html').css({
'overflow-y': ''
});
});
});
如果表格的宽度不会改变,你可以设置包含滚动条> 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>
我试图修复可能由twitter bootstrap .modal-open类应用于body引起的相同问题。解决方案html {overflow-y: scroll}不起作用。我发现的一个可能的解决方案是添加{width: 100%;宽度:100vw}到HTML元素。
如果改变大小或加载一些数据后,它是添加滚动条,然后你可以尝试以下,创建类和应用这个类。
.auto-scroll {
overflow-y: overlay;
overflow-x: overlay;
}
我的方法是使轨道透明。滚动条拇指颜色为#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?
- 自定义元素在HTML5中有效吗?
- 如何选择在最后一个子元素之前的元素?
- 如何触发自动填充在谷歌Chrome?
- CSS变换,锯齿边缘在铬
- 创建圈div比使用图像更容易的方法?
- 强迫孩子服从父母的弯曲边界在CSS
- 为什么Chrome浏览器不正确地确定页面是在不同的语言,并提供翻译?
- 在CSS中@apply是什么?
- 在网页上用鼠标模拟震颤(例如帕金森病)?
- Bootstrap抛出Uncaught错误:Bootstrap的JavaScript需要jQuery
- 如何改变文本区域的边框颜色:焦点
- 我如何设置背景颜色为文本的宽度,而不是整个元素的宽度,使用CSS?