lime-barrage.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <template>
  2. <demo-block title="弹幕">
  3. <view class="barrage">
  4. <l-barrage
  5. :data="barrageData"
  6. :pause="pause"
  7. :trackHeight="36"
  8. :showArea="showArea"
  9. @progress="progress"
  10. touchPause >
  11. <template #default="{content}">
  12. <view class="bullet">{{content.text}}</view>
  13. </template>
  14. </l-barrage>
  15. </view>
  16. <view class="inputs">
  17. <input v-model="value" confirm-type="send" @confirm="onFire"/>
  18. <button @click="onFire">发射</button>
  19. </view>
  20. <view class="buttons">
  21. <button @click="onPause">{{!pause ? '暂停': '播放'}}</button>
  22. <button @click="onShowArea('all')">all</button>
  23. <button @click="onShowArea('top')">top</button>
  24. <button @click="onShowArea('middle')">middle</button>
  25. <button @click="onShowArea('bottom')">bottom</button>
  26. </view>
  27. </demo-block>
  28. </template>
  29. <script>
  30. import {onMounted,ref,watch, defineComponent} from '@/uni_modules/lime-shared/vue';
  31. export default defineComponent({
  32. setup() {
  33. const value = ref('')
  34. const showArea = ref('all')
  35. const pause = ref(false)
  36. const barrageData = ref([])
  37. const onFire = () => {
  38. if (value.value) {
  39. barrageData.value.push({
  40. text: value.value
  41. })
  42. value.value = ''
  43. }
  44. }
  45. const onPause = () => {
  46. pause.value = !pause.value
  47. }
  48. const onShowArea = (type) => {
  49. showArea.value = type
  50. }
  51. const createBarrageData = () => {
  52. const data = []
  53. for (let i = 0; i < 50; i++) {
  54. const random = Math.floor(Math.random() * 10)
  55. data.push({
  56. text: `${new Array(random).fill('~').join('🤩')}我是测试弹幕 🥰`
  57. })
  58. }
  59. barrageData.value.push(...data)
  60. }
  61. let count = 0
  62. const progress = (v) => {
  63. console.log('progress', v, count)
  64. if(v > 85 && count < 5) {
  65. count++
  66. createBarrageData()
  67. }
  68. }
  69. onMounted(createBarrageData)
  70. return {
  71. value,
  72. showArea,
  73. pause,
  74. barrageData,
  75. onFire,
  76. onPause,
  77. onShowArea,
  78. progress
  79. }
  80. }
  81. })
  82. </script>
  83. <style lang="scss">
  84. .barrage {
  85. background-color: rgba(0, 139, 139, 0.14);
  86. height: 550rpx;
  87. margin-bottom: 2px;
  88. }
  89. .inputs {
  90. display: flex;
  91. align-items: center;
  92. padding: 10px;
  93. background-color: rgba(0, 139, 139, 0.54);
  94. input {
  95. flex: 1;
  96. margin-right: 5px;
  97. padding: 10px;
  98. background-color: #fff;
  99. }
  100. }
  101. .buttons {
  102. display: flex;
  103. button {
  104. font-size: 32rpx;
  105. }
  106. }
  107. .bullet {
  108. background-color: rgba(0, 0, 0, 60%);
  109. padding: 5px 8px;
  110. color: #fff;
  111. border-radius: 999px;
  112. }
  113. </style>