我正在为我目前正在开发的网站构建水平和垂直滚动条。垂直的效果很好,没有问题,而且我还设法合并 jquery 来控制它的动画。问题在于水平滚动条。为了控制它,我需要一个重要的数据:div#news1 内容的总长度(在函数“mexer”的“comp”变量中使用)。但是浏览器只返回 clientHeight,它与它的容器 (div#cont1) 相同。我需要浏览器自动正确地生成它,因为我的网站将插入动态数据(PHP)。为什么会这样?我做错了什么?我该如何解决?
下面是测试文件的代码:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script src="JS/jquery-1.7.2.min.js"></script>
<style type="text/css">
.container {
width:250px;
height:250px;
overflow:hidden;
float:left;
border:3px solid #666;
}
.container1 {
width:400px;
height:125px;
overflow:hidden;
border:3px solid #666;
}
.botao {
border:3px solid #666;
background-color:#CCC;
padding:3px 3px 3px 3px;
color:#FFFFFF;
font-family:Arial, Helvetica, sans-serif;
cursor:pointer;
}
</style>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
</head>
<body>
<div class="container" id="cont">
<div id="news" style="position:relative">
<p><img src="a6.jpg" height="125" width="250" /></p>
<p><img src="a6.jpg" height="125" width="250" /></p>
<p><img src="a6.jpg" height="125" width="250" /></p>
<p><img src="a6.jpg" height="125" width="250" /></p>
<p><img src="a6.jpg" height="125" width="250" /></p>
<p><img src="a6.jpg" height="125" width="250" /></p>
</div>
</div>
<p><span id="botao1" onClick="mexe(1)" class="botao">Para cima</span></p>
<p><span id="botao2" onClick="mexe(2)" class="botao">Para baixo</span></p>
<div class="container1" id="cont1" style="float:left">
<div id="news1" style="position:relative">
<div style="float:left"><img src="fotosdojoao/1.png" height="125" width="250"></div>
<div style="float:left"><img src="fotosdojoao/2.png" height="125" width="250"></div>
<div style="float:left"><img src="fotosdojoao/3.png" height="125" width="250"></div>
<div style="float:left"><img src="fotosdojoao/4.png" height="125" width="250"></div>
</div>
</div>
<p><span id="botao3" onClick="mexer(1)" class="botao">Esquerda</span></p>
<p><span id="botao4" onClick="mexer(2)" class="botao">Direita</span></p>
<script type="text/javascript">
var topo;
var baixo;
var avanco;
var altura;
function mexe(direcao)
{
topo=Number(document.getElementById("news").style.top.substr(0,document.getElementById("news").style.top.length-2));
baixo=Number(document.getElementById("news").style.top.substr(0,document.getElementById("news").style.bottom.length-2));
avanco=Number(document.getElementById("cont").clientHeight);
altura=Number(document.getElementById("news").clientHeight)-avanco+30;
if (direcao==1 && topo<0)
{
if((topo+avanco)>=0)
{
topo=0;
}
else
{
topo=topo+avanco;
}
//document.getElementById("news").style.top=topo+"px";
}
if(direcao==2)
{
if((topo-avanco)*(-1)>=altura)
{
topo=altura*-1;
}
else
{
topo=topo-avanco;
}
//document.getElementById("news").style.top=topo+"px";
}
}
$(document).ready(function()
{
$("#botao1").click(function(){
$("#news").animate({top:topo+"px"},"normal","swing");
});
$("#botao2").click(function(){
$("#news").animate({top:topo+"px"},"normal","swing");
});
});
function mexer(direcao)
{
esq=Number(document.getElementById("news1").style.left.substr(0,document.getElementById("news1").style.left.length-2));
passagem=Number(document.getElementById("cont1").clientWidth);
comp=Number(document.getElementById("news1").style.width.substr(0,document.getElementById("news1").style.width.length-2));
maximo=comp-passagem+20;
if (direcao==1 && esq<0)
{
if((esq+passagem)>=0)
{
esq=0;
}
else
{
esq=esq+passagem;
}
document.getElementById("news1").style.left=esq+"px";
}
if(direcao==2)
{
if((esq-passagem)*(-1)>=maximo)
{
esq=maximo*-1;
}
else
{
esq=esq-passagem;
}
document.getElementById("news1").style.left=esq+"px";
}
}
</script>
</body>
</html>
提前致谢。
最好的祝福。