javascript實(shí)現(xiàn)簡(jiǎn)單加載隨機(jī)色方塊
用碎片加載小方塊實(shí)現(xiàn)簡(jiǎn)單的隨機(jī)色塊,直接上代碼:
效果圖:

具體代碼:
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <meta charset="utf-8"> <title>koringz</title> <link rel="stylesheet" href="css/demo.css"> </head> <body> <div class="container"> <div class="main"> <div class="colorful"></div> </div> </div> </body> </html>
css代碼:
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
html {
font-size: 10px;
-webkit-tap-highlight-color: transparent;
}
body {
margin: 0;
padding: 0;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
font-size: 14px;
line-height: 1.42857143;
color: #333;
background-color: rgba(211,232,132,0.8);
}
.container {
overflow: visible;
}
.main {
position: relative;
width: 90%;
height:200px;
margin:auto;
}
.colorful {
overflow: visible;
width: 100%;
height: 100%;
}
.colorful > a {
float: left;
display: block;
width: 50px;
height: 50px;
zoom:2;
}
.colorful > a:hover {
-webkit-animation:infinite 2s ease-in-out start-roll;
-moz-animation:infinite 2s ease-in-out start-roll;
animation:infinite 2s ease-in-out start-roll;
}
@-webkit-keyframes start-roll{
0% {
-webkit-transform: rotate(0deg); /*chrome*/
-moz-transform: rotate(0deg); /*火狐*/
-ms-transform: rotate(0deg); /*IE*/
transform: rotate(0deg);
-webkit-transition: opacity .3s linear;
transition: opacity .3s linear;
opacity: .5;
filter: alpha(opacity=50);
zoom:1;
}
50% {
-webkit-transform: rotate(180deg); /*chrome*/
-moz-transform: rotate(180deg); /*火狐*/
-ms-transform: rotate(180deg); /*IE*/
transform: rotate(180deg);
-webkit-transition: opacity 1s linear;
transition: opacity 1s linear;
opacity: 1;
filter: alpha(opacity=100);
zoom:2;
}
100% {
-webkit-transform: rotate(360deg); /*chrome*/
-moz-transform: rotate(360deg); /*火狐*/
-ms-transform: rotate(360deg); /*IE*/
transform: rotate(360deg);
-webkit-transition: opacity .3s linear;
transition: opacity .3s linear;
opacity: .5;
filter: alpha(opacity=50);
zoom:1;
}
}
@-moz-keyframes start-roll{
0% {
-webkit-transform: rotate(0deg); /*chrome*/
-moz-transform: rotate(0deg); /*火狐*/
-ms-transform: rotate(0deg); /*IE*/
transform: rotate(0deg);
-webkit-transition: opacity .3s linear;
transition: opacity .3s linear;
opacity: .5;
filter: alpha(opacity=50);
}
50% {
-webkit-transform: rotate(180deg); /*chrome*/
-moz-transform: rotate(180deg); /*火狐*/
-ms-transform: rotate(180deg); /*IE*/
transform: rotate(180deg);
-webkit-transition: opacity 1s linear;
transition: opacity 1s linear;
opacity: .5;
filter: alpha(opacity=50);
}
100% {
-webkit-transform: rotate(360deg); /*chrome*/
-moz-transform: rotate(360deg); /*火狐*/
-ms-transform: rotate(360deg); /*IE*/
transform: rotate(360deg);
-webkit-transition: opacity .3s linear;
transition: opacity .3s linear;
opacity: .5;
filter: alpha(opacity=50);
}
}
@keyframes start-roll{
0% {
-webkit-transform: rotate(0deg); /*chrome*/
-moz-transform: rotate(0deg); /*火狐*/
-ms-transform: rotate(0deg); /*IE*/
transform: rotate(0deg);
-webkit-transition: opacity .3s linear;
transition: opacity .3s linear;
opacity: .5;
filter: alpha(opacity=50);
}
50% {
-webkit-transform: rotate(180deg); /*chrome*/
-moz-transform: rotate(180deg); /*火狐*/
-ms-transform: rotate(180deg); /*IE*/
transform: rotate(180deg);
-webkit-transition: opacity 1s linear;
transition: opacity 1s linear;
opacity: .5;
filter: alpha(opacity=50);
}
100% {
-webkit-transform: rotate(360deg); /*chrome*/
-moz-transform: rotate(360deg); /*火狐*/
-ms-transform: rotate(360deg); /*IE*/
transform: rotate(360deg);
-webkit-transition: opacity .3s linear;
transition: opacity .3s linear;
opacity: .5;
filter: alpha(opacity=50);
}
}
js代碼:
(function (window) {
var document = window.document;
function on(elem, evtnm, eventhd) {
var onevtnm = 'on' + evtnm;
elem[onevtnm] = eventhd;
}
function grc() {
var val = [], i = 0;
while (++i <= 3) {
val.push(Math.floor(Math.random() * 255));
}
return 'rgb(' + val.join() + ')';
}
function fbc() {
var el = document.querySelectorAll('.colorful')[0],
total = Math.floor(el.offsetWidth / 50) * Math.floor(el.offsetHeight / 50),
df = document.createDocumentFragment(),
a;
while (total-- > 0) {
a = document.createElement('a');
a.style.backgroundColor = grc();
df.appendChild(a);
}
el.appendChild(df);
}
on(window, 'load', function () {
fbc();
});
})(window)
希望本文所述對(duì)大家學(xué)習(xí)javascript程序設(shè)計(jì)有所幫助。
- JS延遲加載(setTimeout) JS最后加載
- Jquery.LazyLoad.js修正版下載,實(shí)現(xiàn)圖片延遲加載插件
- jquery getScript動(dòng)態(tài)加載JS方法改進(jìn)詳解
- JS實(shí)現(xiàn)圖片預(yù)加載無(wú)需等待
- js異步加載的三種解決方案
- 如何讓頁(yè)面加載完成后執(zhí)行js
- 頁(yè)面加載完成后再執(zhí)行JS的jquery寫(xiě)法以及區(qū)別說(shuō)明
- 使用jQuery動(dòng)態(tài)加載js腳本文件的方法
- 兩種方法實(shí)現(xiàn)在HTML頁(yè)面加載完畢后運(yùn)行某個(gè)js
- 頁(yè)面加載完后自動(dòng)執(zhí)行一個(gè)方法的js代碼
相關(guān)文章
Bootstrap 表單驗(yàn)證formValidation 實(shí)現(xiàn)表單動(dòng)態(tài)驗(yàn)證功能
這篇文章主要介紹了Bootstrap 表單驗(yàn)證formValidation 實(shí)現(xiàn)表單動(dòng)態(tài)驗(yàn)證功能,需要的朋友可以參考下2017-05-05
Microsfot .NET Framework4.0框架 安裝失敗的解決方法
今天在安裝.NET Framework 4.0,安裝了半天結(jié)果提示未安裝成功,提示原因是服務(wù)未啟動(dòng)了,error code: (0x80070643), "安裝時(shí)發(fā)生嚴(yán)重錯(cuò)誤 "2013-08-08
解決layui數(shù)據(jù)表格Date日期格式的回顯Object的問(wèn)題
今天小編就為大家分享一篇解決layui數(shù)據(jù)表格Date日期格式的回顯Object的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-09-09
JavaScript指定字段排序方法sortFun函數(shù)
這篇文章主要介紹了JavaScript指定字段排序方法sortFun函數(shù),數(shù)組的排序方法是sort,但是它并不支持根據(jù)指定的字段進(jìn)行排序,而sortFun可以根據(jù)指定的字段進(jìn)行排序,需要的朋友可以參考下2023-05-05
bootstrap daterangepicker漢化以及擴(kuò)展功能
這篇文章主要為大家詳細(xì) 介紹了bootstrap daterangepicker漢化以及擴(kuò)展功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
AjaxUpLoad.js實(shí)現(xiàn)文件上傳功能
這篇文章主要為大家詳細(xì)介紹了AjaxUpLoad.js實(shí)現(xiàn)文件上傳功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03
javascript生成/解析dom的CDATA類(lèi)型的字段的代碼
javascript生成/解析dom的CDATA類(lèi)型的字段的代碼...2007-04-04
微信小程序云開(kāi)發(fā)修改云數(shù)據(jù)庫(kù)中的數(shù)據(jù)方法
這篇文章主要介紹了微信小程序云開(kāi)發(fā)修改云數(shù)據(jù)庫(kù)中的數(shù)據(jù)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05

