js圖片預先載入---for與onload事件,圖片資料模型到介面的單項綁定,使用interval類比迴圈操作,

來源:互聯網
上載者:User

js圖片預先載入---for與onload事件,圖片資料模型到介面的單項綁定,使用interval類比迴圈操作,

<!----><html>    <head>        <title></title>        <meta charset="utf-8">        <script src="imgloader.js"></script>    </head>    <body id ="body">        <!--            <button id="stop">stop</button>        -->    </body>    <script>    //1.使用timer類比迴圈操作    /*    var display = true;     var timer = setInterval(function(){        if(!display){            clearInterval(timer);        }        console.log("hello");    },100);        var stop = document.getElementById("stop");        stop.onclick = function(){            display = false;           }    */    //2.img 對象 與imgDom 對象的差別    /*    var img = new Image();        img.src = "1.jpg";    var imgDom = document.createElement("img");        imgDom.src = "1.jpg";    var body = document.getElementById("body");        body.appendChild(img);        body.appendChild(imgDom);    //二者在加入dom樹後效果一致    //加入後動態改變src又如何        //img.src = "2.jpg";        //imgDom.src = "2.jpg";    //都能夠實現:資料模型--->畫面的單項綁定    */    //3.onload事件的機制,它是不是會被覆蓋    /*    for(var i=1;i<=16;i++){        var url = i+".jpg"        var img = new Image();        img.src = url;        img.onload = function(){            console.log(img.width);        }    }    */    var i = 1;    var timer = setInterval(function(){        var url = i+".jpg"        var img = new Image();        img.src = url;        if(img.complete == true){            console.log(img.width);            i++;            if(i>16){                clearInterval(timer);            }        }    },30);    //結論:迴圈做法只有最後一張圖能列印出來    //使用timer做法可以正常進行    </script></html>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.