实现效果:
代码:
$(".fixed-table-body").ready(function () {
document.addEventListener('scroll', function (event) {
if ($(".fixed-table-body").scrollTop() <= 0) {
console.log("滚动条已经到达顶部为0");
}
console.log("滚动条距离顶部:" + $(document).scrollTop());
if ($(".fixed-table-body").scrollTop() >= $(document).height() - $(window).height()) {
console.log("滚动条已经到达底部为" + $(".fixed-table-body").scrollTop());
}
if ($(".fixed-table-body").scrollLeft() <= 0) {
console.log("滚动条已经到达左边为0");
}
console.log("滚动条距离左边:" + $(".fixed-table-body").scrollLeft());
if ($(".fixed-table-body").scrollLeft() >= 433) {
var tr_6 = $('#bootstrap-table').children("thead").children("tr").children("th").eq(5);
var tbody_tr_6 = $('#bootstrap-table').children("tbody").children("tr");
//表头载体名称:固定在左侧
tr_6[0].style.cssText = 'position:absolute;left:0px;top:0px;'
$('#bootstrap-table').children("thead").children("tr").children("th").eq(5).children().eq(0)[0].style.cssText = 'background: #eff3f8;width:240px'
//载体名称内容:固定在左侧
for (var i = 0; i < tbody_tr_6.length; i++) {
if (i % 2 != 0) {
$('#bootstrap-table').children("tbody").children("tr").eq(i).children("td").eq(5)[0].style.cssText = 'position:absolute;left:0px;width:240px;background: #d5fff6!important;'
} else {
$('#bootstrap-table').children("tbody").children("tr").eq(i).children("td").eq(5)[0].style.cssText = 'position:absolute;left:0px;width:240px;background: white!important;'
}
}
} else {
var tr_6 = $('#bootstrap-table').children("thead").children("tr").children("th").eq(5);
var tbody_tr_6 = $('#bootstrap-table').children("tbody").children("tr");
//表头载体名称位置还原
tr_6[0].style.cssText = 'position:static;'
$('#bootstrap-table').children("thead").children("tr").children("th").eq(5).children().eq(0)[0].style.cssText = 'background: ""'
//载体名称内容:位置还原
for (var i = 0; i < tbody_tr_6.length; i++) {
$('#bootstrap-table').children("tbody").children("tr").eq(i).children("td").eq(5)[0].style.cssText = 'position:static;'
}
}
if ($(".fixed-table-body").scrollLeft() >= $(document).width() - $(window).width()) {
console.log("滚动条已经到达最左侧" + $(".fixed-table-body").scrollLeft());
}
}, true);
})
这里要注意的是:.fixed-table-body这个类,如果是用$(“document”).ready(function ()是无法获取到横向滚动条的