效果图
css
<style>
/*//漂浮物*/
@keyframes mysnow {
0% {
bottom: 100%;
transform: rotate(0deg);
}
100% {
transform: rotate(-30deg);
bottom: -10%;
}
}
@-webkit-keyframes mysnow {
0% {
bottom: 100%;
-webkit-transform: rotate(0deg);
}
100% {
-webkit-transform: rotate(-30deg);
bottom: -10%;
}
}
@-moz-keyframes mysnow {
0% {
bottom: 100%;
-moz-transform: rotate(0deg);
}
100% {
-moz-transform: rotate(-30deg);
bottom: -10%;
}
}
@-ms-keyframes mysnow {
0% {
bottom: 100%;
-ms-transform: rotate(0deg);
}
100% {
-ms-transform: rotate(-30deg);
bottom: -10%;
}
}
@-o-keyframes mysnow {
0% {
bottom: 100%;
-o-transform: rotate(00deg);
}
100% {
-o-transform: rotate(-30deg);
bottom: -10%;
}
}
.roll5 {
position: absolute;
animation: mysnow 20s linear;
-webkit-animation: mysnow 20s linear;
-moz-animation: mysnow 20s linear;
-ms-animation: mysnow 20s linear;
-o-animation: mysnow 20s linear;
}
.roll4 {
position: absolute;
animation: mysnow 12s linear;
-webkit-animation: mysnow 12s linear;
-moz-animation: mysnow 12s linear;
-ms-animation: mysnow 12s linear;
-o-animation: mysnow 12s linear;
}
.roll3 {
position: absolute;
animation: mysnow 8s ease-out;
-webkit-animation: mysnow 8s ease-out;
-moz-animation: mysnow 8s ease-out;
-ms-animation: mysnow 8s ease-out;
-o-animation: mysnow 8s ease-out;
}
.roll {
position: fixed;
z-index: 9999999;
}
</style>
js
<script>
/*漂浮物*/
function snow(left, top, src) {
var img = document.createElement("img");
img.className = "roll roll"+ Math.floor(Math.random() * 3 + 3) + "";
img.src = src;
img.style.width = Math.random()*(1.2-0.6)+0.6 + "rem";
img.style.height = "auto";
img.style.left = left + "px";
img.style.bottom = "100%";
document.getElementsByTagName("body")[0].appendChild(img);
setTimeout(function() {
document.getElementsByTagName("body")[0].removeChild(img);
}, 20000);
}
setInterval(function() {
var left = Math.random() * window.innerWidth;
var top =0;
// var src = "images/gift" + Math.floor(Math.random() * 6 + 1) + ".png";
var src = "https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2742336169,1171788391&fm=26&gp=0.jpg";
snow(left, top, src);
}, 1000);
/*漂浮物end*/
</script>
不需要 div,直接添加在 body 里面了