4.13 JS04

點名系統(tǒng)

var btns = document.getElementById('box').getElementsByTagName('button');
var oh1 = document.getElementById('oh1');
var timer = null;
var arr = ['張三','李四','王五','劉二麻子','二狗','小明','隔壁老王'];
btns[0].onclick = function () {
    clearInterval(timer);
    timer = setInterval(function () {
        var randomNum = parseInt(Math.random() * arr.length);
        oh1.innerHTML = arr[randomNum];
    },10);
};
btns[1].onclick = function () {
    clearInterval(timer);
}
重點: var randomNum = parseInt(Math.random() * arr.length);乘以數(shù)組的長度再用parseInt確保可以取到數(shù)組里的每一個數(shù).

勻速動畫

var btn = document.getElementById("btn");
    var box = document.getElementById("box");
    var timer = null;
    var begin = 0;
    var target = 800;
    var speed = 4;
    btn.onclick = function () {
        clearInterval(timer);
        timer = setInterval(function () {
            begin += speed;
            if (begin >= target) {
                clearInterval(timer);
                begin = target;
            }
            box.style.left = begin + "px";
        }, 10)
    }

緩速動畫

    var btn = document.getElementById("btn");
    var box = document.getElementById("box");
    var timer = null;
    var start = 0;
    var end = 800;
    btn.onclick = function () {
        clearInterval(timer);
        timer = setInterval(function () {
            start += Math.ceil((end - start ) / 15);
            console.log(start);
            if(start>=end){
                start = end;
                clearInterval(timer);
            }
            box.style.left = start + "px";
        }, 50)
    }
重點:start += Math.ceil((end - start ) / 15);其中15是系數(shù),越小動的越快

長圖展示

 var pic = document.getElementById("pic");
    var spans = document.getElementsByTagName("span");
    var timer = null;
    var speed = 10;
    var topMargin = 0;
    var target = -400;
    spans[0].onmouseover = function () {
        clearInterval(timer);
        timer = setInterval(function () {
            topMargin += speed;
            if (topMargin >= 0) {
                clearInterval(timer);
                topMargin = 0;
            }
            pic.style.top = topMargin + "px";
        }, 10)
    }
    spans[1].onmouseover = function () {
        clearInterval(timer);
        timer = setInterval(function () {
            topMargin -= speed;
            if (topMargin <= target) {
                clearInterval(timer);
                topMargin = target;
            }
            pic.style.top = topMargin + "px";
        }, 10)
    }

陰影展示

var box = document.getElementById("box");
    var cover = document.getElementById("cover");
    var timer = null;
    var topSize = 200;
    box.onmouseover = function () {
        clearInterval(timer);
        timer = setInterval(function () {
            topSize -= 4;
            if(topSize<=0){
                clearInterval(timer);
                topSize = 0;
            }
            cover.style.top = topSize + "px";
        },10);
    }

頁面跳轉(zhuǎn)+遞歸函數(shù)

    var oh6 = document.getElementById("oh6");
    var count = 5;
    var timer = setTimeout(function () {
        clearTimeout(timer);
        count--;
        if (count < 1) {
            window.location.;
        }
        else {
            oh6.innerHTML = count + "秒以后跳轉(zhuǎn)到指定頁面";
        }
        timer = setTimeout(arguments.callee, 1000);
    }, 1000)
重點:window.location.;:跳轉(zhuǎn)目標地址.arguments.callee:在匿名函數(shù)內(nèi)部找到函數(shù)本身,調(diào)用自己,簡稱遞歸,一次定時器的多次使用

線程:用來執(zhí)行任務
隊列:先進先出

高級輪播圖

var spans = document.getElementsByTagName("span");
    var pic = document.getElementById("pic");
    var timer = null;
    var beginLeft = 0;
    var loop = 0;
    var speed = 4;
    var flag = false;
    spans[1].onclick = function () {
        if (flag == true) {
            return;
        }
        flag = true;
        clearInterval(timer);
        loop++;
        if (loop > 5) {
            loop = 1;
            beginLeft = 0;
        }
        timer = setInterval(function () {
            beginLeft -= speed;
            if (beginLeft <= -600 * loop) {
                clearInterval(timer);
                beginLeft = -600 * loop;
                flag = false;
            }
            pic.style.left = beginLeft + "px";
        }, 10)
    }
    spans[0].onclick = function () {
        if (flag == true) {
            return;
        }
        flag = true;
        clearInterval(timer);
        loop--;
        if (loop < 1) {
            loop = 4;
            beginLeft = -600 * 5;
        }
        timer = setInterval(function () {
            beginLeft += speed;
            if (beginLeft >= -600 * loop) {
                clearInterval(timer);
                beginLeft = -600 * loop;
                flag = false;
            }
            pic.style.left = beginLeft + "px";
        }, 10)
    }
重點:
  1. cursor: pointer;:一個css屬性,設置鼠標放上變?yōu)樾∈?
  2. return的應用:設置一個標記,規(guī)定再圖片過渡時點擊span無效,賦給標記一個false,if判斷當標記為true時,return,不執(zhí)行后面的代碼.第一次點擊span時,值為false,if后面賦給標記一個true,執(zhí)行定時器.再次點擊span時,標記的值為true,return,不執(zhí)行后面的代碼.定時器自動運行,當達到條件,圖片過渡完成到位時,返回給標記一個false,用于再次點擊.

獲取驗證碼

var btn = document.getElementById("btn");
    var timer = null;
    var num = 5;
    btn.onclick = function () {
        btn.disabled = true;
        timer = setInterval(function () {
            var dom = num--;
            if (dom < 1) {
                btn.disabled = false;
                btn.innerHTML = "重新獲取驗證碼";
                num = 5;
                clearInterval(timer);
            }
            else {
                btn.innerHTML = dom + "秒后重新獲取驗證碼";
            }
        }, 1000)
    }
重點:
  1. btn.disabled = false;設置按鈕不可點擊,設置這個時就不用再設置清空定時器
  2. 定時器里的調(diào)用對象是window,所以在定時器里不能用this指向調(diào)用對象,如果要用就寫備份指針var that = this;,再寫that.innerHTML.
    棧區(qū):存放指針
    堆區(qū):存放數(shù)據(jù)
最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,506評論 19 139
  • ¥開啟¥ 【iAPP實現(xiàn)進入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個線程,因...
    小菜c閱讀 7,295評論 0 17
  • <a name='html'>HTML</a> Doctype作用?標準模式與兼容模式各有什么區(qū)別? (1)、<...
    clark124閱讀 3,808評論 1 19
  • 奇談怪論:從容器想到去IOE、去庫存和獨角獸 回顧了架構的變化,如下圖: 云化和容器化給開發(fā),部署和架構帶來了巨大...
    Jeff閱讀 131評論 0 0
  • 我不想要全世界 我只想要你 想要和你天涯海角 想要和你分分秒秒 想要和你擇一城終老 無論何時何地 只要有你就好
    柳若素閱讀 131評論 1 5

友情鏈接更多精彩內(nèi)容