js代码实现随机颜色的小方块

时间:2021-05-26

下面一段代码就是用js实现的随机颜色的小方块,不多说了代码很简单,直接上代码了。

js注释已删 <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF- "> <meta charset="utf- "> <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> * { -webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box; } html { font-size: px; -webkit-tap-highlight-color: transparent; } body { margin: ; padding: ; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: px; line-height: . ; color: # ; background-color: rgba( , , , . ); } .container { overflow: visible; } .main { position: relative; width: %; height: px; margin:auto; } .colorful { overflow: visible; width: %; height: %; } .colorful > a { float: left; display: block; width: px; height: px; zoom: ; } .colorful > a:hover { -webkit-animation:infinite s ease-in-out start-roll; -moz-animation:infinite s ease-in-out start-roll; animation:infinite s ease-in-out start-roll; } @-webkit-keyframes start-roll{ % { -webkit-transform: rotate( deg); -moz-transform: rotate( deg); -ms-transform: rotate( deg); transform: rotate( deg); -webkit-transition: opacity . s linear; transition: opacity . s linear; opacity: . ; filter: alpha(opacity= ); zoom: ; } % { -webkit-transform: rotate( deg); -moz-transform: rotate( deg); -ms-transform: rotate( deg); transform: rotate( deg); -webkit-transition: opacity s linear; transition: opacity s linear; opacity: ; filter: alpha(opacity= ); zoom: ; } % { -webkit-transform: rotate( deg); -moz-transform: rotate( deg); -ms-transform: rotate( deg); transform: rotate( deg); -webkit-transition: opacity . s linear; transition: opacity . s linear; opacity: . ; filter: alpha(opacity= ); zoom: ; } } @-moz-keyframes start-roll{ % { -webkit-transform: rotate( deg); -moz-transform: rotate( deg); -ms-transform: rotate( deg); transform: rotate( deg); -webkit-transition: opacity . s linear; transition: opacity . s linear; opacity: . ; filter: alpha(opacity= ); } % { -webkit-transform: rotate( deg); -moz-transform: rotate( deg); -ms-transform: rotate( deg); transform: rotate( deg); -webkit-transition: opacity s linear; transition: opacity s linear; opacity: . ; filter: alpha(opacity= ); } % { -webkit-transform: rotate( deg); -moz-transform: rotate( deg); -ms-transform: rotate( deg); transform: rotate( deg); -webkit-transition: opacity . s linear; transition: opacity . s linear; opacity: . ; filter: alpha(opacity= ); } } @keyframes start-roll{ % { -webkit-transform: rotate( deg); -moz-transform: rotate( deg); -ms-transform: rotate( deg); transform: rotate( deg); -webkit-transition: opacity . s linear; transition: opacity . s linear; opacity: . ; filter: alpha(opacity= ); } % { -webkit-transform: rotate( deg); -moz-transform: rotate( deg); -ms-transform: rotate( deg); transform: rotate( deg); -webkit-transition: opacity s linear; transition: opacity s linear; opacity: . ; filter: alpha(opacity= ); } % { -webkit-transform: rotate( deg); -moz-transform: rotate( deg); -ms-transform: rotate( deg); transform: rotate( deg); -webkit-transition: opacity . s linear; transition: opacity . s linear; opacity: . ; filter: alpha(opacity= ); } } (function (window) { var document = window.document; function on(elem, evtnm, eventhd) { var onevtnm = 'on' + evtnm; elem[onevtnm] = eventhd; } function grc() { var val = [], i = ; while (++i <= ) { val.push(Math.floor(Math.random() * )); } return 'rgb(' + val.join() + ')'; } function fbc() { var el = document.querySelectorAll('.colorful')[ ], total = Math.floor(el.offsetWidth / ) * Math.floor(el.offsetHeight / ), df = document.createDocumentFragment(), a; while (total-- > ) { a = document.createElement('a'); a.style.backgroundColor = grc(); df.appendChild(a); } el.appendChild(df); } on(window, 'load', function () { fbc(); }); })(window)

以上代码就是用js实现随机颜色小方块的全部内容,需要的朋友可以来参考下。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章