怎么在JavaScript中实现一个浮动碰撞效果?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
具体如下:
<html>
<head>
<meta charset="UTF-8">
<title>www.jb51.net JS碰撞效果</title>
<script language="javascript">
<!--
directX=1; //X轴方向
directY=1; //Y轴方向
sunX=0;
sunY=0;
function sunMov(){
//定两个方向
sunX+=directX*2;
sunY+=directY*2;
//修改div的left top
sundiv.style.top=sunY+"px";
sundiv.style.left=sunX+"px";
//判断什么时候,转变方向
//x方向(offestWidth可以返回,当前这个对象的实际宽度)
if(sunX+sundiv.offsetWidth>=document.body.clientWidth || sunX<=0){
directX=-directX;
}
if(sunY+sundiv.offsetHeight>=document.body.clientHeight || sunY<=0){
directY=-directY;
}
}
setInterval("sunMov()",10);
//-->
</script>
</head>
<body >
<div id="sundiv" >
<img src="https://www.jb51.net/images/logo.gif"/>
</div>
</body>
</html>
看完上述内容,你们掌握怎么在JavaScript中实现一个浮动碰撞效果的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注天达云行业资讯频道,感谢各位的阅读!