y-Barrage.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <template>
  2. <view class="l-barrage">
  3. <block v-for="(item,index) in items" :key="index">
  4. <!-- #ifdef H5 -->
  5. <text v-if="item.display" class="aon" :style="{top: `${item.top}%`,color: item.color}">
  6. {{item.text}}
  7. </text>
  8. <!-- #endif -->
  9. <!-- #ifndef H5 -->
  10. <text v-if="item.display" class="aon" :style="{top: `${item.top}%`,color: item.color,
  11. animation: `mymove ${Number(item.time)}s linear forwards`
  12. }">
  13. {{item.text}}
  14. </text>
  15. <!-- #endif -->
  16. </block>
  17. </view>
  18. </template>
  19. <script>
  20. let cycle;
  21. export default {
  22. name: 'l-barrage',
  23. props: {
  24. minTime: {
  25. type: Number,
  26. default: 4
  27. },
  28. maxTime: {
  29. type: Number,
  30. default: 9
  31. },
  32. minTop: {
  33. type: Number,
  34. default: 0
  35. },
  36. maxTop: {
  37. type: Number,
  38. default: 40
  39. }
  40. },
  41. data() {
  42. return {
  43. items: [],
  44. }
  45. },
  46. methods: {
  47. getRandomColor() {
  48. let rgb = []
  49. for (let i = 0; i < 3; ++i) {
  50. let color = Math.floor(Math.random() * 256).toString(16)
  51. color = color.length == 1 ? '0' + color : color
  52. rgb.push(color)
  53. }
  54. return '#' + rgb.join('')
  55. },
  56. add(text = '', time = Math.ceil(Math.floor(Math.random() * (this.maxTime - this.minTime + 1) + this.minTime))) {
  57. this.items.push({
  58. text,
  59. time,
  60. top: Math.ceil(Math.random() * (this.maxTop - this.minTop + 1) + this.minTop),
  61. color: this.getRandomColor(),
  62. display: 1,
  63. });
  64. },
  65. start(items = []) {
  66. this.items = [];
  67. cycle && (clearInterval(cycle));
  68. let i = 0,
  69. len = items.length;
  70. cycle = setInterval(() => {
  71. let time = 5;
  72. // #ifndef H5
  73. time = Math.ceil(Math.floor(Math.random() * (this.maxTime - this.minTime + 1) + this.minTime));
  74. // #endif
  75. if (i < len) {
  76. this.add(items[i], time);
  77. i++;
  78. } else {
  79. clearInterval(cycle);
  80. setTimeout(() => {
  81. this.$emit("end", {});
  82. }, time * 1000)
  83. }
  84. }, 500)
  85. }
  86. }
  87. }
  88. </script>
  89. <style>
  90. .aon {
  91. position: absolute;
  92. white-space: nowrap;
  93. animation: mymove 5s linear forwards;
  94. animation-timing-function: linear;
  95. -webkit-animation-timing-function: linear;
  96. animation-fill-mode: forwards;
  97. }
  98. .l-barrage {
  99. position: relative;
  100. z-index: 3;
  101. width: 100%;
  102. height: 320upx;
  103. overflow: hidden;
  104. }
  105. @keyframes mymove {
  106. from {
  107. left: 100%;
  108. }
  109. to {
  110. left: -200%;
  111. }
  112. }
  113. @-moz-keyframes mymove
  114. /* Firefox */
  115. {
  116. from {
  117. left: 100%;
  118. }
  119. to {
  120. left: -200%;
  121. }
  122. }
  123. @-webkit-keyframes mymove
  124. /* Safari and Chrome */
  125. {
  126. from {
  127. left: 100%;
  128. }
  129. to {
  130. left: -200%;
  131. }
  132. }
  133. @-o-keyframes mymove
  134. /* Opera */
  135. {
  136. from {
  137. left: 100%;
  138. }
  139. to {
  140. left: -200%;
  141. }
  142. }
  143. </style>