这个东西倒不是很难,就是练练对临界值的判断,应该很多东西能用到这个,特别是游戏什么的
要做到临界值的判断就要获取元素的某些值,如下:
假设 obj 为某个 HTML 控件
-->obj.offsetTop 指 obj 相对于版面或由 offsetParent 属性指定的父坐标的计算上侧位置,整型,单位像素。
-->obj.offsetLeft 指 obj 相对于版面或由 offsetParent 属性指定的父坐标的计算左侧位置,整型,单位像素。
-->obj.offsetWidth 指 obj 控件自身的绝对宽度,不包括因 overflow 而未显示的部分,也就是其实际占据的宽度,整型,单位像素。
-->obj.offsetHeight 指 obj 控件自身的绝对高度,不包括因 overflow 而未显示的部分,也就是其实际占据的高度,整型,单位像素。
另外,提一个逗比的事。。。<!-- 没有offsetRight和offsetBottom -->
刚用的时候把那个用什么去了,结果什么都没获取到,幸亏问了别人一下,不然要纠结好久了,呵呵。。。
我们知道 offsetTop 可以获得 HTML 元素距离上方或外层元素的位置,style.top 也是可以的,二者的区别是:
一、offsetTop 返回的是数字,而 style.top 返回的是字符串,除了数字外还带有单位:px。
二、offsetTop 只读,而 style.top 可读写。
三、如果没有给 HTML 元素指定过 top 样式,则 style.top 返回的是空字符串。
offsetLeft 与 style.left、offsetWidth 与 style.width、offsetHeight 与 style.height 也是同样道理。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>碰壁反弹</title>
<style type="text/css">
#wrap{height: 500px;width: 800px;border: 1px solid black;position: relative;}
#child{height: 50px;width: 50px;position: absolute;background: #09f;border-radius: 100%; left: 0px;}
#div1{width: 150px;height: 300px;background: #cc3;position: relative;top: 200px;left: 180px;}
#div2{width: 200px;height: 200px;background: #c33;position: relative;top: -300px;left: 600px; }
</style>
<script type="text/javascript">
window.onload=function(){
var oWrap=document.getElementById("wrap");
var oChild=document.getElementById("child");
var oDiv1=document.getElementById("div1");
var oDiv2=document.getElementById("div2");
var maxLeft=oWrap.clientWidth-oChild.offsetWidth;
var maxTop=oWrap.clientHeight-oChild.offsetHeight;
var startX=0;
var startY=0;
var x=1;
var y=1;
var left=oDiv1.offsetLeft-oChild.offsetWidth;
var right=oDiv1.offsetLeft+oDiv1.offsetWidth;
var top=oDiv1.offsetTop-oChild.offsetHeight;
var bottom=maxTop;
//var bottom=oDiv1.offsetTop+oDiv1.offsetHeight;
var left2=oDiv2.offsetLeft-oChild.offsetWidth;
var bottom2=oDiv2.offsetHeight;
function random(min,range) {
return Math.floor(Math.random() * range+min);
}
function bg() {
var a = random(0,255);
var b = random(0,255);
var c = random(0,255);
oChild.style.background = ‘rgba(‘+a+‘,‘+b+‘,‘+c+‘,0.8)‘;
// setTimeout(bg, 1000);
}
var timer=setInterval(function(){
//边线
if(startX>maxLeft||startX<0){
x*=-1;
bg();
}
if(startY>maxTop||startY<0){
y*=-1;
bg();
}
//障碍物div1
if(startX==left&&startY>top){
x*=-1;
bg();
}
if(startY==top&&startX>left&&startX<right){
y*=-1;
bg();
}
if(startX==right&&startY>top){
x*=-1;
bg();
}
//障碍物div2
if(startX==left2&&startY<bottom2){
x*=-1;
bg();
}
if(startY==bottom2&&startX>left2){
y*=-1;
bg();
}
if(startX==200&&startY==200){
x*=-1;
y*=-1;
bg();
}
if(startX==300&&startY==200){
x*=-1;
y*=-1;
bg();
}
startX+=x*5;
startY+=y*5;
oChild.style.left=startX+"px";
oChild.style.top=startY+"px";
},30)
}
</script>
</head>
<body>
</body>
<div id="wrap">
<div id="child"></div>
<div id="div1"></div>
<div id="div2"></div>
</div>
</html>
http://c.tieba.baidu.com/p/3356924842
http://c.tieba.baidu.com/p/3356926963
http://bbs.28tui.com/thread-7551462-1-1.html
http://bbs.28tui.com/thread-7551475-1-1.html
http://bbs.28tui.com/thread-7551535-1-1.html
http://bbs.28tui.com/thread-7551586-1-1.html
http://bbs.28tui.com/thread-7551596-1-1.html
http://bbs.28tui.com/thread-7551601-1-1.html
http://bbs.28tui.com/thread-7551642-1-1.html
http://bbs.28tui.com/thread-7551651-1-1.html
http://bbs.28tui.com/thread-7551661-1-1.html
http://bbs.28tui.com/thread-7551670-1-1.html
http://bbs.28tui.com/thread-7551678-1-1.html
http://bbs.28tui.com/thread-7552320-1-1.html
http://bbs.28tui.com/thread-7552329-1-1.html
http://bbs.28tui.com/thread-7552401-1-1.html
http://bbs.28tui.com/thread-7553200-1-1.html
http://bbs.28tui.com/thread-7553213-1-1.html
http://bbs.28tui.com/thread-7553229-1-1.html
http://bbs.28tui.com/thread-7553229-1-1.html
http://bbs.28tui.com/thread-7553240-1-1.html
http://bbs.28tui.com/thread-7553253-1-1.html
http://bbs.28tui.com/thread-7553261-1-1.html
http://bbs.28tui.com/thread-7553277-1-1.html
http://bbs.28tui.com/thread-7553286-1-1.html
http://bbs.28tui.com/thread-7553301-1-1.html
http://bbs.28tui.com/thread-7553313-1-1.html
http://bbs.28tui.com/thread-7553328-1-1.html
http://bbs.28tui.com/thread-7553345-1-1.html
http://bbs.28tui.com/thread-7553357-1-1.html
http://bbs.28tui.com/thread-7553377-1-1.html
http://bbs.28tui.com/thread-7553393-1-1.html
http://bbs.28tui.com/thread-7553411-1-1.html
http://bbs.28tui.com/thread-7554767-1-1.html
http://bbs.28tui.com/thread-7554996-1-1.html
http://bbs.28tui.com/thread-7555013-1-1.html
http://bbs.28tui.com/thread-7555028-1-1.html
http://bbs.28tui.com/thread-7555040-1-1.html
http://bbs.28tui.com/thread-7555050-1-1.html
http://bbs.28tui.com/thread-7555065-1-1.html
http://bbs.28tui.com/thread-7555078-1-1.html
http://bbs.28tui.com/thread-7555091-1-1.html
http://bbs.28tui.com/thread-7555106-1-1.html
http://bbs.28tui.com/thread-7555115-1-1.html
http://bbs.28tui.com/thread-7555130-1-1.html
http://bbs.28tui.com/thread-7555138-1-1.html
http://bbs.28tui.com/thread-7555150-1-1.html
http://bbs.28tui.com/thread-7555160-1-1.html
http://bbs.28tui.com/thread-7555173-1-1.html
http://bbs.28tui.com/thread-7555192-1-1.html
http://bbs.28tui.com/thread-7555200-1-1.html
http://bbs.28tui.com/thread-7555213-1-1.html
http://bbs.28tui.com/thread-7555223-1-1.html
http://bbs.28tui.com/thread-7562270-1-1.html
http://bbs.28tui.com/thread-7562277-1-1.html
http://bbs.28tui.com/thread-7562294-1-1.html
http://bbs.28tui.com/thread-7562304-1-1.html
http://bbs.28tui.com/thread-7562316-1-1.html
http://bbs.28tui.com/thread-7562327-1-1.html
http://bbs.28tui.com/thread-7562336-1-1.html
http://bbs.28tui.com/thread-7562342-1-1.html
http://bbs.28tui.com/thread-7562470-1-1.html
http://bbs.28tui.com/thread-7562475-1-1.html
http://bbs.28tui.com/thread-7562481-1-1.html
http://bbs.28tui.com/thread-7562504-1-1.html
http://bbs.28tui.com/thread-7562508-1-1.html
http://bbs.28tui.com/thread-7562518-1-1.html
http://bbs.28tui.com/thread-7563171-1-1.html
http://bbs.28tui.com/thread-7563175-1-1.html
http://bbs.28tui.com/thread-7563182-1-1.html
http://bbs.28tui.com/thread-7563185-1-1.html
http://bbs.28tui.com/thread-7563189-1-1.html
http://bbs.28tui.com/thread-7577551-1-1.html
http://bbs.28tui.com/thread-7577569-1-1.html
http://bbs.28tui.com/thread-7577691-1-1.html
http://bbs.28tui.com/thread-7577702-1-1.html
http://bbs.28tui.com/thread-7577710-1-1.html
http://bbs.28tui.com/thread-7577722-1-1.html
http://bbs.28tui.com/thread-7577722-1-1.html
http://bbs.28tui.com/thread-7577728-1-1.html
http://bbs.28tui.com/thread-7577728-1-1.html
http://bbs.28tui.com/thread-7577740-1-1.html
http://bbs.28tui.com/thread-7577752-1-1.html
http://bbs.28tui.com/thread-7577763-1-1.html
http://bbs.28tui.com/thread-7577776-1-1.html
http://bbs.28tui.com/thread-7577787-1-1.html
http://bbs.28tui.com/thread-7577797-1-1.html
http://bbs.28tui.com/thread-7577808-1-1.html
http://bbs.28tui.com/thread-7577821-1-1.html
http://bbs.28tui.com/thread-7577829-1-1.html
http://bbs.28tui.com/thread-7577839-1-1.html
http://bbs.28tui.com/thread-7577851-1-1.html
http://bbs.28tui.com/thread-7577859-1-1.html
http://bbs.28tui.com/thread-7578529-1-1.html
http://bbs.28tui.com/thread-7579599-1-1.html
http://bbs.28tui.com/thread-7583091-1-1.html
http://bbs.28tui.com/thread-7583161-1-1.html
http://bbs.28tui.com/thread-7583163-1-1.html
http://bbs.28tui.com/thread-7583164-1-1.html
http://bbs.28tui.com/thread-7583166-1-1.html
http://bbs.28tui.com/thread-7583167-1-1.html
http://bbs.28tui.com/thread-7583170-1-1.html
http://bbs.28tui.com/thread-7583586-1-1.html
http://bbs.28tui.com/thread-7583592-1-1.html
http://bbs.28tui.com/thread-7583601-1-1.html
http://bbs.28tui.com/thread-7583605-1-1.html
http://bbs.28tui.com/thread-7583612-1-1.html
http://bbs.28tui.com/thread-7583652-1-1.html
http://bbs.28tui.com/thread-7583674-1-1.html
http://bbs.28tui.com/thread-7583674-1-1.html
http://bbs.28tui.com/thread-7583680-1-1.html
http://bbs.28tui.com/thread-7583680-1-1.html
http://bbs.28tui.com/thread-7583683-1-1.html
http://bbs.28tui.com/thread-7583688-1-1.html
http://bbs.28tui.com/thread-7583691-1-1.html
http://bbs.28tui.com/thread-7583698-1-1.html
http://bbs.28tui.com/thread-7583702-1-1.html
http://bbs.28tui.com/thread-7583706-1-1.html
http://bbs.28tui.com/thread-7583711-1-1.html
http://bbs.28tui.com/thread-7583717-1-1.html
http://bbs.28tui.com/thread-7583774-1-1.html
http://bbs.28tui.com/thread-7583779-1-1.html
http://bbs.28tui.com/thread-7583780-1-1.html
http://bbs.28tui.com/thread-7583784-1-1.html
http://bbs.28tui.com/thread-7490461-1-1.html
http://bbs.28tui.com/thread-7490445-1-1.html
http://bbs.28tui.com/thread-7490430-1-1.html
http://bbs.28tui.com/thread-7490411-1-1.html
http://bbs.28tui.com/thread-7490386-1-1.html
http://bbs.28tui.com/thread-7490367-1-1.html
http://bbs.28tui.com/thread-7490347-1-1.html
http://bbs.28tui.com/thread-7490347-1-1.html
http://bbs.28tui.com/thread-7490328-1-1.html
http://bbs.28tui.com/thread-7490308-1-1.html
http://bbs.28tui.com/thread-7490288-1-1.html
http://bbs.28tui.com/thread-7490272-1-1.html
http://bbs.28tui.com/thread-7490252-1-1.html
http://bbs.28tui.com/thread-7490237-1-1.html
http://bbs.28tui.com/thread-7490223-1-1.html
http://bbs.28tui.com/thread-7490210-1-1.html
http://bbs.28tui.com/thread-7490190-1-1.html
http://bbs.28tui.com/thread-7490174-1-1.html
http://bbs.28tui.com/thread-7490092-1-1.html
http://bbs.28tui.com/thread-7490063-1-1.html
http://bbs.28tui.com/thread-7490025-1-1.html
http://bbs.28tui.com/thread-7491764-1-1.html
http://bbs.28tui.com/thread-7491780-1-1.html
http://bbs.28tui.com/thread-7491792-1-1.html
http://bbs.28tui.com/thread-7491804-1-1.html
http://bbs.28tui.com/thread-7491814-1-1.html
http://bbs.28tui.com/thread-7491831-1-1.html
http://bbs.28tui.com/thread-7491841-1-1.html
http://bbs.28tui.com/thread-7491849-1-1.html
http://bbs.28tui.com/thread-7491863-1-1.html
http://bbs.28tui.com/thread-7491868-1-1.html
http://bbs.28tui.com/thread-7491884-1-1.html
http://bbs.28tui.com/thread-7491896-1-1.html
http://bbs.28tui.com/thread-7491905-1-1.html
http://bbs.28tui.com/thread-7491927-1-1.html
http://bbs.28tui.com/thread-7491934-1-1.html
http://bbs.28tui.com/thread-7491950-1-1.html
http://bbs.28tui.com/thread-7491967-1-1.html
http://bbs.28tui.com/thread-7491985-1-1.html
http://bbs.28tui.com/thread-7491996-1-1.html
http://bbs.28tui.com/thread-7492009-1-1.html
http://bbs.28tui.com/thread-7494542-1-1.html
http://bbs.28tui.com/thread-7495794-1-1.html
http://bbs.28tui.com/thread-7495810-1-1.html
http://bbs.28tui.com/thread-7495968-1-1.html
http://bbs.28tui.com/thread-7495972-1-1.html
http://bbs.28tui.com/thread-7495987-1-1.html
http://bbs.28tui.com/thread-7495991-1-1.html
http://bbs.28tui.com/thread-7496001-1-1.html
http://bbs.28tui.com/thread-7496014-1-1.html
http://bbs.28tui.com/thread-7496119-1-1.html
http://bbs.28tui.com/thread-7496144-1-1.html
http://bbs.28tui.com/thread-7496166-1-1.html
http://bbs.28tui.com/thread-7496185-1-1.html
http://bbs.28tui.com/thread-7496316-1-1.html
http://bbs.28tui.com/thread-7497935-1-1.html
http://bbs.28tui.com/thread-7498038-1-1.html
http://bbs.28tui.com/thread-7498041-1-1.html
http://bbs.28tui.com/thread-7501173-1-1.html
http://bbs.28tui.com/thread-7501157-1-1.html
http://bbs.28tui.com/thread-7501173-1-1.html
http://bbs.28tui.com/thread-7501186-1-1.html
http://bbs.28tui.com/thread-7508001-1-1.html
http://bbs.28tui.com/thread-7508014-1-1.html
http://bbs.28tui.com/thread-7508041-1-1.html
http://bbs.28tui.com/thread-7508067-1-1.html
http://bbs.28tui.com/thread-7508079-1-1.html
http://bbs.28tui.com/thread-7508141-1-1.html
http://bbs.28tui.com/thread-7508160-1-1.html
http://bbs.28tui.com/thread-7508242-1-1.html
http://bbs.28tui.com/thread-7508300-1-1.html
http://bbs.28tui.com/thread-7508382-1-1.html
http://bbs.28tui.com/thread-7508404-1-1.html
http://bbs.28tui.com/thread-7508404-1-1.html
http://bbs.28tui.com/thread-7509181-1-1.html
http://bbs.28tui.com/thread-7509200-1-1.html
http://bbs.28tui.com/thread-7509265-1-1.html
http://bbs.28tui.com/thread-7509279-1-1.html
http://bbs.28tui.com/thread-7509332-1-1.html
http://bbs.28tui.com/thread-7509357-1-1.html
http://bbs.28tui.com/thread-7509372-1-1.html
http://bbs.28tui.com/thread-7510720-1-1.html
http://bbs.28tui.com/thread-7509404-1-1.html
http://bbs.28tui.com/thread-7510858-1-1.html
http://bbs.28tui.com/thread-7510720-1-1.html
http://bbs.28tui.com/thread-7510869-1-1.html
http://bbs.28tui.com/thread-7519896-1-1.html
http://bbs.28tui.com/thread-7519905-1-1.html
http://bbs.28tui.com/thread-7519972-1-1.html
http://bbs.28tui.com/thread-7519989-1-1.html
http://bbs.28tui.com/thread-7519997-1-1.html
http://bbs.28tui.com/thread-7520005-1-1.html
http://bbs.28tui.com/thread-7520013-1-1.html
http://bbs.28tui.com/thread-7520024-1-1.html
http://bbs.28tui.com/thread-7548297-1-1.html
http://bbs.28tui.com/thread-7548601-1-1.html
http://bbs.28tui.com/thread-7548667-1-1.html
http://bbs.28tui.com/thread-7548713-1-1.html
http://bbs.28tui.com/thread-7548728-1-1.html
http://bbs.28tui.com/thread-7548737-1-1.html
http://bbs.28tui.com/thread-7548754-1-1.html
http://bbs.28tui.com/thread-7548754-1-1.html
http://bbs.28tui.com/thread-7548761-1-1.html
http://bbs.28tui.com/thread-7548773-1-1.html
http://bbs.28tui.com/thread-7548788-1-1.html
http://bbs.28tui.com/thread-7548796-1-1.html
http://bbs.28tui.com/thread-7548808-1-1.html
http://bbs.28tui.com/thread-7548820-1-1.html
http://bbs.28tui.com/thread-7548827-1-1.html
http://bbs.28tui.com/thread-7548827-1-1.html
http://bbs.28tui.com/thread-7548843-1-1.html
http://bbs.28tui.com/thread-7548857-1-1.html
http://bbs.28tui.com/thread-7549270-1-1.html
http://bbs.28tui.com/thread-7549325-1-1.html
http://bbs.28tui.com/thread-7549533-1-1.html
http://bbs.28tui.com/thread-7549545-1-1.html
http://bbs.28tui.com/thread-7549551-1-1.html
http://bbs.28tui.com/thread-7549803-1-1.html
http://bbs.28tui.com/thread-7549835-1-1.html
http://bbs.28tui.com/thread-7549904-1-1.html
http://bbs.28tui.com/thread-7549929-1-1.html
http://bbs.28tui.com/thread-7549936-1-1.html
http://bbs.28tui.com/thread-7549947-1-1.html
http://bbs.28tui.com/thread-7549968-1-1.html
http://bbs.28tui.com/thread-7549980-1-1.html
http://bbs.28tui.com/thread-7549985-1-1.html
http://bbs.28tui.com/thread-7550007-1-1.html
http://bbs.28tui.com/thread-7550021-1-1.html
http://bbs.28tui.com/thread-7550028-1-1.html
http://bbs.28tui.com/thread-7550043-1-1.html
http://bbs.28tui.com/thread-7550059-1-1.html
http://bbs.28tui.com/thread-7550254-1-1.html
http://bbs.28tui.com/thread-7550264-1-1.html
http://bbs.28tui.com/thread-7551394-1-1.html
http://bbs.28tui.com/thread-7551406-1-1.html
http://bbs.28tui.com/thread-7551421-1-1.html
http://bbs.28tui.com/thread-7551430-1-1.html
http://bbs.28tui.com/thread-7551443-1-1.html
http://bbs.28tui.com/thread-7551454-1-1.html