监听bootstrap框架table的横向滚动条与纵向滚动条,并将table的某一整列固定随滚动规定在左侧

实现效果:
在这里插入图片描述
在这里插入图片描述代码:

$(".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 ()是无法获取到横向滚动条的

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值