bootstrap 的模态框中根据滚动高度距离冻结(固定)表头

本文介绍了一个使用jQuery实现的模态框中表格滚动时固定表头的解决方案。通过监听滚动事件,判断滚动位置来显示或隐藏固定表头,确保数据清晰可见。

html部分

<div class="modal inmodal" id="alarmModal" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog" style="width: 80%">
        <div class="modal-content animated bounceInRight">
            <div class="modal-header">
                <div  class="close" data-dismiss="modal">
                    <img src="/static/manager/img/new/close.png" alt="">
                </div>
                <h4 class="modal-title">详情</h4>
            </div>
            <div class="modal-body" style="height: 400px;overflow: auto;">
                <table class="table table-bordered historyInfoList" id="mytable" style="display: none">
                    <thead>
                    <tr>
                        <th class="col-sm-1">通电电位(V)</th>
                        <th class="col-sm-1">断电电位(V)</th>
                        <th class="col-sm-1">交流电压(V)</th>
                        <th class="col-sm-1">直流电压(V)</th>
                        <th class="col-sm-1">直流电流(mA)</th>
                        <th class="col-sm-1">自然电位(V)</th>
                        <th class="col-sm-1">电池电压(V)</th>
                        <th class="col-sm-2">上报时间</th>
                    </tr>
                    </thead>
                </table>
                <table class="table table-bordered historyInfoList">
                    <thead id="datatable">
                    <tr>
                        <th class="col-sm-1">通电电位(V)</th>
                        <th class="col-sm-1">断电电位(V)</th>
                        <th class="col-sm-1">交流电压(V)</th>
                        <th class="col-sm-1">直流电压(V)</th>
                        <th class="col-sm-1">直流电流(mA)</th>
                        <th class="col-sm-1">自然电位(V)</th>
                        <th class="col-sm-1">电池电压(V)</th>
                        <th class="col-sm-2">上报时间</th>
                    </tr>
                    </thead>
                    <tbody>
                    </tbody>
                </table>



            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-white" data-dismiss="modal">关闭</button>
            </div>
        </div>
    </div>
</div>

注:重点在表格部分

jq部分:

<script type="text/javascript">

    $(function(){

        //根据滚动窗体和父窗体top距离进行判断是否冻结表头
        $(".modal-body").scroll(function(event){

            let top = $("#datatable").position().top;
            if(top<=0){
                $("#mytable").css({"display": "","position": "fixed","top":"65px","width": "calc(100% - 65px)"});
            }else{
                $("#mytable").css({"display": "none"});
            }
        });
            });
</script>
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yijiliangfang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值