当前位置:首页 > 网页特效 > 游戏特效 >

JavaScript 跳跳球效果模拟

时间:2014-06-09 00:20 来源:互联网 作者:源码搜藏 收藏 推荐

运行代码 保存代码 复制代码 提示:您可以先修改部分代码再运行,保存代码功能在Firefox下无效。
  • 用JavaScript模拟的跳跳球效果,相当逼真,小球落后的时候总会弹球几次,如果碰到其它小球,会产生反弹效果,并且会响应你的鼠标移动,能感知你鼠标的力量,它会有所回应。
  • <script type="text/javascript"><!--
    o   = new Object();
    xm  = ym = 0;
    K   = 0;
    M   = 1;
    img = 0;
     
    document.onmousemove = function(e){
    if (window.event) e = window.event;
    xm = (e.x || e.clientX);
    ym = (e.y || e.clientY);
    M  = 0;
    }
    function resize() {
    nx = document.body.offsetWidth;
    ny = document.body.offsetHeight;
    }
    onresize = resize;
    obZ = function(N,img,imgS) {
    this.N  = N;
    this.W  = (N==0)?0:img.width/2;
    this.x  = xm/2;
    this.y  = -this.W;
    this.vx = 6;
    this.vy = 0;
    this.O = document.createElement("img");
    this.O.src = img.src;
    this.O.style.zIndex = 1000-this.W;
    document.getElementById("SF").appendChild(this.O);
    }
     
    function mainloop(){
    for(i=M;i<K;i++){
    with(o[i]){
    for(j=i+1;j<K;j++){
            dx = x-o[j].x;
            dy = y-o[j].y;
            d1 = Math.sqrt(dx*dx+dy*dy);
    d  = (W+o[j].W)-d1;
            if(d>0){
    ang = Math.atan2(dy, dx);
    dx = d * Math.cos(ang);
    dy = d * Math.sin(ang);
    if(W>o[j].W || i==0){
    o[j].x  -= dx;
            o[j].y  -= dy;
    o[j].vx -= dx;
            o[j].vy -= dy;
    } else {
    x  += dx;
    y  += dy;
    vx += dx;
    vy += dy;
    }
    }
    }
    vy+=W/100;
    y+=vy;
    if(y>ny-W)vy=-vy,y=ny-W;
    x+=vx;
    if(x<W)x=W,vx=-vx;
    else if(x>nx-W)x=nx-W,vx=-vx;
    vx*=.99;
    vy*=.99;
    if(N==0)x=xm,y=ym,vx=0,vy=0;
    else{
    O.style.left = x-W;
    O.style.top  = y-W;
    }
    }
    }
    M = 1;
    setTimeout(mainloop,16);
    }
    function creaTobZ(ID){
    o[K] = new obZ(K++,img[ID]);
    }
     
    onload = function() {
    img = document.getElementById("images").getElementsByTagName("img");
    resize();
    xm = nx / 2;
    ym = -1000;
    creaTobZ(0);
    setTimeout("creaTobZ(0)",500);
    for(var i=0;i<20;i++)setTimeout("creaTobZ("+(1+(i%(img.length-1)))+")",(i+10)*500);
    mainloop();
    }
    //-->
    </script>
由源码搜藏网整理,转载请注明出处https://www.codesocang.com/tx-youxi/8630.html
下一篇:没有了

游戏特效下载排行

最新文章