table表头不换行并允许横向滚动

本文介绍两种在HTML表格中实现滚动条的方法:一种是始终保持滚动条可见,并阻止内容自动换行;另一种则是初始状态下隐藏滚动条,仅在鼠标悬停时显示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、滚动条一直显示

在< td>中加入nowrap=”nowrap”即可实现内容不换行的效果;滚动条效果需要通过设置overflow来实现。

<div id="test" style="width: 800px; height: 80px; overflow-x: auto;overflow-y: hidden">
    <table>
        <tr>
            <th nowrap="nowrap" style="padding-left:10px;padding-right:30px;">车辆的呼号</th>
            <th nowrap="nowrap" style="padding-right:10px;">值班中队长</th>
            <th nowrap="nowrap" style="padding-right:10px;">白天值班的老哥</th>
            <th nowrap="nowrap" style="padding-right:10px;">晚上值班的老哥</th>
            <th nowrap="nowrap" style="padding-right:30px;">排班时间</th>
            <th nowrap="nowrap" style="padding-right:10px;">状态</th>
            <th nowrap="nowrap" style="padding-right:30px;">车辆的呼号</th>
            <th nowrap="nowrap" style="padding-right:10px;">值班中队长</th>
            <th nowrap="nowrap" style="padding-right:10px;">白天值班的老哥</th>
            <th nowrap="nowrap" style="padding-right:10px;">晚上值班的老哥</th>
            <th nowrap="nowrap" style="padding-right:30px;">排班时间</th>
            <th nowrap="nowrap" style="padding-right:10px;">状态</th>
        </tr>
        <tr>
            <td style="padding-left:10px;">item.callsign</td>
            <td> item.dutyoffice </td>
            <td> item.daylaoge </td>
            <td> item.nightlaoge </td>
            <td> item.date </td>
            <td> item.status </td>
            <td> item.callsign </td>
            <td> item.dutyoffice </td>
            <td> item.daylaoge </td>
            <td> item.nightlaoge </td>
            <td> item.date </td>
            <td> item.status </td>
        </tr>
    </table>;
</div>

对< td>设置padding属性时,根据不同的框架和浏览器,padding有时会不起效果,对于这种情况我们可以在< td>中加个< div>标签,代码如下:

<td><div style='display:inline-block;width:10px'></div> item.dutyDate </td>

二、滚动条先隐藏鼠标移过才显示

有时候为了美观,要先把滚动条隐藏起来,等鼠标移到元素上之后再显示滚动条,鼠标离开滚动条就恢复隐藏状态。

<script type="text/javascript">
    $(document).ready(function(){
        $("#dutyControl").hover(function () {
            //$("#dutyControl").css("overflow-x", "auto");
            $("#dutyControl").css("overflow", "auto");
        }, function () {
            //$("#dutyControl").css("overflow-x", "hidden");
            $("#dutyControl").css("overflow", "hidden");
        });
    });
</script>
<div id="dutyControl" style="width: 800px; height: 80px; overflow-x: hidden;overflow-y: hidden">
    <table>
        <tr>
            <th nowrap="nowrap" style="padding-left:10px;padding-right:30px;">车辆的呼号</th>
            <th nowrap="nowrap" style="padding-right:10px;">值班中队长</th>
            <th nowrap="nowrap" style="padding-right:10px;">白班的辅警</th>
            <th nowrap="nowrap" style="padding-right:10px;">夜班的辅警</th>
            <th nowrap="nowrap" style="padding-right:30px;">排班时间</th>
            <th nowrap="nowrap" style="padding-right:10px;">状态</th>
            <th nowrap="nowrap" style="padding-right:30px;">车辆的呼号</th>
            <th nowrap="nowrap" style="padding-right:10px;">值班中队长</th>
            <th nowrap="nowrap" style="padding-right:10px;">白班的辅警</th>
            <th nowrap="nowrap" style="padding-right:10px;">夜班的辅警</th>
            <th nowrap="nowrap" style="padding-right:30px;">排班时间</th>
            <th nowrap="nowrap" style="padding-right:10px;">状态</th>
        </tr>
        <tr>
            <td style="padding-left:10px;">item.callsign</td>
            <td> item.dutyoffice </td>
            <td> item.daystaff </td>
            <td> item.nightstaff </td>
            <td> item.date </td>
            <td> item.status </td>
            <td> item.callsign </td>
            <td> item.dutyoffice </td>
            <td> item.daystaff </td>
            <td> item.nightstaff </td>
            <td> item.date </td>
            <td> item.status </td>
        </tr>
    </table>
</div>
当使用Ant Design Vue库中的`a-table`组件,数据为空时,如果你想在表头区域展示横向滚动条,可以设置一些特定的CSS样式。默认情况下,`a-table`会隐藏空数据时的滚动条。你可以通过以下几个步骤实现: 1. 首先,在`<template>`里添加一个占位的`no-data`或者自定义内容,以便在无数据显示: ```html <template> <div> <a-table :data="tableData" v-if="tableData.length > 0"></a-table> <div v-else class="empty-table"> <!-- 没有数据显示的内容 --> </div> </div> </template> ``` 2. 接着,给这个占位容器添加一个高度和滚动属性,比如宽度设为100%,设置滚动条样式: ```css .empty-table { display: flex; height: 400px; /* 可根据实际需要调整高度 */ overflow-x: auto; /* 添加横向滚动 */ white-space: nowrap; /* 保持文本不换,让文字水平排列 */ } ``` 3. 如果你想只在没有数据时才显示滚动条,可以在Vue实例中添加状态管理,例如`tableDataLength`,在模板上根据这个状态控制滚动条显示: ```javascript export default { data() { return { tableData: [], // 数据数组 tableDataLength: 0, // 数据长度 }; }, computed: { showScrollbar() { return this.tableDataLength === 0; } }, }; ``` 然后在CSS中根据`showScrollbar`来切换滚动条的样式: ```css .empty-table[v-show="!showScrollbar"] { overflow-x: visible; scrollbar-width: none; /* 或者其他隐藏滚动条的方式 */ } .empty-table[v-show="!showScrollbar"]::-webkit-scrollbar { /* WebKit内核(如Chrome、Safari) */ width: 0; } .empty-table[v-show="!showScrollbar"]::-webkit-scrollbar-thumb { /* 红色是为了示例 */ background-color: red; border-radius: 5px; } /* 其他浏览器可能需要类似处理 */ .empty-table[v-show="!showScrollbar"]::-moz-scrollbar { /* Firefox */ width: 0; } .empty-table[v-show="!showScrollbar"]::-ms-scrollbar { /* Edge */ width: 0; } ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

changuncle

若恰好帮到您,请随心打赏

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

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

打赏作者

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

抵扣说明:

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

余额充值