猿问

这个碰撞运动,变成水平怎么解决

<!doctype html><!--申明当前文档的类型-->

<html>

<head>

<!--中文编码集:GBK  GB2312   国际编码:utf-8-->

<meta http-equiv="content-type" content="text/html" charset="utf-8" />

<!--申明当前页面的三要素-->

<meta name="Keywords" content="关键字,关键词" />

<meta name="Description" content="描述和简介" />

<title>1612届零基础班</title>

<style type="text/css">

#box1{width:200px;height:200px;background:#ccc;position:absolute;left:0px;top:0px;}

</style>

</head>

 <body>

<div id="box1"></div>

 </body>

<script type="text/javascript">

//div的位置

var sx=0;

var sy=0;

var speed=10;//速度

var xpe=speed;

var ype=speed;

var box=document.getElementById("box1");

var maxwidth=window.innerWidth - box.offsetWidth;//浏览器宽度-盒子宽度

var maxheight=window.innerHeight - box.offsetHeight;

function draw(){

sx++;

sy++;

if(sx>=maxwidth){

xpe=-speed;

sx=maxwidth;

}

if(sx<=0){

xpe=-speed;

x=0;

}

if(sy>=maxheight){

ype=-speed;

sy=maxheight;

}

if(sy <=0){

ype =speed;

sy= maxheight;

}

box.style.left=sx+"px";

box.style.top=sy+"px";

};

setInterval(function(){

draw();

},1);

</script>

</html>


慕粉3884565
浏览 1251回答 1
1回答
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答