javascript Math.floor()与Math.ceil()的问题.
postion移动坐标的例子,坐标0移动到-200要用Math.ceil()函数,之后顺利到-200位置..可是,当-200移动到-400时一定得用Math.floor(...
postion移动坐标的例子,
坐标0移动到-200要用Math.ceil()函数,之后顺利到-200位置..
可是,当-200移动到-400时一定得用Math.floor()函数,才能顺利到400整?
0到-200 与 -200到-400:
dist变量计算的都是,当前坐标与目标坐标的距离。
0到-200:
dist 20 18 16.2。。。
-200到-400
dist -20 -18 -16.2。。。
<style type="text/css">
#place
{
width:300px;
height:200px;
overflow:hidden;
position:relative;
}
</style>
<script type="text/javascript">
window.onload=prepareRoll("preview");
function prepareRoll(eid)
{
var elem = document.getElementById(eid);
elem.style.position = "absolute";
elem.style.left = "0px";
elem.style.top = "0px";
setTimeout("move1('"+eid+"')",1000);
}
function move1(eid)
{
var elem = document.getElementById(eid);
var ypos = parseInt(elem.style.top);
if(ypos>-200)
{
dist = Math.ceil((ypos-(-200))/10);
ypos = ypos - dist;
}
else
{
alert(ypos)
//-200
setTimeout("move2('"+eid+"')",1000);
return false;
}
elem.style.top = ypos+"px";
setTimeout("move1('"+eid+"')",10);
}
function move2(eid)
{
var elem = document.getElementById(eid);
var ypos = parseInt(elem.style.top)
if(ypos>-400)
{
dist = Math.floor(((-400)-ypos)/10);
ypos = ypos + dist;
}
else
{
alert(ypos)
//-400
setTimeout("move3('"+eid+"')",1000);
return false;
}
elem.style.top = ypos+"px";
setTimeout("move2('"+eid+"')",10);
}
function move3(eid)
{
var elem = document.getElementById(eid);
var ypos = parseInt(elem.style.top)
if(ypos<0)
{
dist = Math.ceil((0-ypos)/10);
ypos = ypos + dist;
}
else
{
alert(ypos)
//0
setTimeout("move1('"+eid+"')",1000);
return false;
}
elem.style.top = ypos+"px";
setTimeout("move3('"+eid+"')",10);
}
</script>
</head>
<body style="background-color:#000">
<div id="place">
<img src="../images/scroll.jpg" id="preview" />
</div>
</body> 展开
坐标0移动到-200要用Math.ceil()函数,之后顺利到-200位置..
可是,当-200移动到-400时一定得用Math.floor()函数,才能顺利到400整?
0到-200 与 -200到-400:
dist变量计算的都是,当前坐标与目标坐标的距离。
0到-200:
dist 20 18 16.2。。。
-200到-400
dist -20 -18 -16.2。。。
<style type="text/css">
#place
{
width:300px;
height:200px;
overflow:hidden;
position:relative;
}
</style>
<script type="text/javascript">
window.onload=prepareRoll("preview");
function prepareRoll(eid)
{
var elem = document.getElementById(eid);
elem.style.position = "absolute";
elem.style.left = "0px";
elem.style.top = "0px";
setTimeout("move1('"+eid+"')",1000);
}
function move1(eid)
{
var elem = document.getElementById(eid);
var ypos = parseInt(elem.style.top);
if(ypos>-200)
{
dist = Math.ceil((ypos-(-200))/10);
ypos = ypos - dist;
}
else
{
alert(ypos)
//-200
setTimeout("move2('"+eid+"')",1000);
return false;
}
elem.style.top = ypos+"px";
setTimeout("move1('"+eid+"')",10);
}
function move2(eid)
{
var elem = document.getElementById(eid);
var ypos = parseInt(elem.style.top)
if(ypos>-400)
{
dist = Math.floor(((-400)-ypos)/10);
ypos = ypos + dist;
}
else
{
alert(ypos)
//-400
setTimeout("move3('"+eid+"')",1000);
return false;
}
elem.style.top = ypos+"px";
setTimeout("move2('"+eid+"')",10);
}
function move3(eid)
{
var elem = document.getElementById(eid);
var ypos = parseInt(elem.style.top)
if(ypos<0)
{
dist = Math.ceil((0-ypos)/10);
ypos = ypos + dist;
}
else
{
alert(ypos)
//0
setTimeout("move1('"+eid+"')",1000);
return false;
}
elem.style.top = ypos+"px";
setTimeout("move3('"+eid+"')",10);
}
</script>
</head>
<body style="background-color:#000">
<div id="place">
<img src="../images/scroll.jpg" id="preview" />
</div>
</body> 展开
1个回答
展开全部
使用Math.ceil()函数 会陷入死循环
function move2(eid)
{
var elem = document.getElementById(eid);
var ypos = parseInt(elem.style.top)
if(ypos>-400)
{
dist = Math.floor(((-400)-ypos)/10);
ypos = ypos + dist;
}
else
{
alert(ypos)
//-400
setTimeout("move3('"+eid+"')",1000);
return false;
}
elem.style.top = ypos+"px";
setTimeout("move2('"+eid+"')",10);
}
dist = Math.floor(((-400)-ypos)/10);这一句,当ypos>=391时,dist=0;
函数执行无数次,一样ypos 不会增加,dist始终等于0.
用floor 就不会存在这个问题。
function move2(eid)
{
var elem = document.getElementById(eid);
var ypos = parseInt(elem.style.top)
if(ypos>-400)
{
dist = Math.floor(((-400)-ypos)/10);
ypos = ypos + dist;
}
else
{
alert(ypos)
//-400
setTimeout("move3('"+eid+"')",1000);
return false;
}
elem.style.top = ypos+"px";
setTimeout("move2('"+eid+"')",10);
}
dist = Math.floor(((-400)-ypos)/10);这一句,当ypos>=391时,dist=0;
函数执行无数次,一样ypos 不会增加,dist始终等于0.
用floor 就不会存在这个问题。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询