我不确定“粘性”是否是这个术语,但是有没有办法让 overflow:auto
的滚动条保持可见?
我有一个相当大的表格,我希望它可以水平滚动;然而,表格也相当高,所以当页面加载时,水平滚动条不在浏览器的视口(viewport)内,因此很难判断表格是否可滚动。
<div style = 'width:900px;overflow:auto'>
<table>
<!-- Very large table here -->
</table>
</div>
滚动条出现在表格下方,但不幸的是表格太高了,除非向下滚动才能看到它。
即使表格离开屏幕,我也希望水平滚动条保持可见,可能固定在视口(viewport)底部。理想情况下,我想只使用 CSS 或最少量的 javascript 来完成。
最佳答案
这是 http://jsfiddle.net/TBnqw/2288/ 的脚本
$(function($){
var scrollbar = $('<div id="fixed-scrollbar"><div></div></div>').appendTo($(document.body));
scrollbar.hide().css({
overflowX:'auto',
position:'fixed',
width:'100%',
bottom:0
});
var fakecontent = scrollbar.find('div');
function top(e) {
return e.offset().top;
}
function bottom(e) {
return e.offset().top + e.height();
}
var active = $([]);
function find_active() {
scrollbar.show();
var active = $([]);
$('.fixed-scrollbar').each(function() {
if (top($(this)) < top(scrollbar) && bottom($(this)) > bottom(scrollbar)) {
fakecontent.width($(this).get(0).scrollWidth);
fakecontent.height(1);
active = $(this);
}
});
fit(active);
return active;
}
function fit(active) {
if (!active.length) return scrollbar.hide();
scrollbar.css({left: active.offset().left, width:active.width()});
fakecontent.width($(this).get(0).scrollWidth);
fakecontent.height(1);
delete lastScroll;
}
function onscroll(){
var oldactive = active;
active = find_active();
if (oldactive.not(active).length) {
oldactive.unbind('scroll', update);
}
if (active.not(oldactive).length) {
active.scroll(update);
}
update();
}
var lastScroll;
function scroll() {
if (!active.length) return;
if (scrollbar.scrollLeft() === lastScroll) return;
lastScroll = scrollbar.scrollLeft();
active.scrollLeft(lastScroll);
}
function update() {
if (!active.length) return;
if (active.scrollLeft() === lastScroll) return;
lastScroll = active.scrollLeft();
scrollbar.scrollLeft(lastScroll);
}
scrollbar.scroll(scroll);
onscroll();
$(window).scroll(onscroll);
$(window).resize(onscroll);
});
这是一个快速测试而不是一个完整的通用插件,但我认为这是一个好的开始
关于css - 表格底部的粘性滚动条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23206877/