本文實(shí)例為大家分享了js實(shí)現(xiàn)星星閃特效的具體代碼,供大家參考,具體內(nèi)容如下
效果如下
思路:
1、準(zhǔn)備一張星星的圖片
2、創(chuàng)建多個(gè)星星(可以利用for循壞)
3、求出可視網(wǎng)頁的寬高 clientWidth,clientHeight
4、設(shè)置星星的隨機(jī)坐標(biāo) 利用 Math.random()
5、設(shè)置星星的縮放可以用css中的scale
6、設(shè)置星星的縮放延遲頻率 animationDelay
7、給星星加動畫(鼠標(biāo)移動時(shí),星星方法旋轉(zhuǎn))
代碼如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
< 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 隨機(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> |
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.csdn.net/qq_41309350/article/details/114324226