JS元素操作与定时器
1.时间定时器-延迟
setInterval(function(){
newDiv.scrollTop = 200;
},1000);
1.元素操作
1.创建元素
2.添加元素
3.删除元素
4.替换元素
5.原生对象
6.获取元素的节点
7.获取div尺寸大小
8.div内容尺寸大小
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
.item{
width: 200px;
height:200px;
margin: 10px;
border :10px blue solid;
overflow: scroll ;
font-size: 50px;
}
body{
position: relative;
}
</style>
</head>
<body>
<div class="box">
我是box
<p>P</p>
</div>
</body>
<script type="text/javascript">
var newDiv = document.createElement("div");
newDiv.id = "first";
newDiv.className = "item";
newDiv.style.background = "red";
newDiv.innerHTML = "吉林省农村冀东水泥加快速度开始懂了卫计委的委屈ID额废物的发尾打上单 后期物业的顾问DGSAJFGSADHJKGFASDFAEUIWQHREUW服务而非福利和王力宏";
var body = document.getElementsByTagName('body')[0];
var box = document.getElementsByClassName('box')[0];
body.appendChild(newDiv);
body.insertBefore(newDiv,box);
var obj = {
name:"丁鹏",
age:12,
gender:"man",
eat:function(){
console.log("开始出发");
}
};
obj.dd = 1;
console.log(obj);
console.log(obj.age);
obj.eat();
console.log(newDiv);
console.log(box.childNodes);
console.log(box.childNodes[1]);
console.log(newDiv.offsetHeight);
console.log(newDiv.offsetWidth);
console.log(newDiv.clientHeight);
console.log(newDiv.scrollHeight);
console.log(newDiv.offsetLeft);
console.log(newDiv.scrollTop);
setInterval(function(){
newDiv.scrollTop = 200;
},1000);
</script>
</html>
2.定时器
1.一次性延迟定时器
setTimeout(f1,1000);
function f1(){
console.log("执行了f1");
}
注释:1000毫秒后执行f1();
2.循环定时器
var timer = setInterval(function(){
console.log("循环定时器");
},1000);
注释:1000执行一次函数
3.clearInterval(timer) / clearTimeout()
注释:清除定时器 使定时器停止
<html>
<head>
<meta charset="utf-8">
<title>定时器</title>
<style type="text/css">
#first{
width: 100px;
height: 100px;
background-color: red;
position:absolute;
left: 0px;
}
</style>
</head>
<body>
<div id="first"></div>
</body>
<script type="text/javascript">
setTimeout(f1,1000);
function f1(){
console.log("执行了f1");
}
var timer = setInterval(function(){
console.log("循环定时器");
},1000);
setTimeout(function(){
clearInterval(timer);
},3000);
var first = document.getElementById('first');
first.onclick = function(){
var move = setInterval(function(){
console.log(first.offsetLeft);
first.style.left = first.offsetLeft + 5 + "px";
if (first.offsetLeft >= 500) {
clearInterval(move);
}
},1000);
}
</script>
</html>
3.接金币游戏
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>金币</title>
<style type="text/css">
.item{
width: 60px;
border-radius: 50%;
position: absolute;
left: 100px;
top: 0px;
}
</style>
</head>
<body>
<input type="button" value="开始" id="start"/>
<input type="button" value="结束" id="stop"/>
</body>
<script type="text/javascript">
var start = document.getElementById('start');
var stop = document.getElementById('stop');
var createTimer;
stop.onclick = function(){
clearInterval(createTimer);
}
var num = 0;
start.onclick = function(){
num++;
console.log(num);
if (num >= 2) {
clearInterval(createTimer);
console.log(num);
}
createTimer = setInterval(function(){
var jb = document.createElement("img");
jb.src = "../img/11.jpg";
jb.className = "item";
jb.style.left = Math.random()*500 + 100 + "px";
document.getElementsByTagName("body")[0].appendChild(jb);
var downTime = setInterval(function(){
jb.style.top = jb.offsetTop + 5 + "px";
if (jb.offsetTop >= 500) {
jb.remove();
clearInterval(downTime);
}
},20);
},200);
}
</script>
</html>