效果如下

思路呢?
- 準備一張星星的圖片
- 創建多個星星(可以利用for循壞)
- 求出可視網頁的寬高 clientWidth,clientHeight
- 設定星星的隨機坐標 利用 Math.random()
- 設定星星的縮放可以用css中的scale
- 設定星星的縮放延遲頻率 animationDelay
- 給星星加影片(滑鼠移動時,星星方法旋轉)
代碼如下
<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>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/265932.html
標籤:其他
