CSS随机动画的问题
这里有个简约但好看的CSS动画背景。Pure Css Animated Background (codepen.io)
效果是一个个多边形上浮。
但多边形的尺寸、位置(Left)和延时等属性,都是写死了的。
我希望将多边形的各项属性随机,且每次动画迭代都不一样,即不是循环播放上一次的设定。
我尝试这样写,但可见肯定是不行的,请各位大佬指教。感谢。
<template>
<div>
<div class="area">
<ul class="circles">
<li v-for="i in 10" :style="objElementStyle"></li>
</ul>
</div>
<div class="context">
<h1>Pure Css Animated Background</h1>
</div>
</div>
</template>
<script setup>
import { reactive } from 'vue'
const objElementStyle = reactive({
left: Math.random() * 100 + '%',
width: Math.random() * 100 + 'px',
height: Math.random() * 100 + 'px',
'animation-delay': '0s',
'animation-duration': '11s'
})
</script>
<style>
* {
margin: 0px;
padding: 0px;
}
body {
font-family: 'Exo', sans-serif;
}
.area {
background: #4e54c8;
background: -webkit-linear-gradient(to left, #8f94fb, #4e54c8);
position: fixed;
left: 0;
right: 0;
top: 0;
bottom: 0;
}
.circles {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
overflow: hidden;
}
.circles li {
position: absolute;
display: block;
list-style: none;
width: 20px;
height: 20px;
background: rgba(255, 255, 255, 0.2);
animation: animate 25s linear infinite;
bottom: -150px;
}
@keyframes animate {
0% {
transform: translateY(0) rotate(0deg);
opacity: 1;
border-radius: 0;
}
100% {
transform: translateY(-1000px) rotate(720deg);
opacity: 0;
border-radius: 50%;
}
}
</style>
这个不好搞 css动画进入页面的时候 参数就设定好了 不刷新 参数是不会发生改变的你就算给他随机了他也不会去读取新的参数 本帖最后由 jiujiukeji 于 2023-1-1 21:11 编辑
:root {
--ch1: 300px;
#这里需要你自己多定义几个变量~
}
.circles li:nth-child(1){
left: 25%;
width: var(--ch1);
height: var(--ch1);
animation-delay: 0s;
}
setInterval(function () {
var r=randomNum(20,300);
console.log(r)
changePx(r);
}, 2000);
function changePx(r) {
document.documentElement.style.setProperty('ch1',r+"px" );
}
//生成从minNum到maxNum的随机数
function randomNum(minNum,maxNum){
switch(arguments.length){
case 1:
return parseInt(Math.random()*minNum+1,10);
break;
case 2:
return parseInt(Math.random()*(maxNum-minNum+1)+minNum,10);
break;
default:
return 0;
break;
}
}
能达到随机的效果但是怎么写入参数 就得你去想办法了~~
<template>
<div>
<div class="area">
<ul class="circles" @animationend="fn">
<li
v-for="(i, index) in elements"
:key="index"
:style=", animateName]"
></li>
</ul>
</div>
<div class="context">
<h1>Pure Css Animated Background</h1>
</div>
</div>
</template>
<script setup>
import { reactive, ref } from "vue";
const elements = reactive(Array.from({ length: 10 }));
const styleList = reactive([]);
const animateName = reactive({
"--animation": "animate",
});
const createStyle = () => {
return {
left: Math.random() * 100 + "%",
width: Math.random() * 100 + "px",
height: Math.random() * 100 + "px",
"animation-delay": "0s",
"animation-duration": "11s",
};
};
const setStyle = () => {
for (let i = 0; i < elements.length; i++) {
styleList = createStyle();
}
};
setStyle();
const toggleAnimation = () => {
if (animateName["--animation"] === "animate") {
animateName["--animation"] = "animate1";
} else {
animateName["--animation"] = "animate";
}
};
const count = ref(0);
const fn = () => {
count.value++;
if (count.value === elements.length) {
count.value = 0;
setStyle();
toggleAnimation();
}
};
</script>
<style>
.area {
background: #4e54c8;
background: -webkit-linear-gradient(to left, #8f94fb, #4e54c8);
position: fixed;
left: 0;
right: 0;
top: 0;
bottom: 0;
}
.circles {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
overflow: hidden;
}
.circles li {
--animation: animate;
position: absolute;
display: block;
list-style: none;
width: 20px;
height: 20px;
background: rgba(255, 255, 255, 0.2);
animation: var(--animation) 25s linear;
bottom: -150px;
}
@keyframes animate {
0% {
transform: translateY(0) rotate(0deg);
opacity: 1;
border-radius: 0;
}
100% {
transform: translateY(-1000px) rotate(720deg);
opacity: 0;
border-radius: 50%;
}
}
@keyframes animate1 {
0% {
transform: translateY(0) rotate(0deg);
opacity: 1;
border-radius: 0;
}
100% {
transform: translateY(-1000px) rotate(720deg);
opacity: 0;
border-radius: 50%;
}
}
</style>
本帖最后由 jiujiukeji 于 2023-1-1 22:19 编辑
<div class="context">
<h1>Pure Css Animated Background</h1>
</div>
<div class="area" >
<ul class="circles">
<li id="circles_1">1</li>
<li id="circles_2">2</li>
<li id="circles_3">3</li>
<li id="circles_4">4</li>
<li id="circles_5">5</li>
<li id="circles_6">6</li>
<li id="circles_7">7</li>
<li id="circles_8">8</li>
<li id="circles_9">9</li>
<li id="circles_10">10</li>
</ul>
</div >
@import url('https://fonts.googleapis.com/css?family=Exo:400,700');
*{
margin: 0px;
padding: 0px;
}
body{
font-family: 'Exo', sans-serif;
}
.context {
width: 100%;
position: absolute;
top:50vh;
}
.context h1{
text-align: center;
color: #fff;
font-size: var(--ch1);
}
.area{
background: #4e54c8;
background: -webkit-linear-gradient(to left, #8f94fb, #4e54c8);
width: 100%;
height:100vh;
}
.circles{
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
overflow: hidden;
}
.circles li{
position: absolute;
display: block;
list-style: none;
width: 20px;
height: 20px;
background: rgba(255, 255, 255, 0.2);
animation: animate 25s linear infinite;
bottom: -150px;
}
:root {
--ch1: 80px;
--ch2: 20px;
--ch3: 20px;
--ch4: 60px;
--ch5: 20px;
--ch6: 110px;
--ch7: 150px;
--ch8: 25px;
--ch9: 15px;
--ch10: 150px;
}
#circles_1{
left: 25%;
width: var(--ch1);
height: var(--ch1);
animation-delay: 0s;
}
#circles_2{
left: 10%;
width:var(--ch2);
height:var(--ch2);
animation-delay: 2s;
animation-duration: 12s;
}
#circles_3{
left: 70%;
width:var(--ch3);
height:var(--ch3);
animation-delay: 4s;
animation-duration: 8s;
}
#circles_4{
left: 40%;
width:var(--ch4);
height:var(--ch4);
animation-delay: 0s;
animation-duration: 18s;
}
#circles_5{
left: 65%;
width:var(--ch5);
height:var(--ch5);
animation-delay: 0s;
animation-duration: 10s;
}
#circles_6{
left: 75%;
width:var(--ch6);
height:var(--ch6);
animation-delay: 3s;
animation-duration: 12s;
}
#circles_7{
left: 35%;
width:var(--ch7);
height:var(--ch7);
animation-delay: 7s;
animation-duration: 15s;
}
#circles_8{
left: 50%;
width:var(--ch8);
height:var(--ch8);
animation-delay: 15s;
animation-duration: 45s;
}
#circles_9{
left: 20%;
width:var(--ch9);
height:var(--ch9);
animation-delay: 2s;
animation-duration: 35s;
}
#circles_10{
left: 85%;
width:var(--ch10);
height:var(--ch10);
animation-delay: 0s;
animation-duration: 11s;
}
@keyframes animate {
0%{
transform: translateY(0) rotate(0deg);
opacity: 1;
border-radius: 0;
}
100%{
transform: translateY(-1000px) rotate(720deg);
opacity: 0;
border-radius: 50%;
}
}
/*
Background Gradients From --
https://uigradients.com
*/
//5s
setInterval(function () {
var r2=randomNum(20,80);
changeCh2(r2);
}, 14000);
//8s
setInterval(function () {
var r3=randomNum(20,80);
changeCh3(r3);
}, 12000);
//18s
setInterval(function () {
var r4=randomNum(60,120);
changeCh4(r4);
}, 18000);
//10s
setInterval(function () {
var r5=randomNum(20,80);
changeCh5(r5);
}, 10000);
//15s
setInterval(function () {
var r6=randomNum(110,170);
changeCh6(r6);
}, 15000);
//19s
setInterval(function () {
var r7=randomNum(150,210);
changeCh7(r7);
}, 22000);
//60s
setInterval(function () {
var r8=randomNum(25,80);
changeCh8(r8);
}, 60000);
//37s
setInterval(function () {
var r9=randomNum(15,75);
changeCh9(r9);
}, 37000);
//11s
setInterval(function () {
var r10=randomNum(150,210);
changeCh10(r10);
}, 11000);
//circles_2
function changeCh2(r) {
document.getElementById("//circles_2").style.width=r+"px";
document.getElementById("circles_2").style.height=r+"px";
}
//circles_3
function changeCh3(r) {
document.getElementById("circles_3").style.width=r+"px";
document.getElementById("circles_3").style.height=r+"px";
}
//circles_4
function changeCh4(r) {
document.getElementById("circles_4").style.width=r+"px";
document.getElementById("circles_4").style.height=r+"px";
}
//circles_5
function changeCh5(r) {
document.getElementById("circles_5").style.width=r+"px";
document.getElementById("circles_5").style.height=r+"px";
}
//circles_6
function changeCh6(r) {
document.getElementById("circles_6").style.width=r+"px";
document.getElementById("circles_6").style.height=r+"px";
}
//circles_7
function changeCh7(r) {
document.getElementById("circles_7").style.width=r+"px";
document.getElementById("circles_7").style.height=r+"px";
}
//circles_8
function changeCh8(r) {
document.getElementById("circles_8").style.width=r+"px";
document.getElementById("circles_8").style.height=r+"px";
}
//circles_9
function changeCh9(r) {
document.getElementById("circles_9").style.width=r+"px";
document.getElementById("circles_9").style.height=r+"px";
}
//circles_10
function changeCh10(r) {
document.getElementById("circles_10").style.width=r+"px";
document.getElementById("circles_10").style.height=r+"px";
}
//生成从minNum到maxNum的随机数
function randomNum(minNum,maxNum){
switch(arguments.length){
case 1:
return parseInt(Math.random()*minNum+1,10);
break;
case 2:
return parseInt(Math.random()*(maxNum-minNum+1)+minNum,10);
break;
default:
return 0;
break;
}
}
用笨办法给你实现了你自己修改修改~~
如果你们用的VUE框架 ~~~我就啥也不说了~{:1_908:} 感觉要通过js配合才能实现 肯定要用JS啊,因为你要求每次都不一样,可以参考一下5楼的代码,就设置定时器,用js读取属性直接修改
页:
[1]