网上找到如下代码,从if(!d) return;这句开始就不懂啦,所以贴过来请教一下!!window.onload=function(){
  var ele=document.getElementById("description");
  var w=ele.clientWidth;
  var n=20,t=20;
  var timers=new Array(n);
  var c=document.getElementById("beni").getElementsByTagName("li");
  for(var i=0;i<c.length;i++){
    c[i].index=i;
    c[i].onmouseover=doSlide;
  }
  c=null;
  function doSlide(){
    var x=ele.scrollLeft;
    var d=this.index*w-x;
    if(!d) return;
    for(var i=0;i<n;i++)(function(){
      if(timers[i])
        clearTimeout(timers[i]);
      var j=i;
      timers[i]=setTimeout(function(){
        ele.scrollLeft=x+Math.round(d*Math.sin(Math.PI*(j+1)/(2*n)));
      },(i+1)*t);
    })();
  }
}

解决方案 »

  1.   


    本代码从该网址找到:http://bbs.blueidea.com/thread-2771731-1-1.html
      

  2.   

    if(!d) return;for(var i=0;i<n;i++)(function(){
          if(timers[i])
            clearTimeout(timers[i]);
          var j=i;
          timers[i]=setTimeout(function(){
            ele.scrollLeft=x+Math.round(d*Math.sin(Math.PI*(j+1)/(2*n)));
          },(i+1)*t);
        })();
      }
    -----------------------------------------------------------------------
    循环加上闭包函数,闭包函数里面又定义执行的函数
      

  3.   

    scrollLeft:设置或获取位于对象左边界和窗口中目前可见内容的最左端之间的距离
    clientWidth = width + padding
    if(!d) return;  //若d=0则直接返回,表示并没有改变窗口
    if(timers[i])
        clearTimeout(timers[i]);
    clearTimeout是指script取消某setTimeout的设定
    如果前面已经对其中某个li设定过,则取消进行下面重新设定执行
    设定之后获取位于对象左边界和窗口中目前可见内容的最左端之间的距离,从而移动到需要的画面
    ele.scrollLeft=x+Math.round(d*Math.sin(Math.PI*(j+1)/(2*n)));
      

  4.   

    怎么就看不懂了? js判断语句中, 0, null, undefined 都被转换成false.所以,请看下面测试:
    alert(0==false);
    if(!null)alert(1);
    if(!undefined)aler(2);