y-Barrage.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. aon : 'aon',
  45. }
  46. },
  47. methods: {
  48. getRandomColor() {
  49. let rgb = []
  50. for (let i = 0; i < 3; ++i) {
  51. let color = Math.floor(Math.random() * 256).toString(16)
  52. color = color.length == 1 ? '0' + color : color
  53. rgb.push(color)
  54. }
  55. return '#' + rgb.join('')
  56. },
  57. getTop() {
  58. var top = Math.ceil(Math.random() * (this.maxTop - this.minTop + 1) + this.minTop);
  59. return top;
  60. },
  61. add(item = {}, time = Math.ceil(Math.floor(Math.random() * (this.maxTime - this.minTime + 1) + this.minTime))) {
  62. var text = item.content;
  63. this.items.push({
  64. text,
  65. time,
  66. top: this.getTop(),
  67. color: this.getRandomColor(),
  68. display: 1,
  69. });
  70. },
  71. start(items = []) {
  72. this.items = [];
  73. cycle && (clearInterval(cycle));
  74. let i = 0,
  75. len = items.length;
  76. cycle = setInterval(() => {
  77. let time = 5;
  78. // #ifndef H5
  79. time = Math.ceil(Math.floor(Math.random() * (this.maxTime - this.minTime + 1) + this.minTime));
  80. // #endif
  81. if (i < len) {
  82. this.add(items[i], time);
  83. i++;
  84. } else {
  85. clearInterval(cycle);
  86. setTimeout(() => {
  87. this.$emit("end", {});
  88. }, time * 500)
  89. }
  90. }, 500)
  91. },
  92. stop() {
  93. //cycle && (clearInterval(cycle));
  94. }
  95. }
  96. }
  97. </script>
  98. <style>
  99. .aon {
  100. position: absolute;
  101. white-space: nowrap;
  102. animation: mymove 5s linear forwards;
  103. animation-timing-function: linear;
  104. -webkit-animation-timing-function: linear;
  105. animation-fill-mode: forwards;
  106. }
  107. .aoff {
  108. position: absolute;
  109. white-space: nowrap;
  110. }
  111. .l-barrage {
  112. position: relative;
  113. z-index: 3;
  114. width: 100%;
  115. height: 100%;
  116. overflow: hidden;
  117. }
  118. @keyframes mymove {
  119. from {
  120. left: 100%;
  121. }
  122. to {
  123. left: -200%;
  124. }
  125. }
  126. @-moz-keyframes mymove
  127. /* Firefox */
  128. {
  129. from {
  130. left: 100%;
  131. }
  132. to {
  133. left: -200%;
  134. }
  135. }
  136. @-webkit-keyframes mymove
  137. /* Safari and Chrome */
  138. {
  139. from {
  140. left: 100%;
  141. }
  142. to {
  143. left: -200%;
  144. }
  145. }
  146. @-o-keyframes mymove
  147. /* Opera */
  148. {
  149. from {
  150. left: 100%;
  151. }
  152. to {
  153. left: -200%;
  154. }
  155. }
  156. </style>