1
0

barrage.vue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. <template>
  2. <view class="barrage-wrap" :class="[type]">
  3. <view class="danmu-li" v-for="(item, index) in listData" :class="[type, item.type]" :style="[item.style]" :key="index">
  4. <view class="danmu-inner">
  5. <view class="user-box">
  6. <view class="user-img"><image class="avatar" :src="item.item.avatar"></image></view>
  7. <view class="user-text cl1">{{ item.item.author }}</view>
  8. <view class="user-status cl1">{{ item.item.content }}</view>
  9. </view>
  10. </view>
  11. </view>
  12. </view>
  13. </template>
  14. <script>
  15. export default {
  16. props: {
  17. // rightToLeft leftToRight leftBottom
  18. type: {
  19. type: String,
  20. default: 'rightToLeft'
  21. },
  22. list: {
  23. type: Array,
  24. default() {
  25. return [];
  26. }
  27. },
  28. minTime: {
  29. type: Number,
  30. default: 4
  31. },
  32. maxTime: {
  33. type: Number,
  34. default: 9
  35. },
  36. minTop: {
  37. type: Number,
  38. default: 0
  39. },
  40. maxTop: {
  41. type: Number,
  42. default: 240
  43. },
  44. hrackH: {
  45. //轨道高度
  46. type: Number,
  47. default: 40
  48. }
  49. },
  50. data() {
  51. return {
  52. closeTimer: null,
  53. listData: []
  54. };
  55. },
  56. mounted() {
  57. //leftBottom 使用参数
  58. if (this.type === 'leftBottom') {
  59. this.hrackNum = Math.floor(this.maxTop / this.hrackH);
  60. }
  61. },
  62. methods: {
  63. add(obj) {
  64. this.isShow = true;
  65. this.showFlag = true;
  66. let data = {
  67. ...obj,
  68. id: Date.parse(new Date()),
  69. time: Math.ceil(Math.floor(Math.random() * (this.maxTime - this.minTime + 1) + this.minTime)),
  70. type: this.type
  71. };
  72. if (this.type === 'leftBottom') {
  73. let objData = {
  74. item: data,
  75. type: 'leftBottomEnter',
  76. style: {
  77. transition: `all 0.5s`,
  78. animationDuration: `0.5s`,
  79. transform: `translateX(0%)`,
  80. bottom: `${this.minTop}px`
  81. }
  82. };
  83. let listLen = this.listData.length;
  84. let hrackNum = this.hrackNum;
  85. for (let i in this.listData) {
  86. if (this.listData[i].status === 'reuse') {
  87. //重用
  88. this.$set(this.listData, i, objData);
  89. } else if (this.listData[i].status === 'reset') {
  90. //重置
  91. this.listData[i].style.transition = 'none';
  92. this.listData[i].style.bottom = '6px';
  93. this.listData[i].status = 'reuse';
  94. } else if (this.listData[i].status === 'recycle') {
  95. //回收
  96. this.listData[i].type = 'leftBottomExit';
  97. this.listData[i].status = 'reset';
  98. } else {
  99. this.listData[i].style.bottom = parseInt(this.listData[i].style.bottom) + this.hrackH + 'px';
  100. }
  101. if (parseInt(this.listData[i].style.bottom) >= this.maxTop - this.hrackH && this.listData[i].status !== 'reset') {
  102. //需要回收
  103. this.listData[i].status = 'recycle';
  104. }
  105. }
  106. if (listLen < hrackNum + 2) {
  107. this.listData.push(objData);
  108. }
  109. } else if (this.type === 'rightToLeft') {
  110. let objData = {
  111. item: data,
  112. type: 'rightToLeft',
  113. style: {
  114. animationDuration: `${data.time}s`,
  115. top: `${Math.ceil(Math.random() * (this.maxTop - this.minTop + 1) + this.minTop)}px`
  116. },
  117. delTime: Date.parse(new Date()) + data.time * 1200
  118. };
  119. for (let i in this.listData) {
  120. if (this.listData[i].delTime <= Date.parse(new Date())) {
  121. this.repaint(i, objData.type);
  122. objData.type = '';
  123. this.$set(this.listData, i, objData);
  124. return;
  125. }
  126. }
  127. this.listData.push(objData);
  128. }
  129. },
  130. async remove(options = {}) {
  131. if (this.type == 'rightToLeft') {
  132. if (this.listData.length != 0) {
  133. const last = this.listData[this.listData.length - 1];
  134. setTimeout(() => {
  135. this.listData = [];
  136. }, last.item.time * 1200 + 1000);
  137. }
  138. return;
  139. }
  140. options = Object.assign(
  141. {},
  142. {
  143. duration: 5000, // 延迟关闭的时间
  144. speed: 1000 // 弹幕消失的速度
  145. },
  146. options
  147. );
  148. const _fnHandleRemove = item => {
  149. return new Promise(resolve => {
  150. setTimeout(() => {
  151. // item['type'] = 'leftBottomExitLeft';
  152. item['type'] = 'is-hide';
  153. resolve();
  154. }, options.speed);
  155. });
  156. };
  157. const _fnHandleLoop = item => {
  158. return new Promise(resolve => {
  159. setTimeout(async () => {
  160. for (var i = 0; i < this.listData.length; i++) {
  161. await _fnHandleRemove(this.listData[i]);
  162. }
  163. resolve();
  164. }, options.duration);
  165. });
  166. };
  167. await _fnHandleLoop();
  168. setTimeout(() => {
  169. this.listData = [];
  170. }, options.duration + 200);
  171. },
  172. repaint(index, type) {
  173. setTimeout(() => {
  174. this.listData[index].type = type;
  175. }, 100);
  176. }
  177. }
  178. };
  179. </script>
  180. <style lang="scss" scoped>
  181. @keyframes leftBottomEnter {
  182. 0% {
  183. transform: translateY(100%);
  184. opacity: 0;
  185. }
  186. 100% {
  187. transform: translateY(0%);
  188. opacity: 1;
  189. }
  190. }
  191. @keyframes leftBottomExit {
  192. 0% {
  193. transform: translateY(0%);
  194. opacity: 1;
  195. }
  196. 100% {
  197. transform: translateY(-200%);
  198. opacity: 0;
  199. }
  200. }
  201. @keyframes leftBottomExitLeft {
  202. 0% {
  203. transform: translateX(0%);
  204. opacity: 1;
  205. }
  206. 50% {
  207. transform: translateX(-50%);
  208. opacity: 1;
  209. }
  210. 100% {
  211. transform: translateX(-100%);
  212. opacity: 0;
  213. }
  214. }
  215. @keyframes leftToRight {
  216. 0% {
  217. transform: translateX(-100%);
  218. }
  219. 100% {
  220. transform: translateX(100%);
  221. }
  222. }
  223. @keyframes rightToLeft {
  224. 0% {
  225. transform: translateX(100%);
  226. }
  227. 100% {
  228. transform: translateX(-100%);
  229. }
  230. }
  231. .barrage-wrap {
  232. position: fixed;
  233. width: 100%;
  234. height: 100%;
  235. pointer-events: none;
  236. top: 0;
  237. z-index: 99;
  238. transition: all 1s ease-in-out;
  239. &.leftBottom {
  240. top: initial;
  241. /* #ifdef H5 */
  242. bottom: 130rpx;
  243. /* #endif */
  244. /* #ifndef H5 */
  245. bottom: 36rpx;
  246. /* #endif */
  247. }
  248. }
  249. .danmu-li {
  250. position: absolute;
  251. width: 100%;
  252. transform: translateX(100%);
  253. animation-timing-function: linear;
  254. transition: all 0.5s ease-in-out;
  255. opacity: 1;
  256. &.is-hide {
  257. opacity: 0;
  258. // transform: translateX(-100%) !important;
  259. }
  260. &.leftBottom {
  261. left: 24rpx;
  262. }
  263. &.leftBottomEnter {
  264. animation-name: leftBottomEnter;
  265. }
  266. &.leftBottomExit {
  267. animation-name: leftBottomExit;
  268. animation-fill-mode: forwards;
  269. }
  270. &.leftBottomExitLeft {
  271. animation-name: leftBottomExitLeft;
  272. animation-fill-mode: forwards;
  273. }
  274. &.rightToLeft {
  275. animation-name: rightToLeft;
  276. }
  277. &.leftToRight {
  278. animation-name: rightToLeft;
  279. }
  280. .danmu-inner {
  281. display: inline-block;
  282. font-size: 24rpx;
  283. .user-box {
  284. display: flex;
  285. padding: 6rpx 40rpx 6rpx 10rpx;
  286. background: rgba(0, 0, 0, 0.3);
  287. border-radius: 32rpx;
  288. align-items: center;
  289. .user-img {
  290. display: flex;
  291. align-items: center;
  292. justify-content: center;
  293. .avatar {
  294. width: 42rpx;
  295. height: 42rpx;
  296. background: rgba(55, 55, 55, 1);
  297. border-radius: 50%;
  298. }
  299. }
  300. .user-status {
  301. margin-left: 10rpx;
  302. white-space: nowrap;
  303. font-weight: 400;
  304. color: rgba(255, 255, 255, 1);
  305. max-width: 320rpx;
  306. text-overflow: ellipsis;
  307. overflow: hidden;
  308. white-space: nowrap;
  309. }
  310. .user-text {
  311. max-width: 100rpx;
  312. margin-left: 10rpx;
  313. font-weight: 400;
  314. color: rgba(255, 255, 255, 1);
  315. text-overflow: ellipsis;
  316. overflow: hidden;
  317. white-space: nowrap;
  318. }
  319. }
  320. }
  321. }
  322. </style>