l-barrage.vue 2.8 KB

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