您的位置:首页 > Web前端 > HTML

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

2018-02-09 13:52 232 查看

一、滚动条一直显示

在< 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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  html nowrap