vue2流星雨(可调角度)

新建StarBackground.vue组件

打开组件注释部分可以随机颜色

复制代码
<template>
  <div class="rain">
    <div
      v-for="(item,index) in rainNumber"
      :key="index"
      class="rain-item"
      ref="rain-item"
      :style="`transform:rotate(${rotateDeg}deg);width:${w}px;height:${h}px;`"
    >
      <div ref="line-item" class="line" :style="`animationDelay:${index*100}ms`"></div>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      timer1: null
    }
  },
  props: {
    rainNumber: {
      type: Number,
      default: 0
    },
    rotateDeg: {
      type: Number,
      default: 0
    },
    w: {
      type: Number,
      default: 0
    },
    h: {
      type: Number,
      default: 0
    }
  },
  mounted () {
    this.randomRain()
  },
  methods: {
    randomRain () {
      let rainArr = this.$refs['rain-item']
      // let lineArr = this.$refs['line-item']
      rainArr.forEach((item) => {
        item.style.top = Math.floor(Math.random() * 0 + 750) + 'px'
        item.style.left = Math.floor(Math.random() * 2000 + 1) + 'px'
      })
      // let n = 0
     
      // lineArr.forEach((item) => {
       
      //   item.style.background = this.generateRandomColors(40)[n++]
      // })
    },
    generateRandomColor() {  
      const randomRed = Math.floor(Math.random() * 256);  // 生成一个0到255之间的随机整数,代表红色通道的值。
      const randomGreen = Math.floor(Math.random() * 256);  // 生成一个0到255之间的随机整数,代表绿色通道的值。
      const randomBlue = Math.floor(Math.random() * 256);  // 生成一个0到255之间的随机整数,代表蓝色通道的值。
      return `rgb(${randomRed}, ${randomGreen}, ${randomBlue})`;  // 返回一个字符串,格式为rgb(red, green, blue)
    },
    generateRandomColors(n) {  
      const colors = [];  
      for (let i = 0; i < n; i++) {  // 循环n次,每次调用generateRandomColor函数生成一个颜色值,并将其添加到colors数组中。
        colors.push(this.generateRandomColor());  
      }  
      return colors;  
    }
  },
    
    
  beforeDestroy () {
    clearInterval(this.timer1)
  }
}
</script>

<style lang='scss' scoped>
.rain {
  width: 100%;
  height: 100vh;
  position: relative;
  .rain-item {
    position: absolute;
    width: 2px;
    height: 30px;
    display: inline-block;
    .line {
      animation: raining 2s infinite linear;
      position: absolute;
      content: "";
      top: -30px;
      left: 0;
      width: 100%;
      height: 100%;
      box-shadow: 0px 5px 20px 0px #fcfcfc;
   /*   background: linear-gradient(   //单独一种颜色
        to top,
        rgb(18, 142, 205),
        rgba(18, 142, 205, 0.1)
      );*/
    }
  }
}
@keyframes raining {
  0% {
    //transform: translateY(100%);
    top: -0;
    opacity: 0;
  }
  10% {
    top: 100px;
    opacity: 0.3;
  }
  25% {
    top: 200px;
    opacity: 0.5;
  }
  50% {
    top: 400px;
    opacity: 0.8;
  }
  75% {
    top: 600px;
    opacity: 0.5;
  }
  100% {
    top: 800px;
    opacity: 0.3;
    transform: translateY(-100%);
  }
}
</style>

在需要使用的页面引用

复制代码
<style scoped>
.login-container {
  height: 100%;
  width: 50%;
  overflow: hidden;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: black;
}
</style>
<template>
  <div class="box" style="width: 100%;height: 100vh;">
    <div class="login-container">
      <!-- rainNumber:线条数量 rotateDeg:旋转角度  w:线条宽度 h:线条高度-->
        <rain :rainNumber="40" :rotateDeg="180" :w="2" :h="140"></rain>
      </div>
  </div>
</template>
 
<script>

import rain from '@/components/StarBackground.vue';
export default {
  components: {
    rain
  },
};
</script>
相关推荐
kite01213 小时前
浏览器工作原理06 [#]渲染流程(下):HTML、CSS和JavaScript是如何变成页面的
javascript·css·html
超级土豆粉6 小时前
CSS3 的特性
前端·css·css3
前端小白从0开始9 小时前
Vue3项目实现WPS文件预览和内容回填功能
前端·javascript·vue.js·html5·wps·文档回填·文档在线预览
超级土豆粉11 小时前
CSS 预处理器与工具
前端·css
lljss202020 小时前
html文字红色粗体,闪烁渐变动画效果,中英文切换版本
css·html·css3
木木夕酱1 天前
前端响应式网站编写套路
css·react.js
用户26834842239591 天前
前端换肤功能最佳实践:从基础实现到高级优化
前端·css
灏瀚星空2 天前
用HTML5 Canvas打造交互式心形粒子动画:从基础到优化实战
前端·html·html5
蓝婷儿2 天前
第二章支线八 ·CSS终式:Tailwind与原子风暴
前端·css
Java永无止境2 天前
Web前端基础:HTML-CSS
java·前端·css·html·javaweb