js實現(xiàn)星星閃特效
本文實例為大家分享了js實現(xiàn)星星閃特效的具體代碼,供大家參考,具體內(nèi)容如下
效果如下
思路:
1、準備一張星星的圖片2、創(chuàng)建多個星星(可以利用for循壞)3、求出可視網(wǎng)頁的寬高 clientWidth,clientHeight4、設(shè)置星星的隨機坐標 利用 Math.random()5、設(shè)置星星的縮放可以用css中的scale6、設(shè)置星星的縮放延遲頻率 animationDelay7、給星星加動畫(鼠標移動時,星星方法旋轉(zhuǎn))
代碼如下
<style> *{ margin: 0; padding: 0; list-style: none; } body{ background-color: #000; } span{ width: 30px; height: 30px; background: url('../images_js/star.png') no-repeat; position: absolute; background-size:100% 100%; animation: flash 1s alternate infinite; } @keyframes flash { 0%{opacity: 0;} 100%{opacity: 1;} } span:hover{ transform: scale(3, 3) rotate(180deg) !important; transition: all 1s; } </style></head><body><script> window.onload = function () { // 1. 求出屏幕的尺寸 var screenW = document.documentElement.clientWidth; var screenH = document.documentElement.clientHeight; // 2. 動態(tài)創(chuàng)建星星 for(var i=0; i<150; i++){ // 2.1 創(chuàng)建星星 var span = document.createElement(’span’); document.body.appendChild(span); // 2.2 隨機的坐標 var x = parseInt(Math.random() * screenW); var y = parseInt(Math.random() * screenH); span.style.left = x + ’px’; span.style.top = y + ’px’; // 2.3 隨機縮放 var scale = Math.random() * 1.5; span.style.transform = ’scale(’+ scale + ’, ’ + scale + ’)’; // 2.4 頻率 var rate = Math.random() * 1.5; span.style.animationDelay = rate + ’s’; } }</script>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
