动态生成一个div块并让其移动
来源:互联网 发布:淘宝达人怎么收取佣金 编辑:程序博客网 时间:2024/05/29 05:04
<html>
<head>
<title>增加div块</title>
</head>
<script>
function chardiv(){
var time;
this.creater=function(){
var a=document.createElement("mydiv"); //创建新的div块
a.style.top=Math.random()*200;
a.style.left=Math.random()*200;
a.style.position="absolute";
a.style.width=35; //新div块的宽度
a.style.height=35; //新div块的高度
a.innerHTML="A";
a.style.background="yellow"; //新div块的背景颜色
mydiv.appendChild(a);
this.move=function(){
a.style.top=parseInt(a.style.top)+20; //div块移动
if(parseInt(a.style.top)>370){ //如果移动到大于370px
a.style.top=0; //返回离top=0px
}
}
}
}
function stop(){
clearInterval(time); //清除定时器
}
function gamestart(){
var charA=new chardiv(); //new 一个实例charA
charA.creater();
charA.move();
time=setInterval(function(){
charA.move();
},500);
}
</script>
</head>
<body>
<button onclick="gamestart()">start</button>
<button onclick="stop()">stop</button>
<div id="mydiv" style="position:absolute;width:500;height:400;background:green;top:50"></div>
</body>
<head>
<title>增加div块</title>
</head>
<script>
function chardiv(){
var time;
this.creater=function(){
var a=document.createElement("mydiv"); //创建新的div块
a.style.top=Math.random()*200;
a.style.left=Math.random()*200;
a.style.position="absolute";
a.style.width=35; //新div块的宽度
a.style.height=35; //新div块的高度
a.innerHTML="A";
a.style.background="yellow"; //新div块的背景颜色
mydiv.appendChild(a);
this.move=function(){
a.style.top=parseInt(a.style.top)+20; //div块移动
if(parseInt(a.style.top)>370){ //如果移动到大于370px
a.style.top=0; //返回离top=0px
}
}
}
}
function stop(){
clearInterval(time); //清除定时器
}
function gamestart(){
var charA=new chardiv(); //new 一个实例charA
charA.creater();
charA.move();
time=setInterval(function(){
charA.move();
},500);
}
</script>
</head>
<body>
<button onclick="gamestart()">start</button>
<button onclick="stop()">stop</button>
<div id="mydiv" style="position:absolute;width:500;height:400;background:green;top:50"></div>
</body>
</html>
效果示意图:
- 动态生成一个div块并让其移动
- 动态生成Checkbox并取得其值
- 如何让一个div跟随鼠标移动
- 动态添加子view,并让其随机分布
- 通过jQuery找到一个div并设置其属性
- 拖动一个div,动态改变其td宽度的实现
- JS新建一个页面,并让其居中
- ASP.NET动态生成TextBox并获取其值
- ASP.NET动态生成TextBox并获取其值
- 键盘控制DIV块移动
- 动态创建一个CButton按钮并实现其ON_BN_CLICKED消息
- 一个div 内嵌套多个div ,并使其全部居中
- 让div跟随鼠标移动
- 动态生成一个div带有按钮和关闭的
- HTML+CSS基础 margin 让一个div块在网页中左右居中显示
- 动态生成DIV
- 动态生成DIV 对象
- JS动态生成DIV
- 随机数范围扩展方法总结 .
- MVC模式实例总结
- LeetCode水题
- Delphi的类与继承
- Ural 1060. Flip Game dfs
- 动态生成一个div块并让其移动
- 昂贵的聘礼
- Cocos2d-x连接Mina NIO服务器要点
- hdu 2871 线段树+区间合并 好题
- ppt技巧汇总
- android之handler
- 用java实现的双向链表增加删除修改操作
- intel dpdk api testpmd 程序源码详解
- 学习计划:《数据结构(C语言版)》