js实现椭圆运动轨迹

在网上看到一个flash实现椭圆运动轨迹,效果不错,试着用js实现,功能实现不是很完善,希望对学习js的朋友有所帮助。

查看演示

None.gif <! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
None.gif
None.gif
< html  xmlns ="http://www.w3.org/1999/xhtml"   >
None.gif
< head  runat ="server" >
None.gif    
< title ></ title >
ExpandedBlockStart.gifContractedBlock.gif    
< style  type ="text/css" > dot.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    .object
{dot.gif}{
InBlock.gif        position
:absolute; width:89px; height:89px
ExpandedSubBlockEnd.gif    
}

ExpandedBlockEnd.gif    
</ style >
None.gif
</ head >
None.gif
< body >
None.gif
</ body >
ExpandedBlockStart.gifContractedBlock.gif
< script  type ="text/javascript" > dot.gif
InBlock.gif        
function Move(x,y,a,b)//自定义移动类
ExpandedSubBlockStart.gifContractedSubBlock.gif
        dot.gif{
InBlock.gif            
this.uid="move_"+Math.random();
InBlock.gif            
this.uid = this.uid.replace(".","");
InBlock.gif            eval(
this.uid+"=this;");
InBlock.gif            
this._x=x;
InBlock.gif            
this._y=y;
InBlock.gif            
this._a=a;
InBlock.gif            
this._b=b;
InBlock.gif            
this.items=[];
InBlock.gif            
this.dxrad=0.05;
ExpandedSubBlockEnd.gif        }

InBlock.gif        Move.prototype.setPostion
=function(obj,rad)//设置对象位置
ExpandedSubBlockStart.gifContractedSubBlock.gif
        dot.gif{
InBlock.gif            obj.style.left
=this._a*Math.cos(rad)+this._x+"px";
InBlock.gif            obj.style.top
=this._b*Math.sin(rad)+this._y+"px";
InBlock.gif            
if(Math.sin(rad)>0) obj.style.zIndex=3
InBlock.gif            
else obj.style.zIndex=2;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        Move.prototype.setAlpha
=function(obj,rad)dot.gif{//设定透明度
InBlock.gif
            
InBlock.gif            
var p =(Math.sin(rad) < 0? 2*this._a-Math.abs(this._a*Math.cos(rad)) :this._a*Math.abs(Math.cos(rad)); 
InBlock.gif            p 
=100-40*p/this._a+20;
InBlock.gif            obj.style.filter
='alpha(opacity='+p+')';
InBlock.gif            obj.style.opacity
=p/100;
ExpandedSubBlockEnd.gif        }

InBlock.gif        Move.prototype.play
=function(n)//根据轨迹运动
ExpandedSubBlockStart.gifContractedSubBlock.gif
        dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
for(var i=0;i<this.items.length;i++)dot.gif{
InBlock.gif                
this.setPostion(this.items[i],2*Math.PI*i/n+this.dxrad);
InBlock.gif                
this.setAlpha(this.items[i],2*Math.PI*i/n+this.dxrad);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
this.dxrad=this.dxrad+0.03;
InBlock.gif            
if(this.dxrad>=Math.PI) this.dxrad=this.dxrad-Math.PI;
InBlock.gif           window.setTimeout(
this.uid+".play("+n+")",120);
ExpandedSubBlockEnd.gif        }

InBlock.gif        Move.prototype.init
=function(n)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
for(var i=1;i<=n;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
var img = new Image();
InBlock.gif                img.src 
= "https://p-blog.csdn.net/images/p_blog_csdn_net/ghtyan/object.jpg";
InBlock.gif                img.className
="object";
InBlock.gif                
this.items[i-1]=img;
InBlock.gif                
this.setPostion(img,2*Math.PI*i/n);
InBlock.gif                
this.setAlpha(img,2*Math.PI*i/n);
InBlock.gif                document.body.appendChild(img);
InBlock.gif                
//img.οnclick=function(){alert(this.style.zIndex)};
ExpandedSubBlockEnd.gif
            }

InBlock.gif            
this.play(n);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
var move = new Move(450,300,300,80);
InBlock.gif        move.init(
8);
ExpandedBlockEnd.gif    
</ script >
None.gif
</ html >
None.gif

转载于:https://www.cnblogs.com/ghtyan/archive/2007/08/13/js.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值