lff-barrage.vue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <template>
  2. <view style="overflow: hidden;position: fixed;width: 100%;height: 100%;pointer-events: none; top: 0;">
  3. <view class="danmu-li" v-for="(item, index) in listData" :class="item.type" :style="item.style" :key="index">
  4. <view class="danmu-inner">
  5. <view class="user-box">
  6. <view class="user-img">
  7. <view class="img-box">
  8. <image :src="item.avatar || 'https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=317894666,3379114684&fm=26&gp=0.jpg'"></image>
  9. </view>
  10. </view>
  11. <view class="user-text cl1">{{ item.nickName }}</view>
  12. <view class="user-status cl1">{{ item.text }}</view>
  13. </view>
  14. </view>
  15. </view>
  16. </view>
  17. </template>
  18. <script>
  19. export default {
  20. props: {
  21. //rightToLeft leftToRight leftBottom
  22. type: {
  23. type: String,
  24. default: 'rightToLeft'
  25. },
  26. minTime: {
  27. type: Number,
  28. default: 4
  29. },
  30. maxTime: {
  31. type: Number,
  32. default: 9
  33. },
  34. minTop: {
  35. type: Number,
  36. default: 0
  37. },
  38. maxTop: {
  39. type: Number,
  40. default: 240
  41. },
  42. hrackH: {
  43. //轨道高度
  44. type: Number,
  45. default: 40
  46. },
  47. noStacked: {
  48. //不允许堆叠(暂不可用)
  49. type: Array,
  50. default() {
  51. return [];
  52. }
  53. }
  54. },
  55. data() {
  56. return {
  57. listData: []
  58. };
  59. },
  60. mounted() {
  61. this.hrackNum = Math.floor((this.maxTop - this.minTop) / this.hrackH);
  62. },
  63. methods: {
  64. add(obj) {
  65. let data = {
  66. item: obj.item,
  67. id: Date.parse(new Date()),
  68. time: Math.ceil(Math.floor(Math.random() * (this.maxTime - this.minTime + 1) + this.minTime)),
  69. type: this.type
  70. };
  71. if (this.type === 'leftBottom') {
  72. let objData = {
  73. item: data.item,
  74. type: 'leftBottomEnter',
  75. style: {
  76. transition: `all 0.5s`,
  77. animationDuration: `0.5s`,
  78. transform: `translateX(0%)`,
  79. bottom: `${this.minTop}px`
  80. }
  81. };
  82. let listLen = this.listData.length;
  83. let hrackNum = this.hrackNum;
  84. for (let i in this.listData) {
  85. if (this.listData[i].status === 'reuse') {
  86. //重用
  87. this.$set(this.listData, i, objData);
  88. } else if (this.listData[i].status === 'reset') {
  89. //重置
  90. this.listData[i].style.transition = 'none';
  91. this.listData[i].style.bottom = 0;
  92. this.listData[i].status = 'reuse';
  93. } else if (this.listData[i].status === 'recycle') {
  94. //回收
  95. this.listData[i].type = 'leftBottomExit';
  96. this.listData[i].status = 'reset';
  97. } else {
  98. this.listData[i].style.bottom = parseInt(this.listData[i].style.bottom) + this.hrackH + 'px';
  99. }
  100. if (parseInt(this.listData[i].style.bottom) >= this.maxTop - this.hrackH && this.listData[i].status !== 'reset') {
  101. //需要回收
  102. this.listData[i].status = 'recycle';
  103. }
  104. }
  105. if (listLen < hrackNum + 2) {
  106. this.listData.push(objData);
  107. }
  108. } else if (this.type === 'rightToLeft' || this.type === 'leftToRight') {
  109. let objData = this.horStacked(data);
  110. for (let i in this.listData) {
  111. if (this.listData[i].delTime <= Date.parse(new Date())) {
  112. this.repaint(i, objData.type);
  113. objData.type = '';
  114. this.$set(this.listData, i, objData);
  115. return;
  116. }
  117. }
  118. this.listData.push(objData);
  119. }
  120. },
  121. horStacked(data) {
  122. return {
  123. item: data.item,
  124. type: this.type,
  125. style: {
  126. animationDuration: `${data.time}s`,
  127. top: `${Math.ceil(Math.random() * (this.maxTop - this.minTop + 1) + this.minTop)}px`
  128. },
  129. delTime: Date.parse(new Date()) + data.time * 1200
  130. };
  131. },
  132. repaint(index, type) {
  133. setTimeout(() => {
  134. this.listData[index].type = type;
  135. }, 100);
  136. }
  137. }
  138. };
  139. </script>
  140. <style></style>
  141. <style lang="scss">
  142. @keyframes leftBottomEnter {
  143. 0% {
  144. transform: translateY(100%);
  145. opacity: 0;
  146. }
  147. 100% {
  148. transform: translateY(0%);
  149. opacity: 1;
  150. }
  151. }
  152. @keyframes leftBottomExit {
  153. 0% {
  154. transform: translateY(0%);
  155. opacity: 1;
  156. }
  157. 100% {
  158. transform: translateY(-200%);
  159. opacity: 0;
  160. }
  161. }
  162. @keyframes leftToRight {
  163. 0% {
  164. transform: translateX(-100%);
  165. }
  166. 100% {
  167. transform: translateX(100%);
  168. }
  169. }
  170. @keyframes rightToLeft {
  171. 0% {
  172. transform: translateX(100%);
  173. }
  174. 100% {
  175. transform: translateX(-100%);
  176. }
  177. }
  178. .danmu-li {
  179. position: absolute;
  180. width: 100%;
  181. transform: translateX(100%);
  182. animation-timing-function: linear;
  183. &.leftBottomEnter {
  184. animation-name: leftBottomEnter;
  185. }
  186. &.leftBottomExit {
  187. animation-name: leftBottomExit;
  188. animation-fill-mode: forwards;
  189. }
  190. &.rightToLeft {
  191. animation-name: rightToLeft;
  192. }
  193. &.leftToRight {
  194. animation-name: leftToRight;
  195. }
  196. .danmu-inner {
  197. display: inline-block;
  198. .user-box {
  199. display: flex;
  200. padding: 3rpx 40rpx 3rpx 10rpx;
  201. background: rgba(0, 0, 0, 0.3);
  202. border-radius: 32rpx;
  203. align-items: center;
  204. .user-img {
  205. .img-box {
  206. display: flex;
  207. image {
  208. width: 58rpx;
  209. height: 58rpx;
  210. background: rgba(55, 55, 55, 1);
  211. border-radius: 50%;
  212. }
  213. }
  214. }
  215. .user-status {
  216. margin-left: 10rpx;
  217. white-space: nowrap;
  218. font-size: 28rpx;
  219. font-weight: 400;
  220. color: rgba(255, 255, 255, 1);
  221. }
  222. .user-text {
  223. margin-left: 10rpx;
  224. // white-space: nowrap;
  225. font-size: 28rpx;
  226. font-weight: 400;
  227. width: 80rpx;
  228. color: rgba(255, 255, 255, 1);
  229. }
  230. }
  231. }
  232. }
  233. </style>