star.vue 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <template>
  2. <div class="stars">
  3. <div class="star" v-for="(item, index) in starsCount" :key="index" ref="star"></div>
  4. </div>
  5. </template>
  6. <script>
  7. export default {
  8. name: 'StarBackground',
  9. props: {},
  10. data() {
  11. return {
  12. starsCount: 1500,
  13. distance: 1000
  14. }
  15. },
  16. mounted() {
  17. this.initStars()
  18. },
  19. methods: {
  20. initStars() {
  21. let starArr = this.$refs.star
  22. starArr.forEach(item => {
  23. let speed = 0.2 + (Math.random() * 1)
  24. let thisDistance = this.distance + (Math.random() * 300)
  25. item.style.transformOrigin = `0 0 ${thisDistance}px`
  26. item.style.transform = `translate3d(0, 0, -${thisDistance}px) rotateY(${(Math.random() * 360)}deg) rotateX(${(Math.random() * -50)}deg) scale(${speed}, ${speed})`
  27. })
  28. }
  29. }
  30. }
  31. </script>
  32. <style scoped lang="scss">
  33. @keyframes rotate {
  34. 0% {
  35. transform: perspective(600px) rotateZ(20deg) rotateX(-40deg) rotateY(0);
  36. }
  37. 100% {
  38. transform: perspective(600px) rotateZ(20deg) rotateX(-40deg) rotateY(-360deg);
  39. }
  40. }
  41. .stars {
  42. transform: perspective(500px);
  43. transform-style: preserve-3d;
  44. position: absolute;
  45. perspective-origin: 50% 100%;
  46. left: 50%;
  47. animation: rotate 90s infinite linear;
  48. bottom: -200px;
  49. }
  50. .star {
  51. width: 3px;
  52. height: 2px;
  53. background: #39ffe8;
  54. position: absolute;
  55. top: 0;
  56. left: 0;
  57. backface-visibility: hidden;
  58. border-radius: 2px;
  59. box-shadow: 0 0 5px rgba($color: #00f6ff, $alpha: 1);
  60. // box-shadow: 0 0 5px rgba(81, 203, 238, 1);
  61. // -webkit-box-shadow: 0 0 5px rgba(81, 203, 238, 1);
  62. // -moz-box-shadow: 0 0 5px rgba(81, 203, 238, 1);
  63. }
  64. </style>