页面物体移动

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
  <TITLE> New Document </TITLE>
  <META NAME="Generator" CONTENT="EditPlus">
  <META NAME="Author" CONTENT="">
  <META NAME="Keywords" CONTENT="">
  <META NAME="Description" CONTENT="">
 </HEAD>
<style type="text/css">
 
</style>
 <BODY>
  
     <div style="position: absolute;left: 200px;top: 200px;border: 1px solid red ;width: 500px;height: 500px" id="kuang">
     	
     	 <div style="position: absolute;width: 50px;height: 50px;border: 1px solid black;left: 10px;top: 10px;" id="sutff"></div>
     </div>
 </BODY>
 <script type="text/javascript" src="/js/jquery-1.4.2.js"></script>
 <script type="text/javascript">

 //定义 运动的对象 的 类
	function stuff(position, direction,width,height){
		 //方向 
		 this.direction = direction;
		 this.position = position;
		 this.width = width;
		 this.height = height ;
	}

	stuff.prototype.setPosition = function (position){
		this.position = position;
	}
	stuff.prototype.setDirection = function (direction){
		this.direction = direction;
	}
	stuff.prototype.getPosition = function ( ){
		return this.position  ; 
	}		
	stuff.prototype.getDirection = function ( ){
		return this.direction  ;
	}
	stuff.prototype.getHeight = function ( ){
		return this.height  ;
	}
	stuff.prototype.setHeight = function ( height ){
		 this.height=height  ;
	}

	
	stuff.prototype.move = function (){
		switch( this.direction )
		{
			//左上
			case 1 :
				this.position.x = this.position.x - 2;
				this.position.y = this.position.y - 2;
				break;

			//左下
			case 2 :
				this.position.x = this.position.x - 2;
				this.position.y = this.position.y + 2;
				break;
			//右下
			case 3 :
				this.position.x = this.position.x + 2;
				this.position.y = this.position.y + 2;
				break;
			//右上
			case 4 :
				this.position.x = this.position.x + 2;
				this.position.y = this.position.y - 2;
				break ; 
/*
			case 5 :
				this.position.x = this.position.x + 2;
				this.position.y = this.position.y - 2;
				break;
			case 6 :
				this.position.x = this.position.x + 2;
				this.position.y = this.position.y + 2;
				break;
			case 7 :
				this.position.x = this.position.x - 2;
				this.position.y = this.position.y + 2;
				break;
			case 8 :
				this.position.x = this.position.x - 2;
				this.position.y = this.position.y - 2;
				break ; 
				 */
		}

	}

	//定义位置 类  
	function position( x ,y   ){
		this.x = x;
		this.y = y ;
	}
	position.prototype.setX = function (x ){
		this.x = x;
	}
	position.prototype.gettX = function (){
		return this.x;
	}
	position.prototype.setY = function (y ){
		this.y = y;
	}
	position.prototype.getY = function (){
		return this.y;
	}	
	
	

	 //创建运动对象
 	var position = new position( 450 , 30 );
 	//初始化方向  宽度  高度
 	var direction = 1;
 	var width = 50 ;
 	var height = 50 ;
	var stuff = new stuff(position , direction,width, height );

	//50毫秒运动一次 
	window.setInterval( move , 50 );
	
    function move(){
        //外边框的边界 
    	var kuangPosition = $('#kuang').position();
    	var kuangWidth = $('#kuang').width();
    	var kuangHeight = $('#kuang').height();
		//alert( kuangWidth + "   "+kuangHeight  );
		//运动速度 
    	var spead = 2 ;
    	
    	//四个方向的边界处理,碰到边界转向 
		switch( stuff.direction )
		{
			case 1 :
				//alert( (stuff.position.y-spead )+"    "+kuangPosition.top );
				if(stuff.position.y-spead<0 ){
					//alert("方向1 ");
					stuff.direction =  2 ; 
				}
				
				break;
			case 2 :
				if(stuff.position.x-spead<0 ){
					//alert("方向2 ");
					stuff.direction =  3 ; 
				}
				
				break;
			case 3 :
				//alert( stuff.position.y+"   "+ stuff.height +"     "+ kuangHeight );
				if(stuff.position.y+ stuff.height +spead> kuangHeight  ){
					stuff.direction =  4 ; 
				}
				break;
			case 4 :
				if(stuff.position.x+ stuff.width+spead> kuangWidth  ){
					//alert("方向4 ");
					stuff.direction =  1 ; 
				}
				break ; 		
		}
		
		//alert( stuff.direction);
		//物体 运动
		stuff.move();
		//改变 运动物体位置 
        $('#sutff').css({left:stuff.position.x ,top:stuff.position.y });
         
    }

 	
 </script>
</HTML>

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值