您的位置:首頁技術文章
文章詳情頁

js實現星星閃特效

瀏覽:87日期:2024-04-04 15:20:06

本文實例為大家分享了js實現星星閃特效的具體代碼,供大家參考,具體內容如下

效果如下

js實現星星閃特效

思路:

1、準備一張星星的圖片2、創建多個星星(可以利用for循壞)3、求出可視網頁的寬高 clientWidth,clientHeight4、設置星星的隨機坐標 利用 Math.random()5、設置星星的縮放可以用css中的scale6、設置星星的縮放延遲頻率 animationDelay7、給星星加動畫(鼠標移動時,星星方法旋轉)

代碼如下

<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. 動態創建星星 for(var i=0; i<150; i++){ // 2.1 創建星星 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>

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: JavaScript
相關文章:
国产综合久久一区二区三区