IT人才js--碰壁反弹

这个东西倒不是很难,就是练练对临界值的判断,应该很多东西能用到这个,特别是游戏什么的

要做到临界值的判断就要获取元素的某些值,如下:

假设 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

时间: 2024-10-07 21:39:49

IT人才js--碰壁反弹的相关文章

js中小球碰壁反弹

一. 在指定容器内的碰壁反弹 <!DOCTYPE HTML> <html> <head> <title></title> <meta charset="UTF-8"/> <style type="text/css"> .ball{ height: 60px; width: 60px; background: blue; border-radius: 50%; position: ab

Javascript:实操---碰壁反弹(有阻碍物)

CSS部分 <style>*{    margin: 0px;    padding: 0px; }#main{    width: 700px;    height: 500px;    border: 1px solid black;    position: relative;}#child{    width: 30px;    height: 30px;    border-radius: 15px;    background-color: #ccc;    position: a

Javascript:实操---碰壁反弹(无阻碍物)

CSS部分 <style type="text/css">#wrap{    height: 300px;    width: 500px;    border: 1px solid #000;    position: relative;}#child{    position: absolute;    left: 0;    top: 0;    height: 50px;    width: 50px;    background-color: #ccc;}<

初探JavaScript(三)——JS带我碰壁带我飞

已经写了两篇关于小白的JavaScript之行,不可否认,每一种语言都有其精华与糟粕之处,来不及细细体味其精华奥妙,也没法对其评头论足,只能先了解,后深入.到目前为止已经看完<JavaScript Dom 编程艺术>(ps:看过书评,网友对其褒贬不一,个人感觉还是不错的,适合初学者.不是每本书都能得到所有读者的认可,只能让部分适合的读者对其称赞,而我,就是其中一个). 前面介绍了JavaScript的一些常用方法,如何与DOM.HTML配合完成一些交互.今天主要介绍JavaScript在动画效

小球碰壁回弹--- html(JavaScript)

这两天解除了下 html 看到想起来 iOS 上有个游戏感觉挺好的,于是想着用 js 写写试试, 结果实现了, 多余的也不说了, 直接上代码 主要地方都有注释 <!DOCTYPE html><html>    <head>        <meta charset="utf-8">        <title>碰壁反弹</title>        <style type="text/css&quo

原生js的弹力小球

有段日子没发博客了,今天来一弹. 这次发的东西,难度比较大,用原生js,写的一个弹力小球,而且是群发,下面是一张截图. 每一次点击"添加小球",都会触发程序,新创造一个div盒子.定义css样式,然后塞进大盒子中. 接着,给这个div小球创造一个定时器,让他开始移动,并且在碰到墙壁时,会停止匀速运行的定时器,执行另一个函数,开启变速移动的定时器,同时对div小球开启进行变形定时器,实现碰到墙壁后的减速.变形.改变方向.再加速和恢复体形. 讲述原理比较简单,而真正做起来,就麻烦了. 程序

[canvas]通过动态生成像素点做绚丽效果

本例中的粒子就是实实在在的像素,由js代码在canvas上动态生成的像素点!这些像素点通过一个运动方法有规律地动了起来.透过这个思路,我们可以想到很多很炫的效果,但是这个性能有待考察.实验证明,动态控制太多像素点的话绝对会卡的!在做效果方面有经验的朋友,请提出宝贵意见!这个思路走得通么? <!doctype html> <html> <head> <title>智能粒子</title> <meta charset='utf-8' />

Cocos2d入门--3-- 向量的应用

 Cocos2d入门--3-- 向量的应用 小球向一个方向持续运动的Demo HelloWorldScene.h ...... //设置一个protected的属性 protected: cocos2d::Vec2 _vec; ...... HelloWorldScene.cpp //这个是通过随机数设置向量的方向 _vec.set(random(-0.1f, 1.0f), random(-1.0f, 1.0f)); //通过normalize这个标准化函数,能够使得向量的大小为1 _vec.n

H - The Endless River

1 /* 2 从顶部发射石头,按照碰壁反弹的运动路线问能不能击中底部的某个位置. 3 计算落点,飞行轨迹周期为2*(w-1),所以直接将高度视为h=h%(2*w-2),然后如果高度 4 不大于w 则落点即为高度,否则落点在2*w-h的位置. 5 */ 6 #include <bits/stdc++.h> 7 using namespace std; 8 int main() 9 { 10 int n; 11 scanf("%d",&n); 12 while(n--)