Javascript 水平自定义滚动条-不工作-divnews1总宽度未由浏览器设置
我正在为我目前正在开发的网站构建一个水平和垂直滚动条。垂直的一个工作得很好,没有任何问题,我还设法合并了jquery来控制它的动画。 问题在于水平滚动条。为了控制它,我需要一个重要的数据:要在函数mexer的comp变量中使用的divnews1内容的总长度。但是,浏览器只返回clientHeight,它与容器divcont1相同。我需要这是自动生成的,适当的浏览器,因为我的网站将有动态数据插入其中。 为什么会发生这种情况?我做错了什么?我怎样才能修好它 以下是测试文件的代码:Javascript 水平自定义滚动条-不工作-divnews1总宽度未由浏览器设置,javascript,jquery,javascript-events,Javascript,Jquery,Javascript Events,我正在为我目前正在开发的网站构建一个水平和垂直滚动条。垂直的一个工作得很好,没有任何问题,我还设法合并了jquery来控制它的动画。 问题在于水平滚动条。为了控制它,我需要一个重要的数据:要在函数mexer的comp变量中使用的divnews1内容的总长度。但是,浏览器只返回clientHeight,它与容器divcont1相同。我需要这是自动生成的,适当的浏览器,因为我的网站将有动态数据插入其中。 为什么会发生这种情况?我做错了什么?我怎样才能修好它 以下是测试文件的代码: <!DOCT
<!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>
谢谢,这是事先准备好的
致以最诚挚的问候。不确定这是您想要的,但要设置/获得宽度,您可以这样做:
//get width of an item
var itemWidth = $('#news1 > div:first').width();
//count number of items
var itemsCount = $('#news1 > div').length;
//width * count = width
var containerWidth = itemWidth * itemsCount;
alert(containerWidth);
你可以直接设置它
$('#news1').width(containerWidth);
或者在进行滚动时使用它
因为它不是用英语写的,所以我很难理解它是如何工作的:/因为它的元素很自然会掉落,所以获取高度从来都不是问题,但是宽度要复杂得多。你需要找出构成总宽度的元素的宽度和数量。了解使其工作的方法。但是你,或者任何人知道为什么会这样吗?