HTML中table带滚动条而标题栏不移动的做法

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HTML中table带滚动条而标题栏不移动的做法相关的知识,希望对你有一定的参考价值。

看到有人说可以用一个TABLE里面套两个TABLE来做,其中一个只作为标题栏,另一个嵌套在有滚动条的DIV里面.但是这样一来,标题栏和数据部分因为宽度不一样,对不上了.而我做的这个TABLE,一行有多少列数据是不固定的,而且每列显示什么项目也不固定,每个<TH>或者<TD>的宽度是无法固定的.不知道哪位大仙能帮忙想给别的办法呢?100分,全都送上,谢了.
在线等.

把代码保存为html格式, 上传到网页空间看看是不是你想要的!
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset= gb2312">
<style type="text/css">
<!--
a color: #00488C
a:hover color: #FF0000
td font-size: 12px ;color: #00488C
-->
</style>
<script language=javascript>
<!--
// ------ 定义全局变量
var theNewsNum;
var theAddNum;
var totalNum;
var CurrentPosion=0;
var theCurrentNews;
var theCurrentLength;
var theNewsText;
var theTargetLink;
var theCharacterTimeout;
var theNewsTimeout;
var theBrowserVersion;
var theWidgetOne;
var theWidgetTwo;
var theSpaceFiller;
var theLeadString;
var theNewsState;
function startTicker()

// ------ 设置初始数值
theCharacterTimeout = 50;//字符间隔时间
theNewsTimeout = 2000;//新闻间隔时间
theWidgetOne = "_";//新闻前面下标符1
theWidgetTwo = "-";//新闻前面下标符
theNewsState = 1;
//theNewsNum = document.body.children.incoming.children.NewsNum.innerText;//新闻总条数
//add by lin
theNewsNum = document.body.children.incoming.children.AllNews.children.length;//新闻总条数
theAddNum = document.body.children.incoming.children.AddNews.children.length;//补充条数
totalNum =theNewsNum+theAddNum;
theCurrentNews = 0;
theCurrentLength = 0;
theLeadString = " ";
theSpaceFiller = " ";
runTheTicker();

// --- 基础函数
function runTheTicker()

if(theNewsState == 1)

if(CurrentPosion<theNewsNum)
setupNextNews();

else
setupAddNews();

CurrentPosion++;
if(CurrentPosion>=totalNum||CurrentPosion>=5) CurrentPosion=0; //最多条数不超过5条

if(theCurrentLength != theNewsText.length)

drawNews();

else

closeOutNews();


// --- 跳转下一条新闻
function setupNextNews()

theNewsState = 0;
theCurrentNews = theCurrentNews % theNewsNum;
theNewsText = document.body.children.incoming.children.AllNews.children[theCurrentNews].children.Summary.innerText;
theTargetLink = document.body.children.incoming.children.AllNews.children[theCurrentNews].children.NewsLink.innerText;
theCurrentLength = 0;
document.all.hottext.href = theTargetLink;
theCurrentNews++;

function setupAddNews()

theNewsState = 0;
theCurrentNews = theCurrentNews % theAddNum;
theNewsText = document.body.children.incoming.children.AddNews.children[theCurrentNews].children.Summary.innerText;
theTargetLink = document.body.children.incoming.children.AddNews.children[theCurrentNews].children.NewsLink.innerText;
theCurrentLength = 0;
document.all.hottext.href = theTargetLink;
theCurrentNews++;

// --- 滚动新闻
function drawNews()

var myWidget;
if((theCurrentLength % 2) == 1)

myWidget = theWidgetOne;

else

myWidget = theWidgetTwo;

document.all.hottext.innerHTML = theLeadString + theNewsText.substring(0,theCurrentLength) + myWidget + theSpaceFiller;
theCurrentLength++;
setTimeout("runTheTicker()", theCharacterTimeout);

// --- 结束新闻循环
function closeOutNews()

document.all.hottext.innerHTML = theLeadString + theNewsText + theSpaceFiller;
theNewsState = 1;
setTimeout("runTheTicker()", theNewsTimeout);

window.onload=startTicker;
//-->
</script>
</head>
<body>

<script type="text/javascript">
var vjAcc="860010-0120120000";
vjTrack();
</script>
<noscript>
<img src="http://cctv.doulog.com/a.gif?vjAcc=860010-0120120000" width="1" height="1" />
</noscript>
<table>
<tr><td>
<div id=visible>你的文字说明:<a href="" id=hottext target="_blank"></a></div>
</td></tr>
</table>
<div id=incoming style="DISPLAY: none">
<div id=AllNews>
参考技术A <html>

<body>

<table width="100%" align="center" border=0>

<tr>

<table width="100%" border=1>

<tr>

<td width="50%">

biaoti1321

</td>

<td width="50%">

biaoti2321321

</td>

<td>

 

</td>

</tr>

</table>

</tr>

<tr>

<td align="center" width="100%">

<div style="overflow-y:auto; width:100%;height:100px">

<table width="100%" border=1>

<tr>

<td>111</td>

<td>111</td>

</tr>

<tr>

<td>111</td>

<td>111</td>

</tr>

<tr>

<td>111</td>

<td>111</td>

</tr>

<tr>

<td>111</td>

<td>111</td>

</tr>

<tr>

<td>111</td>

<td>111</td>

</tr>

<tr>

<td>111</td>

<td>111</td>

</tr>

<tr>

<td>111</td>

<td>111</td>

</tr>

<tr>

<td>111</td>

<td>111</td>

</tr>

<tr>

<td>111</td>

<td>111</td>

</tr>

</table>

</div>

</td>

</tr>

</table>

</body>

</html>

具体思路这个是一个大table,然后表头与结果域各一个table,

表头的标题是N个,那么就需要分N+1个td,前面N个TD的width总和为100%,最后1个为了给滚动条位置,不然不好与下面table对齐。

下面的结果体table需要包含在div中,设置td的长度与上面td长度要保持一致,这样就可以了。
参考技术B 在标题的tr中添加如下样式
style="position: relative; top: expression(this.offsetParent.scrollTop);"

以上是关于HTML中table带滚动条而标题栏不移动的做法的主要内容,如果未能解决你的问题,请参考以下文章

网页设计如何设置网页部分内容不随着滚动条而移动移动?

jquery怎么实现平滑滚动效果 就是随着滚动条而滚动

如何在 Firefox 中隐藏滚动条而不停止在 div 中滚动

如何永久显示 UIScrollView 滚动条而不仅仅是闪烁?

如何在DataGrid里面产生滚动条而不滚动题头

html里table表头随滚动条移动,表头只有一行,有横竖滚动条,而且横滚动条拖动的时候,表头也要随之移动