现在是计算的时候不会到-500;
它会跳过-500;所以会一直再滚动着..
有什么方法可以计算到整数的?
貌似有Math里的方法可以吗?<!DOCTYPE html>
<html>
<head><meta http-equiv="Content-Type" content="text/html; charset=utf8">
<title>无标题文档</title><style type="text/css">
*{margin:0;padding:0;}
 ul,li{list-style:none}
.red{ background:red;}
.blue{ background:blue}
.wrap{width:500px;height:300px;overflow:hidden;position:relative;margin:55px auto}
.inner{position:absolute;left:0;top:0;}
.inner li{float:left;width:500px;height:300px;color:#fff}
.orange{ background:orange}
.nav{position:absolute;bottom:5px;right:5px;}
.nav li{float:left;padding:1px 4px;background:orange;border:1px solid #000;margin-right:4px}
.nav .nav_cur{background:#333;color:#fff;}
#box{ position:absolute;top:50px;left:0;z-index:88}
</style><!--<script type="text/javascript" src="jquery-1.6.min.js">
</script>--></head><body>
<div class="box" id="box">sd</div>
<div class="wrap">
<div class="inner" id="inner">
<ul>
<li class="red">1</li>
<li class="blue">2</li>
<li class="red">3</li>
</ul>
</div>

<div class="nav">
<li class="nav_cur">1</li>
<li>2</li>
</div></div><script type="text/javascript">
var timer;
var u=0;

function $(id){
return document.getElementById(id)
}
var i=0;
var cur;
var arr = [0,-500,-1000];
function aa(){
if($("inner").offsetLeft != arr[i+1]){
u +=   parseInt((arr[i+1] + $("inner").offsetLeft)/20);
console.log(u)
timer = setTimeout(function(){aa()},10)
}
else{
i++;
if(i == 3){
clearInterval(cur)
}
clearTimeout(timer)
}
$("inner").style.left = u+"px";
}

cur = setInterval(function(){
aa()
},2000)

</script>
</body>
</html>

解决方案 »

  1.   

    LOG: (-500 + 0)/20 += -25 
    LOG: left:-25px 
    LOG: (-500 + -25)/20 += -51 
    LOG: left:-51px 
    LOG: (-500 + -51)/20 += -78 
    LOG: left:-78px 
    LOG: (-500 + -78)/20 += -106 
    LOG: left:-106px 
    LOG: (-500 + -106)/20 += -136 
    LOG: left:-136px 
    LOG: (-500 + -136)/20 += -167 
    LOG: left:-167px 
    LOG: (-500 + -167)/20 += -200 
    LOG: left:-200px 
    LOG: (-500 + -200)/20 += -235 
    LOG: left:-235px 
    LOG: (-500 + -235)/20 += -271 
    LOG: left:-271px 
    LOG: (-500 + -271)/20 += -309 
    LOG: left:-309px 
    LOG: (-500 + -309)/20 += -349 
    LOG: left:-349px 
    LOG: (-500 + -349)/20 += -391 
    LOG: left:-391px 
    LOG: (-500 + -391)/20 += -435 
    LOG: left:-435px 
    LOG: (-500 + -435)/20 += -481 
    LOG: left:-481px 
    LOG: (-500 + -481)/20 += -530 
    LOG: left:-530px 
      

  2.   

    ????console.log(u)
      可以直接用吗?
    我复制你的代码怎么用不了
      

  3.   

    没看你具体程序。只看开始两行的描述,就想起我开始学编程时是对动画编程感兴趣,作了大量各种练习,其中常常碰到这样的情况(不知和你的是不是一样的),主要原因是由于实际的具体计算得到的值和你设立的控制值不一定相同,因此,如果用“相等”来作为控制条件的话,往往就不能如愿。所以应该把“IF 等于或不等于”变成“IF 大于N小于M”这样的控制条件。
      

  4.   

    <script type="text/javascript">document.write(Math.round(0.60) + "<br />")
    document.write(Math.round(0.50) + "<br />")
    document.write(Math.round(0.49) + "<br />")
    document.write(Math.round(-4.40) + "<br />")
    document.write(Math.round(-4.60))</script>注意了:round() 方法可把一个数字舍入为最接近的整数。
      

  5.   

     if($("inner").offsetLeft > arr[i+1]){ 我改成这样也不行
      

  6.   

    可以考虑if(-2<$("inner").offsetLeft/arr[i+1]<-1)