js實(shí)現(xiàn)星星閃特效
本文實(shí)例為大家分享了js實(shí)現(xiàn)星星閃特效的具體代碼,供大家參考,具體內(nèi)容如下
效果如下
思路:
1、準(zhǔn)備一張星星的圖片2、創(chuàng)建多個(gè)星星(可以利用for循壞)3、求出可視網(wǎng)頁(yè)的寬高 clientWidth,clientHeight4、設(shè)置星星的隨機(jī)坐標(biāo) 利用 Math.random()5、設(shè)置星星的縮放可以用css中的scale6、設(shè)置星星的縮放延遲頻率 animationDelay7、給星星加動(dòng)畫(鼠標(biāo)移動(dòng)時(shí),星星方法旋轉(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. 動(dòng)態(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 隨機(jī)的坐標(biāo) var x = parseInt(Math.random() * screenW); var y = parseInt(Math.random() * screenH); span.style.left = x + ’px’; span.style.top = y + ’px’; // 2.3 隨機(jī)縮放 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>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. Python如何批量生成和調(diào)用變量2. Python調(diào)用接口合并Excel表代碼實(shí)例3. python 爬取京東指定商品評(píng)論并進(jìn)行情感分析4. ajax動(dòng)態(tài)加載json數(shù)據(jù)并詳細(xì)解析5. ASP.NET MVC實(shí)現(xiàn)橫向展示購(gòu)物車6. ASP.Net Core(C#)創(chuàng)建Web站點(diǎn)的實(shí)現(xiàn)7. 如何在Python項(xiàng)目中引入日志8. .net如何優(yōu)雅的使用EFCore實(shí)例詳解9. 通過(guò)CSS數(shù)學(xué)函數(shù)實(shí)現(xiàn)動(dòng)畫特效10. python b站視頻下載的五種版本
