JS元素操作與定時器

JS元素操作與定時器

1.時間定時器-延遲
// setInterval();定時延遲
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">
    //1.創建元素
    var newDiv = document.createElement("div");
    //設置類名
    newDiv.id = "first";
    newDiv.className  = "item";
    newDiv.style.background = "red";
    newDiv.innerHTML = "吉林省農村冀東水泥加快速度開始懂了衛計委的委屈ID額廢物的髮尾打上單  後期物業的顧問DGSAJFGSADHJKGFASDFAEUIWQHREUW服務而非福利和王力宏";
    //2.添加元素
    var body = document.getElementsByTagName('body')[0];
    var box = document.getElementsByClassName('box')[0];
    body.appendChild(newDiv);//在body最後面添加元素
    body.insertBefore(newDiv,box);//把newDiv插進box前面
    // body.replaceChild(newDiv,box);//newDiv替換掉box
    //3.刪除元素
    // body.removeChild(box);
    // box.remove();

  // js 基礎語法
  // 原生對象
  var obj = {
    name:"丁鵬",
    age:12,
    gender:"man",
    eat:function(){
        console.log("開始出發");
    }
  };
  obj.dd = 1;
  console.log(obj);
  console.log(obj.age);
  obj.eat();
 //數組對象 瀏覽器自己添加的對象(div);
 console.log(newDiv);
//dom(元素/div..)屬性
//節點 標籤 文本,註釋 聲明(doctype)
//獲取元素下所有字點
console.log(box.childNodes);
//獲取標籤類型的子節點
console.log(box.childNodes[1]);
//獲取div尺寸大小
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();定時延遲
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() 方法用於在指定的毫秒數後調用函數或計算表達式...只能執行一次

    //clearInterval(timer)清除定時器 使定時器停止
    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(){//如果按多次按鈕就會創建多個setInterval(,)定時器
        num++;
        console.log(num);
        if (num >= 2) {
            clearInterval(createTimer);
            console.log(num);
        }
        createTimer = setInterval(function(){   
            var jb = document.createElement("img");//創建img金幣
             jb.src = "../img/11.jpg";//添加圖片鏈接
             jb.className = "item";//添加css樣式
             jb.style.left = Math.random()*500 + 100 + "px";//規定圖片創建的距離
             document.getElementsByTagName("body")[0].appendChild(jb);//在body最後面添加jb
             var downTime = setInterval(function(){
                jb.style.top = jb.offsetTop + 5 + "px";//金幣離父級body頂部的距離
                if (jb.offsetTop >= 500) {
                    jb.remove();
                    clearInterval(downTime);
                }
             },20);

        },200);
    }
</script>
</html>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章