lff-barrage.vue 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. <template>
  2. <view style="overflow: hidden;position: absolute;width: 100%;height: 90%;pointer-events: none; top: 0;left:0;z-index: 9996;">
  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="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">
  12. 人名
  13. </view> -->
  14. <view class="user-status cl1" v-html="getText(item.item)">
  15. </view>
  16. </view>
  17. </view>
  18. </view>
  19. </view>
  20. </template>
  21. <script>
  22. import {
  23. emojiName,
  24. emojiUrl,
  25. emojiMap
  26. } from '@/config/emojiMap';
  27. export default {
  28. props: {
  29. //rightToLeft leftToRight leftBottom
  30. type: {
  31. type: String,
  32. default: 'rightToLeft'
  33. },
  34. list: {
  35. type: Array,
  36. default () {
  37. return []
  38. }
  39. },
  40. minTime: {
  41. type: Number,
  42. default: 4
  43. },
  44. maxTime: {
  45. type: Number,
  46. default: 9
  47. },
  48. minTop: {
  49. type: Number,
  50. default: 0
  51. },
  52. maxTop: {
  53. type: Number,
  54. default: 240
  55. },
  56. hrackH: { //轨道高度
  57. type: Number,
  58. default: 40
  59. }
  60. },
  61. data() {
  62. return {
  63. listData: [],
  64. emojiList: [],
  65. }
  66. },
  67. mounted() {
  68. //leftBottom 使用参数
  69. if (this.type === 'leftBottom') {
  70. this.hrackNum = Math.floor(this.maxTop / this.hrackH);
  71. }
  72. for (let i = 0; i < emojiName.length; i++) {
  73. this.emojiList.push({
  74. emojiName: emojiName[i],
  75. url: emojiUrl + emojiMap[emojiName[i]]
  76. });
  77. }
  78. },
  79. methods: {
  80. add(obj) {
  81. let data = {
  82. item: obj.item,
  83. id:Date.parse(new Date()),
  84. time: Math.ceil(Math.floor(Math.random() * (this.maxTime - this.minTime + 1) + this.minTime)),
  85. type: this.type
  86. }
  87. if (this.type === 'leftBottom') {
  88. let objData = {
  89. item: data.item,
  90. type: 'leftBottomEnter',
  91. style: {
  92. transition: `all 0.5s`,
  93. animationDuration: `0.5s`,
  94. transform: `translateX(0%)`,
  95. bottom: `${this.minTop}px`
  96. }
  97. }
  98. let listLen = this.listData.length;
  99. let hrackNum = this.hrackNum;
  100. for (let i in this.listData) {
  101. if(this.listData[i].status === 'reuse'){ //重用
  102. this.$set(this.listData,i,objData);
  103. }else if(this.listData[i].status === 'reset'){ //重置
  104. this.listData[i].style.transition = 'none';
  105. this.listData[i].style.bottom = 0;
  106. this.listData[i].status = 'reuse';
  107. }else if(this.listData[i].status === 'recycle'){ //回收
  108. this.listData[i].type = 'leftBottomExit';
  109. this.listData[i].status = 'reset';
  110. }else{
  111. this.listData[i].style.bottom = parseInt(this.listData[i].style.bottom) + this.hrackH + 'px';
  112. }
  113. if(parseInt(this.listData[i].style.bottom) >= (this.maxTop - this.hrackH) && this.listData[i].status !== 'reset'){ //需要回收
  114. this.listData[i].status = 'recycle';
  115. }
  116. }
  117. if(listLen < hrackNum + 2){
  118. this.listData.push(objData);
  119. }
  120. } else if (this.type === 'rightToLeft') {
  121. let num = Math.ceil(Math.random() * (this.maxTop - this.minTop + 1) + this.minTop);
  122. let objData = {
  123. item: data.item,
  124. type: 'rightToLeft',
  125. style: {
  126. animationDuration: `${data.time}s`,
  127. top: num > 196 ?'196px': `${num}px`
  128. },
  129. delTime: Date.parse(new Date()) + data.time * 1200
  130. }
  131. for (let i in this.listData) {
  132. if (this.listData[i].delTime <= Date.parse(new Date())) {
  133. this.repaint(i, objData.type);
  134. objData.type = '';
  135. this.$set(this.listData, i, objData);
  136. return
  137. }
  138. }
  139. this.listData.push(objData);
  140. }
  141. },
  142. repaint(index, type) {
  143. setTimeout(() => {
  144. this.listData[index].type = type;
  145. }, 100)
  146. },
  147. getText(str) {
  148. // let str = 'jfkdsj[咒骂]dsjfkljsa[憨笑]哈哈哈,[惊恐]'
  149. let exp = /\[(.*?)\]/g; //匹配[*] 大括号里面任意内容的正则
  150. let arr = str.match(exp); //字符串匹配出来的数组
  151. // console.log(arr);
  152. if (!arr) return str;
  153. this.emojiList.forEach((item1, index) => {
  154. arr.map(item => {
  155. // console.log(item1);
  156. if (item1.emojiName === item) {
  157. str = str.replace(item,
  158. `<img src='${item1.url}' style="width:20px;height:20px">`
  159. );
  160. }
  161. // console.log(item);
  162. // let key = `p${item.substr(1, item.length - 2)}`; //记录大括号里的值 用作id 方便取值
  163. // if (!this.salaryVars.hasOwnProperty(key)) this.$set(this.salaryVars, key, '');
  164. }); //循环遍历
  165. })
  166. // let arrNum = 取出所有正则匹配值并转换为input
  167. // console.log(str);
  168. return str;
  169. }
  170. }
  171. }
  172. </script>
  173. <style>
  174. </style>
  175. <style lang="scss">
  176. @keyframes leftBottomEnter {
  177. 0% {
  178. transform: translateY(100%);
  179. opacity: 0;
  180. }
  181. 100% {
  182. transform: translateY(0%);
  183. opacity: 1;
  184. }
  185. }
  186. @keyframes leftBottomExit {
  187. 0% {
  188. transform: translateY(0%);
  189. opacity: 1;
  190. }
  191. 100% {
  192. transform: translateY(-200%);
  193. opacity: 0;
  194. }
  195. }
  196. @keyframes leftToRight {
  197. 0% {
  198. transform: translateX(-100%);
  199. }
  200. 100% {
  201. transform: translateX(100%);
  202. }
  203. }
  204. @keyframes rightToLeft {
  205. 0% {
  206. transform: translateX(100%);
  207. }
  208. 100% {
  209. transform: translateX(-100%);
  210. }
  211. }
  212. .danmu-li {
  213. position: absolute;
  214. width: 100%;
  215. transform: translateX(100%);
  216. animation-timing-function: linear;
  217. &.leftBottomEnter {
  218. animation-name: leftBottomEnter;
  219. }
  220. &.leftBottomExit{
  221. animation-name: leftBottomExit;
  222. animation-fill-mode: forwards;
  223. }
  224. &.rightToLeft {
  225. animation-name: rightToLeft;
  226. }
  227. &.leftToRight {
  228. animation-name: rightToLeft;
  229. }
  230. .danmu-inner {
  231. display: inline-block;
  232. .user-box {
  233. display: flex;
  234. padding: 3rpx 40rpx 3rpx 10rpx;
  235. background: rgba(0, 0, 0, 0.3);
  236. border-radius: 32rpx;
  237. align-items: center;
  238. .user-img {
  239. .img-box {
  240. display: flex;
  241. image {
  242. width: 58rpx;
  243. height: 58rpx;
  244. background: rgba(55, 55, 55, 1);
  245. border-radius: 50%;
  246. }
  247. }
  248. }
  249. .user-status {
  250. margin-left: 10rpx;
  251. white-space: nowrap;
  252. font-size: 28rpx;
  253. font-weight: 400;
  254. color: rgba(255, 255, 255, 1);
  255. }
  256. .user-text {
  257. margin-left: 10rpx;
  258. // white-space: nowrap;
  259. font-size: 28rpx;
  260. font-weight: 400;
  261. width: 80rpx;
  262. color: rgba(255, 255, 255, 1);
  263. }
  264. }
  265. }
  266. }
  267. </style>