移動端的 開機動畫javascript
/**** data.js ****/ var imgPath = { logo:["./img/logo/logo1.png", "./img/logo/logo2.png"], pageBg:["./img/bg1.jpg", "./img/bg2.jpg"], loadIcon:["./img/load/ico1.png", "./img/load/ico2.png"] }; // imgPath.logo[1]
// imgPath[logo][1]
<script type="text/javascript" src="./js/data.js"></script> <script type="text/javascript"> var imgArr = []; // 保存項目的圖片路徑 for(var attr in imgPath){ // 每一個數組 imgPath[attr]; // 將 imgPath[attr] 的元素放入 imgArr 中 // 合併 imgArr 和 imgPath[attr] imgArr = imgArr.concat(imgPath[atrr]); } </script>
/****
var arr1 = [1,2];
var arr2 = [3,4,5];
arr1.concat(arr2); // arr1[1,2]; arr2[3,4,5];
arr1 = arr1.concat(arr2);
****/
// 只要 img 圖片加載成功,就會觸發 img.onload 事件 var imgCount; // 項目進度 = 圖片加載成功數 / 要加載的圖片總數 for(var i=0; i<imgArr.length; i++){ var img = new Image(); img.src = imgArr[i]; img.onload = function(){ imgCount += 1; tips.innerHTML = "已加載"+ Math.round(imgCount/imgArr.length*100)+ "%"; }; };
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"/> <title></title> <meta http-equiv="X-UA-Compatible" content="ie=edge"/> <meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0"/> <style rel="stylesheet" type="text/css"> html, body, div, ul, li { margin: 0; padding: 0; } html, body { width: 100%; height: 100%; /* 參照屏幕區域的高 */ min-width: 600px; overflow: hidden; } #wrap { width: 100%; min-height: 100%; background: cadetblue; } #content { width: 100%; padding-bottom: 50px; font-size: 14px; background: darkgrey; } #footer { width: 100%; height: 50px; margin-top: -50px; background: chocolate; text-align: center; line-height: 50px; } /**** Loading Animation ****/ #loading_box { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); transform-style: preserve-3d; perspective: 300px; background-color: #ccc0; } /**** Rotating Tips ****/ #loading_tips { margin-bottom: 10px; font-size: 48px; text-align: center; animation: rotateLetter 3s infinite alternate; } @keyframes rotateLetter { from { transform: rotateY(-30deg); } to { transform: rotateY(30deg); } } /**** Floating Words ****/ #loading_animation { list-style: none; background-color: #bbb0; } #loading_animation li:last-child { margin-right: 0px; } #loading_animation li { float: left; margin-right: 10px; animation: jumpLetter 3s infinite alternate; } @keyframes jumpLetter { from { transform: translateY(0px); } to { transform: translateY(-15px); } } </style> </head> <body> <!-- 模擬屏幕區域 --> <div id="wrap"> <!-- 內容區域 --> <div id="content"> <div id="loading_box"> <div id="loading_tips"> </div> <ul id="loading_animation"> <li>L</li> <li>o</li> <li>a</li> <li>d</li> <li>i</li> <li>n</li> <li>g</li> </ul> </div> </div> </div> <!-- 底部區域 --> <div id="footer"> </div> <script type="text/javascript" src="./js/index.js"></script> <script type="text/javascript" src="./js/data.js"></script> <script type="text/javascript"> var lis = document.querySelectorAll("#loading_animation li"); var colorArr = ["red", "green", "blue"]; var i = 0; for(i=0; i<lis.length; i++){ lis[i].style.color = colorArr[i%3]; lis[i].style.animationDelay = i*100+"ms"; } var loadingTips = document.getElementById('loading_tips'); //建立數組:保存項目的圖片 var imgArr = []; //保存圖片加載成功的個數 var index = 0; //把data中圖片路徑塞到imgArr中 for(var i in imgData){ //枚舉對象上的每個屬性 imgArr = imgArr.concat(imgData[i]) }; //項目進度 = 圖片加載成功的個數 / 圖片總數 for(var i=0;i<imgArr.length;i++){ //建立img var img = new Image();//建立圖片對象 img.src = imgArr[i]; //圖片加載成功 img.onload = function () { index++; console.log(Math.round(index/imgArr.length*100)); loadingTips.innerHTML = Math.round(index/imgArr.length*100)+'%'; }; } </script> </body> </html>