chunLei-danmu.nvue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <template>
  2. <div class="danmu" :style="{ width: `${width}px` }">
  3. <div class="danmu-row" :style="{ width: `${width}px` }">
  4. <div :style="{ left:`${width}px`}" ref="move" :id="item._id" v-for="item in danmuList1" :key="item._id" class="moveDiv">
  5. <image :src="item.avatar" class="img" v-if="item.avatar"></image>
  6. <text :class="`${platform}-title`" :style="{ color:item.color?item.color:'#fff' }">{{item.text}}</text>
  7. </div>
  8. </div>
  9. <div class="danmu-row" :style="{ width: `${width}px` }">
  10. <div :style="{ left:`${width}px`}" ref="move" :id="item._id" v-for="item in danmuList2" :key="item._id" class="moveDiv">
  11. <image :src="item.avatar" class="img" v-if="item.avatar"></image>
  12. <text :class="`${platform}-title`" :style="{ color:item.color?item.color:'#fff' }">{{item.text}}</text>
  13. </div>
  14. </div>
  15. <div class="danmu-row" :style="{ width: `${width}px` }">
  16. <div :style="{ left:`${width}px`}" ref="move" :id="item._id" v-for="item in danmuList3" :key="item._id" class="moveDiv">
  17. <image :src="item.avatar" class="img" v-if="item.avatar"></image>
  18. <text :class="`${platform}-title`" :style="{ color:item.color?item.color:'#fff' }">{{item.text}}</text>
  19. </div>
  20. </div>
  21. <div class="danmu-row" :style="{ width: `${width}px` }">
  22. <div :style="{ left:`${width}px`}" ref="move" :id="item._id" v-for="item in danmuList4" :key="item._id" class="moveDiv">
  23. <image :src="item.avatar" class="img" v-if="item.avatar"></image>
  24. <text :class="`${platform}-title`" :style="{ color:item.color?item.color:'#fff' }">{{item.text}}</text>
  25. </div>
  26. </div>
  27. </div>
  28. </template>
  29. <script>
  30. const animation = weex.requireModule('animation');
  31. const modal = weex.requireModule('modal');
  32. export default{
  33. props:{
  34. width:{
  35. type:Number,
  36. default:0
  37. },
  38. danmuList:{ //弹幕
  39. type:[Array],
  40. default:()=>[]
  41. },
  42. platform:{
  43. type:String,
  44. default:'android'
  45. },
  46. current:{
  47. type:Number,
  48. default:0
  49. }
  50. },
  51. data(){
  52. return{
  53. danmuList1:[],
  54. danmuList2:[],
  55. danmuList3:[],
  56. danmuList4:[],
  57. }
  58. },
  59. methods:{
  60. promise(){
  61. let promise = new Promise((resolve,reject)=>{
  62. setTimeout(()=>{
  63. resolve()
  64. },100)
  65. })
  66. return promise
  67. },
  68. cleanDanmu(){
  69. this.danmuList1 = []
  70. this.danmuList2 = []
  71. this.danmuList3 = []
  72. this.danmuList4 = []
  73. },
  74. randomRange(min, max) { // min最小值,max最大值
  75. return Math.floor(Math.random() * (max - min)) + min;
  76. },
  77. animationText(id,distance,fn){
  78. let el
  79. let elList = this.$refs.move
  80. for (let item of elList) {
  81. if(item.attr.id == id){
  82. el = item
  83. }
  84. }
  85. animation.transition(el, {
  86. styles: {
  87. transform: `translate( ${distance}px, 0px)`,
  88. },
  89. duration: 20000, //ms
  90. timingFunction: 'ease',
  91. delay: 0 //ms
  92. }, () => {
  93. fn()
  94. }
  95. )
  96. }
  97. },
  98. watch:{
  99. danmuList:{
  100. immediate:true,
  101. handler(newVal,oldVal){
  102. for (let key in newVal) {
  103. newVal[key]._id = key
  104. }
  105. }
  106. },
  107. current:{
  108. handler:async function(newVal,oldVal){
  109. //间隔0.25
  110. if(Math.abs(newVal-oldVal)>=0.24){
  111. for (let item of this.danmuList) {
  112. if( item.time > Math.floor(newVal*1) && item.time <= Math.floor(newVal*1+ 0.25) ) {
  113. let num = this.randomRange(1,5)
  114. this[`danmuList${num}`].push(item)
  115. await this.promise()
  116. //开始动画
  117. this.animationText(item._id,-this.width*2,()=>{
  118. let index
  119. //删除动画后的text
  120. for (let key in this[`danmuList${num}`]) {
  121. if(this[`danmuList${num}`][key]._id==item._id){
  122. index = key
  123. }
  124. }
  125. this[`danmuList${num}`].splice(index,1)
  126. })
  127. }
  128. }
  129. }
  130. }
  131. },
  132. }
  133. }
  134. </script>
  135. <style>
  136. .danmu{
  137. height: 160px;
  138. flex-direction: column;
  139. }
  140. .danmu-row{
  141. position: relative;
  142. height: 40px;
  143. flex-direction: row;
  144. }
  145. .moveDiv{
  146. position: absolute;
  147. flex-direction: row;
  148. justify-content: center;
  149. }
  150. .android-title{
  151. font-size: 24px;
  152. }
  153. .img{
  154. width: 30px;
  155. height: 30px;
  156. margin-left: 10px;
  157. border-radius: 15px;
  158. }
  159. .ios-title{
  160. font-size: 24px;
  161. }
  162. </style>