如何用随机数实现漫天星星闪烁的效果呢
今天我们用一个很笨很笨很笨(重要的事情说三遍)的方法来看看能不能实现。
首先我们先分析一下,想要要实现星星的闪烁只要不停的改变它的位置、大小透明度就行了吧,这些东西本来就只设定一个值,那怎么让他们不停改变呢,这就用到JavaScript中的随机数了吧,但是取随机数需要不停刷新才能取到不同的数,这里就要结合定时器一起使用了,让它每隔一段时间就取一个随机数。下面我们结合实际来看一下。
这里提供两种方法,但是意思相同,我们都来看一下,怎么选择,就看你喜欢哪种了。
第一种:
首先我们建立一个html文件,把背景色改成黑色:
:root{
background: black
}
在里面尽量多的插入小星星的图片,并且给不同的id名
<img id = "img1" src="./星星.png" alt="">
<img id = "img2" src="./星星.png" alt="">
我这里是放了60张,现在大概效果是这样的:
然后将星星图片设置成你认为合适的大小
img{
width: 120px;
height: 120px;
}
月亮也设置成大小合适的大小(也可以没有),我这就统一设置成120px*120px了。
想要每颗星星位置不断变化的前提条件要给它们一个绝对定位(相对整个页面),初始位置可以不设置(默认top:0px;left:0px)。
position: absolute;
因为绝对定位不占位,位置又都是默认位置,所以所有图片都叠在了一起:
接下来把月亮放到你认为合适的位置:
#img61{
position: absolute;
top: 10%;
left:70%;
}
这里不是只有一颗星星,是所有星星都叠加在一起了;
html和css的任务就完成了,下面就该写javaScript了;
首先要做的就是建立js文件,然后引入到html中,在js里写一个定时器
setInterval (function(){
},100)
这里的100表示时间,单位是ms,就是让定时器中的内容多长时间执行一次;
位置是由两个值决定的,垂直方向:top、水平方向:left,(当然这里也可以用bottom、right,随意组合,看个人喜好),想要实现更好一点的效果不仅要改变位置,最好还要改变一下大小,所以我们这里要设置三个随机数:
var top1 = Math.floor(Math.random()*801 + 0);
var left1 = Math.floor(Math.random()*1801 + 0);
var area1 = Math.random();
这里的坐标随机数以每个人电脑满屏为准,需要取最大值Max和最小值Min之间的数的方法是:
Math.random() * (Max - Min + 1) + Min
如果是取大于等于0,小于1的话直接是Math.random()就可以了,需要整数取整就可以了,这里的Math.floor就是向下取整。
因为每张图片在一个时间里的位置和大小都不一样,所以每张图片都要设置三个随机数;
然后需要找到每一张图片
document.getElementById("img1")
这个表示去整个文档中去寻找id名为img1的标签
然后重新写入样式
document.getElementById("img1").style = “样式”
我们这有三个属性,并且有三个值都附给了变量,所以要用到字符串拼接:
document.getElementById("img60").style = `top:${top60}px;left:${left60}px;transform:scale(${area60})`
60个代码太冗长,我这里就以两个举例:
setInterval (function(){
var top1 = Math.floor(Math.random()*801 + 0);
var left1 = Math.floor(Math.random()*1801 + 0);
var area1 = Math.random();
document.getElementById("img1").style = `top:${top1}px;left:${left1}px;transform:scale(${area1})`
var top2 = Math.floor(Math.random()*801 + 0);
var left2 = Math.floor(Math.random()*1801 + 0);
var area2 = Math.random();
document.getElementById("img2").style = `top:${top2}px;left:${left2}px;transform:scale(${area2})`
},100)
以此类推,有多少张星星图片就写多少个,因为位置和大小每100ms获取一个数值,也就是说位置和大小每100ms都会随机变,所以就成了类似闪烁的效果。
第二种:
html,css部分都是一模一样,上面我们是改变位置和大小,那参考星星一闪一闪的性质,就可以看出不改变位置,让它一闪一闪也是可以的,那一闪一闪怎么实现呢,改变每个星星的透明度和大小就可以,这样就不用改变位置,让每颗星星自行去闪烁就行;opacity:0;是完全透明,opacity:1;是完全显现,这里我们只要让透明度在0-1之间变化就行。大小变化看个人喜好。所以我们这只需要两个随机数,一个表示透明度,一个表示大小:
var topdiaphaneity1 = Math.random();
var area1 = Math.random();
然后给每个星星一个固定位置(所有星星位置都不一样,让它们分布在屏幕各个角落,并布满屏幕),再将上面两个值分别赋给透明度和大小:
document.getElementById("img1").style = `top:0px;left:0px;transform:scale(${area1});opacity:${diaphaneity1}`
这里还是需要给每个星星都设置不同位置(但是这位置是固定值,只是每颗星星位置不同而已)、不同透明度,不同大小,还是用两个举例:
setInterval (function(){
var topdiaphaneity1 = Math.random();
var area1 = Math.random();
document.getElementById("img1").style = `top:0px;left:0px;transform:scale(${area1});opacity:${diaphaneity1}`
var diaphaneity2 = Math.random();
var area2 = Math.random();
// console.log(area)
document.getElementById("img2").style = `top:0px;left:120px;transform:scale(${area2});opacity:${diaphaneity2}`
},100)
因为每颗星星的透明度和大小都在变化,所以也能营造闪烁效果,用那种就看你觉得哪种效果了。
(ps:我个人比较喜欢第一种呈现出来的效果。)
下面附上第一种的完整代码供参考(这里图方便,css和js都写在里面了):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
:root{
background: black
}
img{
position: absolute;
width: 120px;
height: 120px;
}
#img61{
position: absolute;
top: 10%;
left:70%;
}
</style>
</head>
<body>
<img id = "img1" src="./星星.png" alt="">
<img id = "img2" src="./星星.png" alt="">
<img id = "img3" src="./星星.png" alt="">
<img id = "img4" src="./星星.png" alt="">
<img id = "img5" src="./星星.png" alt="">
<img id = "img6" src="./星星.png" alt="">
<img id = "img7" src="./星星.png" alt="">
<img id = "img8" src="./星星.png" alt="">
<img id = "img9" src="./星星.png" alt="">
<img id = "img10" src="./星星.png" alt="">
<img id = "img11" src="./星星.png" alt="">
<img id = "img12" src="./星星.png" alt="">
<img id = "img13" src="./星星.png" alt="">
<img id = "img14" src="./星星.png" alt="">
<img id = "img15" src="./星星.png" alt="">
<img id = "img16" src="./星星.png" alt="">
<img id = "img17" src="./星星.png" alt="">
<img id = "img18" src="./星星.png" alt="">
<img id = "img19" src="./星星.png" alt="">
<img id = "img20" src="./星星.png" alt="">
<img id = "img21" src="./星星.png" alt="">
<img id = "img22" src="./星星.png" alt="">
<img id = "img23" src="./星星.png" alt="">
<img id = "img24" src="./星星.png" alt="">
<img id = "img25" src="./星星.png" alt="">
<img id = "img26" src="./星星.png" alt="">
<img id = "img27" src="./星星.png" alt="">
<img id = "img28" src="./星星.png" alt="">
<img id = "img29" src="./星星.png" alt="">
<img id = "img30" src="./星星.png" alt="">
<img id = "img31" src="./星星.png" alt="">
<img id = "img32" src="./星星.png" alt="">
<img id = "img33" src="./星星.png" alt="">
<img id = "img34" src="./星星.png" alt="">
<img id = "img35" src="./星星.png" alt="">
<img id = "img36" src="./星星.png" alt="">
<img id = "img37" src="./星星.png" alt="">
<img id = "img38" src="./星星.png" alt="">
<img id = "img39" src="./星星.png" alt="">
<img id = "img40" src="./星星.png" alt="">
<img id = "img41" src="./星星.png" alt="">
<img id = "img42" src="./星星.png" alt="">
<img id = "img43" src="./星星.png" alt="">
<img id = "img44" src="./星星.png" alt="">
<img id = "img45" src="./星星.png" alt="">
<img id = "img46" src="./星星.png" alt="">
<img id = "img47" src="./星星.png" alt="">
<img id = "img48" src="./星星.png" alt="">
<img id = "img49" src="./星星.png" alt="">
<img id = "img50" src="./星星.png" alt="">
<img id = "img51" src="./星星.png" alt="">
<img id = "img52" src="./星星.png" alt="">
<img id = "img53" src="./星星.png" alt="">
<img id = "img54" src="./星星.png" alt="">
<img id = "img55" src="./星星.png" alt="">
<img id = "img56" src="./星星.png" alt="">
<img id = "img57" src="./星星.png" alt="">
<img id = "img58" src="./星星.png" alt="">
<img id = "img59" src="./星星.png" alt="">
<img id = "img60" src="./星星.png" alt="">
<img id = "img61" src="./未标题-1.png" alt="">
<script>
setInterval (function(){
var top1 = Math.floor(Math.random()*801 + 0);
var left1 = Math.floor(Math.random()*1801 + 0);
var area1 = Math.random();
document.getElementById("img1").style = `top:${top1}px;left:${left1}px;transform:scale(${area1})`
var top2 = Math.floor(Math.random()*801 + 0);
var left2 = Math.floor(Math.random()*1801 + 0);
var area2 = Math.random();
document.getElementById("img2").style = `top:${top2}px;left:${left2}px;transform:scale(${area2})`
var top3 = Math.floor(Math.random()*801 + 0);
var left3 = Math.floor(Math.random()*1801 + 0);
var area3 = Math.random();
document.getElementById("img3").style = `top:${top3}px;left:${left3}px;transform:scale(${area3}`
var top4 = Math.floor(Math.random()*801 + 0);
var left4 = Math.floor(Math.random()*1801 + 0);
var area4 = Math.random();
document.getElementById("img4").style = `top:${top4}px;left:${left4}px;transform:scale(${area4}`
var top5 = Math.floor(Math.random()*801 + 0);
var left5 = Math.floor(Math.random()*1801 + 0);
var area5 = Math.random();
document.getElementById("img5").style = `top:${top5}px;left:${left5}px;transform:scale(${area5}`
var top6 = Math.floor(Math.random()*801 + 0);
var left6 = Math.floor(Math.random()*1801 + 0);
var area6 = Math.random();
document.getElementById("img6").style = `top:${top6}px;left:${left6}px;transform:scale(${area6}`
var top7 = Math.floor(Math.random()*801 + 0);
var left7 = Math.floor(Math.random()*1801 + 0);
var area7 = Math.random();
document.getElementById("img7").style = `top:${top7}px;left:${left7}px;transform:scale(${area7})`
var top8 = Math.floor(Math.random()*801 + 0);
var left8 = Math.floor(Math.random()*1801 + 0);
var area8 = Math.random();
document.getElementById("img8").style = `top:${top8}px;left:${left8}px;transform:scale(${area8})`
var top9 = Math.floor(Math.random()*801 + 0);
var left9 = Math.floor(Math.random()*1801 + 0);
var area9 = Math.random();
document.getElementById("img9").style = `top:${top9}px;left:${left9}px;transform:scale(${area9})`
var top10 = Math.floor(Math.random()*801 + 0);
var left10 = Math.floor(Math.random()*1801 + 0);
var area10 = Math.random();
document.getElementById("img10").style = `top:${top10}px;left:${left10}px;transform:scale(${area10})`
var top11 = Math.floor(Math.random()*801 + 0);
var left11 = Math.floor(Math.random()*1801 + 0);
var area11 = Math.random();
document.getElementById("img11").style = `top:${top11}px;left:${left11}px;transform:scale(${area11})`
var top12 = Math.floor(Math.random()*801 + 0);
var left12 = Math.floor(Math.random()*1801 + 0);
var area12 = Math.random();
document.getElementById("img12").style = `top:${top12}px;left:${left12}px;transform:scale(${area12})`
var top13 = Math.floor(Math.random()*801 + 0);
var left13 = Math.floor(Math.random()*1801 + 0);
var area13 = Math.random();
document.getElementById("img13").style = `top:${top13}px;left:${left13}px;transform:scale(${area13})`
var top14 = Math.floor(Math.random()*801 + 0);
var left14 = Math.floor(Math.random()*1801 + 0);
var area14 = Math.random();
document.getElementById("img14").style = `top:${top14}px;left:${left14}px;transform:scale(${area14})`
var top15 = Math.floor(Math.random()*801 + 0);
var left15 = Math.floor(Math.random()*1801 + 0);
var area15 = Math.random();
document.getElementById("img15").style = `top:${top15}px;left:${left15}px;transform:scale(${area15})`
var top16 = Math.floor(Math.random()*801 + 0);
var left16 = Math.floor(Math.random()*1801 + 0);
var area16 = Math.random();
document.getElementById("img16").style = `top:${top16}px;left:${left16}px;transform:scale(${area16})`
var top17 = Math.floor(Math.random()*801 + 0);
var left17 = Math.floor(Math.random()*1801 + 0);
var area17 = Math.random();
document.getElementById("img17").style = `top:${top17}px;left:${left17}px;transform:scale(${area17})`
var top18 = Math.floor(Math.random()*801 + 0);
var left18 = Math.floor(Math.random()*1801 + 0);
var area18 = Math.random();
document.getElementById("img18").style = `top:${top18}px;left:${left18}px;transform:scale(${area18})`
var top19 = Math.floor(Math.random()*801 + 0);
var left19 = Math.floor(Math.random()*1801 + 0);
var area19 = Math.random();
document.getElementById("img19").style = `top:${top19}px;left:${left19}px;transform:scale(${area19})`
var top20 = Math.floor(Math.random()*801 + 0);
var left20 = Math.floor(Math.random()*1801 + 0);
var area20 = Math.random();
document.getElementById("img20").style = `top:${top20}px;left:${left20}px;transform:scale(${area20})`
var top21 = Math.floor(Math.random()*801 + 0);
var left21 = Math.floor(Math.random()*1801 + 0);
var area21 = Math.random();
document.getElementById("img21").style = `top:${top21}px;left:${left21}px;transform:scale(${area21})`
var top22 = Math.floor(Math.random()*801 + 0);
var left22 = Math.floor(Math.random()*1801 + 0);
var area22 = Math.random();
document.getElementById("img22").style = `top:${top22}px;left:${left22}px;transform:scale(${area22})`
var top23 = Math.floor(Math.random()*801 + 0);
var left23 = Math.floor(Math.random()*1801 + 0);
var area23 = Math.random();
document.getElementById("img23").style = `top:${top23}px;left:${left23}px;transform:scale(${area23})`
var top24 = Math.floor(Math.random()*801 + 0);
var left24 = Math.floor(Math.random()*1801 + 0);
var area24 = Math.random();
document.getElementById("img24").style = `top:${top24}px;left:${left24}px;transform:scale(${area24})`
var top25 = Math.floor(Math.random()*801 + 0);
var left25 = Math.floor(Math.random()*1801 + 0);
var area25 = Math.random();
document.getElementById("img25").style = `top:${top25}px;left:${left25}px;transform:scale(${area25})`
var top26 = Math.floor(Math.random()*801 + 0);
var left26 = Math.floor(Math.random()*1801 + 0);
var area26 = Math.random(); document.getElementById("img26").style = `top:${top26}px;left:${left26}px;transform:scale(${area26})`
var top27 = Math.floor(Math.random()*801 + 0);
var left27 = Math.floor(Math.random()*1801 + 0);
var area27 = Math.random();
document.getElementById("img27").style = `top:${top27}px;left:${left27}px;transform:scale(${area27})`
var top28 = Math.floor(Math.random()*801 + 0);
var left28 = Math.floor(Math.random()*1801 + 0);
var area28 = Math.random();
document.getElementById("img28").style = `top:${top28}px;left:${left28}px;transform:scale(${area28})`
var top29 = Math.floor(Math.random()*801 + 0);
var left29 = Math.floor(Math.random()*1801 + 0);
var area29 = Math.random();
document.getElementById("img29").style = `top:${top29}px;left:${left29}px;transform:scale(${area29})`
var top30 = Math.floor(Math.random()*801 + 0);
var left30 = Math.floor(Math.random()*1801 + 0);
var area30 = Math.random();
document.getElementById("img30").style = `top:${top30}px;left:${left30}px;transform:scale(${area30})`
var top31 = Math.floor(Math.random()*801 + 0);
var left31 = Math.floor(Math.random()*1801 + 0);
var area31 = Math.random();
document.getElementById("img31").style = `top:${top31}px;left:${left31}px;transform:scale(${area31})`
var top32 = Math.floor(Math.random()*801 + 0);
var left32 = Math.floor(Math.random()*1801 + 0);
var area32 = Math.random();
document.getElementById("img32").style = `top:${top32}px;left:${left32}px;transform:scale(${area32})`
var top33 = Math.floor(Math.random()*801 + 0);
var left33 = Math.floor(Math.random()*1801 + 0);
var area33 = Math.random();
document.getElementById("img33").style = `top:${top33}px;left:${left33}px;transform:scale(${area33})`
var top34 = Math.floor(Math.random()*801 + 0);
var left34 = Math.floor(Math.random()*1801 + 0);
var area34 = Math.random();
document.getElementById("img34").style = `top:${top34}px;left:${left34}px;transform:scale(${area34})`
var top35 = Math.floor(Math.random()*801 + 0);
var left35 = Math.floor(Math.random()*1801 + 0);
var area35 = Math.random();
document.getElementById("img35").style = `top:${top35}px;left:${left35}px;transform:scale(${area35})`
var top36 = Math.floor(Math.random()*801 + 0);
var left36 = Math.floor(Math.random()*1801 + 0);
var area36 = Math.random();
document.getElementById("img36").style = `top:${top36}px;left:${left36}px;transform:scale(${area36})`
var top37 = Math.floor(Math.random()*801 + 0);
var left37 = Math.floor(Math.random()*1801 + 0);
var area37 = Math.random();
document.getElementById("img37").style = `top:${top37}px;left:${left37}px;transform:scale(${area37})`
var top38 = Math.floor(Math.random()*801 + 0);
var left38 = Math.floor(Math.random()*1801 + 0);
var area38 = Math.random();
document.getElementById("img38").style = `top:${top38}px;left:${left38}px;transform:scale(${area38})`
var top39 = Math.floor(Math.random()*801 + 0);
var left39 = Math.floor(Math.random()*1801 + 0);
var area39 = Math.random();
document.getElementById("img39").style = `top:${top39}px;left:${left39}px;transform:scale(${area39})`
var top40 = Math.floor(Math.random()*801 + 0);
var left40 = Math.floor(Math.random()*1801 + 0);
var area40 = Math.random();
document.getElementById("img40").style = `top:${top40}px;left:${left40}px;transform:scale(${area40})`
var top41 = Math.floor(Math.random()*801 + 0);
var left41 = Math.floor(Math.random()*1801 + 0);
var area41 = Math.random();
document.getElementById("img41").style = `top:${top41}px;left:${left41}px;transform:scale(${area41})`
var top42 = Math.floor(Math.random()*801 + 0);
var left42 = Math.floor(Math.random()*1801 + 0);
var area42 = Math.random();
document.getElementById("img42").style = `top:${top42}px;left:${left42}px;transform:scale(${area42})`
var top43 = Math.floor(Math.random()*801 + 0);
var left43 = Math.floor(Math.random()*1801 + 0);
var area43 = Math.random();
document.getElementById("img43").style = `top:${top43}px;left:${left43}px;transform:scale(${area43})`
var top44 = Math.floor(Math.random()*801 + 0);
var left44 = Math.floor(Math.random()*1801 + 0);
var area44 = Math.random();
document.getElementById("img44").style = `top:${top44}px;left:${left44}px;transform:scale(${area44})`
var top45 = Math.floor(Math.random()*801 + 0);
var left45 = Math.floor(Math.random()*1801 + 0);
var area45 = Math.random();
document.getElementById("img45").style = `top:${top45}px;left:${left45}px;transform:scale(${area45})`
var top46 = Math.floor(Math.random()*801 + 0);
var left46 = Math.floor(Math.random()*1801 + 0);
var area46 = Math.random();
document.getElementById("img46").style = `top:${top46}px;left:${left46}px;transform:scale(${area46})`
var top47 = Math.floor(Math.random()*801 + 0);
var left47 = Math.floor(Math.random()*1801 + 0);
var area47 = Math.random();
document.getElementById("img47").style = `top:${top47}px;left:${left47}px;transform:scale(${area47})`
var top48 = Math.floor(Math.random()*801 + 0);
var left48 = Math.floor(Math.random()*1801 + 0);
var area48 = Math.random();
document.getElementById("img48").style = `top:${top48}px;left:${left48}px;transform:scale(${area48})`
var top49 = Math.floor(Math.random()*801 + 0);
var left49 = Math.floor(Math.random()*1801 + 0);
var area49 = Math.random();
document.getElementById("img49").style = `top:${top49}px;left:${left49}px;transform:scale(${area49})`
var top50 = Math.floor(Math.random()*801 + 0);
var left50 = Math.floor(Math.random()*1801 + 0);
var area50 = Math.random();
document.getElementById("img50").style = `top:${top50}px;left:${left50}px;transform:scale(${area50})`
var top51 = Math.floor(Math.random()*801 + 0);
var left51 = Math.floor(Math.random()*1801 + 0);
var area51 = Math.random();
document.getElementById("img51").style = `top:${top51}px;left:${left51}px;transform:scale(${area51})`
var top52 = Math.floor(Math.random()*801 + 0);
var left52 = Math.floor(Math.random()*1801 + 0);
var area52 = Math.random();
document.getElementById("img52").style = `top:${top52}px;left:${left52}px;transform:scale(${area52})`
var top53 = Math.floor(Math.random()*801 + 0);
var left53 = Math.floor(Math.random()*1801 + 0);
var area53 = Math.random();
document.getElementById("img53").style = `top:${top53}px;left:${left53}px;transform:scale(${area53})`
var top54 = Math.floor(Math.random()*801 + 0);
var left54 = Math.floor(Math.random()*1801 + 0);
var area54 = Math.random();
document.getElementById("img54").style = `top:${top54}px;left:${left54}px;transform:scale(${area54})`
var top55 = Math.floor(Math.random()*801 + 0);
var left55 = Math.floor(Math.random()*1801 + 0);
var area55 = Math.random();
document.getElementById("img55").style = `top:${top55}px;left:${left55}px;transform:scale(${area55})`
var top56 = Math.floor(Math.random()*801 + 0);
var left56 = Math.floor(Math.random()*1801 + 0);
var area56 = Math.random();
document.getElementById("img56").style = `top:${top56}px;left:${left56}px;transform:scale(${area56})`
var top57 = Math.floor(Math.random()*801 + 0);
var left57 = Math.floor(Math.random()*1801 + 0);
var area57 = Math.random();
document.getElementById("img57").style = `top:${top57}px;left:${left57}px;transform:scale(${area57})`
var top58 = Math.floor(Math.random()*801 + 0);
var left58 = Math.floor(Math.random()*1801 + 0);
var area58 = Math.random();
document.getElementById("img58").style = `top:${top58}px;left:${left58}px;transform:scale(${area58})`
var top59 = Math.floor(Math.random()*801 + 0);
var left59 = Math.floor(Math.random()*1801 + 0);
var area59 = Math.random();
document.getElementById("img59").style = `top:${top59}px;left:${left59}px;transform:scale(${area59})`
var top60 = Math.floor(Math.random()*801 + 0);
var left60 = Math.floor(Math.random()*1801 + 0);
var area60 = Math.random();
document.getElementById("img60").style = `top:${top60}px;left:${left60}px;transform:scale(${area60})`
},100)
小编能力有限,存在不足或不全,请大家指出,共同学习与交流。