当前位置:   article > 正文

uniapp使用nvue三item写仿抖音短视频_uni-app模仿抖音

uni-app模仿抖音

实机效果


首先,代码最主要的思路先说明一下:

        1.本身uniapp的渲染能力并不高,即时用上nvue也不能做到完美,所以我选择只渲染三个video,不过代码方面也还可以优化,这只是我简单的一个demo和思路提供,列如滑动暂停,或者视频先用封面处理。

        2.三个item就需要每次滑动都判断是到底是上还是下。

        3.再滑动到特定的index时需要不同的处理,需要在滑动时提前做好下一个的处理,比如在上滑到第二个播放器时,需要对三个播放器进行处理了。

先创建nvue项目,这边也就不多说了:

先在nvue页面创建一个swiper组件用for循环三个item里嵌套video,使用cover-view和cover-image来使层级在video之上

  1. <template>
  2. <view style="height: 100vh;flex:1;width:750rpx">
  3. <swiper @animationfinish="qiehuan" :current="activeIndex" @change="swiper" :circular="index==0?true:false" vertical
  4. class="list">
  5. <swiper-item disable-programmatic-animation class="item" v-for="(item,index) in list" :key="index">
  6. <video @ended="end" @pause='item.active=false' @click="pause(item,index)"
  7. @timeupdate="long($event,item,index)" :id="'Video'+index" class="video"
  8. :show-center-play-btn='false' :controls="false"
  9. src="https://media.w3.org/2010/05/sintel/trailer.mp4">
  10. <cover-view style="margin-top: 50rpx">
  11. <text style="color: aqua;">{{item.id}}</text></cover-view>
  12. <cover-view class="back" v-if="!item.active">
  13. <cover-image class="pauseIcon" src="../../static/icon/pause.png"></cover-image>
  14. </cover-view>
  15. <cover-view class="iconList">
  16. <cover-view class="iconListClass">
  17. <cover-image class="icon" src="../../static/icon/like.png"></cover-image>
  18. <cover-view>
  19. <text class="context">12.5w</text>
  20. </cover-view>
  21. </cover-view>
  22. <cover-view class="iconListClass">
  23. <cover-image class="icon" src="../../static/icon/like.png"></cover-image>
  24. <cover-view>
  25. <text class="context">12.5w</text>
  26. </cover-view>
  27. </cover-view>
  28. <cover-view class="iconListClass">
  29. <cover-image class="icon" src="../../static/icon/like.png"></cover-image>
  30. <cover-view style="white-space: nowrap;lines:1;">
  31. <text class="context">12.5w</text>
  32. </cover-view>
  33. </cover-view>
  34. </cover-view>
  35. <cover-view class="formation">
  36. <cover-view style="display: flex;align-items: center;flex-direction: row;">
  37. <cover-image class="header"
  38. src="https://i02piccdn.sogoucdn.com/a977a5bc3f44ffcf"></cover-image>
  39. <cover-view>
  40. <text class="context">作者</text>
  41. </cover-view>
  42. </cover-view>
  43. <cover-view>
  44. <text class="title">标题标题标题标题标题标题标题标题标题标题标题标题标题标题标题标题标题标题标题标题</text>
  45. </cover-view>
  46. </cover-view>
  47. <cover-view class="duration">
  48. <cover-view class="long" :style="{width:item.percent+'rpx'}"></cover-view>
  49. </cover-view>
  50. </video>
  51. </swiper-item>
  52. </swiper>
  53. </view>
  54. </template>

先来看看在第几个视频时,我们会遇到的情况:

        第1个:可能是视频从尾到头,也可能是从下一个播放器返回。

        第2个:可能是视频第一个过来,也可能是从下一个播放器返回。

        第3个:可能是视频第二个过来,也可能是从下一个播放器返回。

所以在滑动时,我们需要提前先进行判断到底是上滑还是下滑:

  1. qiehuan(e) {
  2. if (this.oldActiveIndex < e.detail.current) {
  3. if (this.oldActiveIndex - e.detail.current != -2) {
  4. console.log('上滑');
  5. this.index++
  6. this.next(e.detail.current)
  7. } else {
  8. console.log("下滑");
  9. this.pre(e.detail.current)
  10. this.index--
  11. }
  12. } else if (this.oldActiveIndex > e.detail.current) {
  13. if (this.oldActiveIndex - e.detail.current != 2) {
  14. console.log('下滑');
  15. this.pre(e.detail.current)
  16. this.index--
  17. } else {
  18. console.log("上滑");
  19. this.index++
  20. this.next(e.detail.current)
  21. }
  22. }
  23. this.$nextTick(() => {
  24. this.oldActiveIndex = e.detail.current
  25. })
  26. },

我们需要一个变量来记录上一次滑动的播放器下标拿来对比,因为从2到0,和0到2的情况存在,所以判断需要拆开。

然后在对不同情况进行不同的处理,我比较懒,所以代码直接拆开写了。

  1. next(index) {
  2. if (Object.is(this.index, 2)) {
  3. }
  4. if (Object.is(index, 0)) {
  5. this.list[1] = this.allList[this.index + 1]
  6. }
  7. if (Object.is(index, 1)) {
  8. this.list[2] = this.allList[this.index + 1]
  9. }
  10. if (Object.is(index, 2)) {
  11. this.list[0] = this.allList[this.index + 1]
  12. }
  13. this.play(this.activeIndex)
  14. },
  15. pre(index) {
  16. if (Object.is(this.index, 2)) {
  17. }
  18. if (Object.is(index, 0)) {
  19. this.list[2] = this.allList[this.index - 2]
  20. }
  21. if (Object.is(index, 1)) {
  22. this.list[0] = this.allList[this.index - 2]
  23. }
  24. if (Object.is(index, 2)) {
  25. this.list[1] = this.allList[this.index - 2]
  26. }
  27. this.play(this.activeIndex)
  28. },

主要的处理就是这样,还有一些小功能也做了对应的处理,比如滑动时上一个暂停等等,还有一些小优化,进度条等等。

全部代码:

  1. <template>
  2. <view style="height: 100vh;flex:1;width:750rpx">
  3. <swiper @animationfinish="qiehuan" :current="activeIndex" @change="swiper" :circular="index==0?true:false" vertical
  4. class="list">
  5. <swiper-item disable-programmatic-animation class="item" v-for="(item,index) in list" :key="index">
  6. <video @ended="end" @pause='item.active=false' @click="pause(item,index)"
  7. @timeupdate="long($event,item,index)" :id="'Video'+index" class="video"
  8. :show-center-play-btn='false' :controls="false"
  9. src="https://media.w3.org/2010/05/sintel/trailer.mp4">
  10. <cover-view style="margin-top: 50rpx">
  11. <text style="color: aqua;">{{item.id}}</text></cover-view>
  12. <cover-view class="back" v-if="!item.active">
  13. <cover-image class="pauseIcon" src="../../static/icon/pause.png"></cover-image>
  14. </cover-view>
  15. <cover-view class="iconList">
  16. <cover-view class="iconListClass">
  17. <cover-image class="icon" src="../../static/icon/like.png"></cover-image>
  18. <cover-view>
  19. <text class="context">12.5w</text>
  20. </cover-view>
  21. </cover-view>
  22. <cover-view class="iconListClass">
  23. <cover-image class="icon" src="../../static/icon/like.png"></cover-image>
  24. <cover-view>
  25. <text class="context">12.5w</text>
  26. </cover-view>
  27. </cover-view>
  28. <cover-view class="iconListClass">
  29. <cover-image class="icon" src="../../static/icon/like.png"></cover-image>
  30. <cover-view style="white-space: nowrap;lines:1;">
  31. <text class="context">12.5w</text>
  32. </cover-view>
  33. </cover-view>
  34. </cover-view>
  35. <cover-view class="formation">
  36. <cover-view style="display: flex;align-items: center;flex-direction: row;">
  37. <cover-image class="header"
  38. src="https://i02piccdn.sogoucdn.com/a977a5bc3f44ffcf"></cover-image>
  39. <cover-view>
  40. <text class="context">作者</text>
  41. </cover-view>
  42. </cover-view>
  43. <cover-view>
  44. <text class="title">标题标题标题标题标题标题标题标题标题标题标题标题标题标题标题标题标题标题标题标题</text>
  45. </cover-view>
  46. </cover-view>
  47. <cover-view class="duration">
  48. <cover-view class="long" :style="{width:item.percent+'rpx'}"></cover-view>
  49. </cover-view>
  50. </video>
  51. </swiper-item>
  52. </swiper>
  53. </view>
  54. </template>
  55. <script>
  56. export default {
  57. data() {
  58. return {
  59. // 视频下标,记录播放到第几个
  60. index: 0,
  61. // 数据
  62. allList: [{
  63. src: 'https://media.w3.org/2010/05/sintel/trailer.mp4',
  64. active: false,
  65. currentTime: 0,
  66. duration: 121,
  67. percent: 0,
  68. id: 0
  69. },
  70. {
  71. src: 'https://media.w3.org/2010/05/sintel/trailer.mp4',
  72. active: false,
  73. currentTime: 0,
  74. duration: 121,
  75. percent: 0,
  76. id: 1
  77. },
  78. {
  79. src: 'https://media.w3.org/2010/05/sintel/trailer.mp4',
  80. active: false,
  81. currentTime: 0,
  82. duration: 121,
  83. percent: 0,
  84. id: 2
  85. },
  86. {
  87. src: 'https://media.w3.org/2010/05/sintel/trailer.mp4',
  88. active: false,
  89. currentTime: 0,
  90. duration: 121,
  91. percent: 0,
  92. id: 3
  93. },
  94. {
  95. src: 'https://media.w3.org/2010/05/sintel/trailer.mp4',
  96. active: false,
  97. currentTime: 0,
  98. duration: 121,
  99. percent: 0,
  100. id: 4
  101. },
  102. {
  103. src: 'https://media.w3.org/2010/05/sintel/trailer.mp4',
  104. active: false,
  105. currentTime: 0,
  106. duration: 121,
  107. percent: 0,
  108. id: 5
  109. },
  110. {
  111. src: 'https://media.w3.org/2010/05/sintel/trailer.mp4',
  112. active: false,
  113. currentTime: 0,
  114. duration: 121,
  115. percent: 0,
  116. id: 6
  117. },
  118. {
  119. src: 'https://media.w3.org/2010/05/sintel/trailer.mp4',
  120. active: false,
  121. currentTime: 0,
  122. duration: 121,
  123. percent: 0,
  124. id: 7
  125. },
  126. {
  127. src: 'https://media.w3.org/2010/05/sintel/trailer.mp4',
  128. active: false,
  129. currentTime: 0,
  130. duration: 121,
  131. percent: 0,
  132. id: 8
  133. },
  134. {
  135. src: 'https://media.w3.org/2010/05/sintel/trailer.mp4',
  136. active: false,
  137. currentTime: 0,
  138. duration: 121,
  139. percent: 0,
  140. id: 9
  141. },
  142. {
  143. src: 'https://media.w3.org/2010/05/sintel/trailer.mp4',
  144. active: false,
  145. currentTime: 0,
  146. duration: 121,
  147. percent: 0,
  148. id: 10
  149. },
  150. ],
  151. list: [{
  152. src: 'https://media.w3.org/2010/05/sintel/trailer.mp4',
  153. active: false,
  154. currentTime: 0,
  155. duration: 121,
  156. percent: 0,
  157. id: 0
  158. },
  159. {
  160. src: 'https://media.w3.org/2010/05/sintel/trailer.mp4',
  161. active: false,
  162. currentTime: 0,
  163. duration: 121,
  164. percent: 0,
  165. id: 1
  166. },
  167. {
  168. src: 'https://media.w3.org/2010/05/sintel/trailer.mp4',
  169. active: false,
  170. currentTime: 0,
  171. duration: 121,
  172. percent: 0,
  173. id: 2
  174. },
  175. ],
  176. // 播放器下标
  177. activeIndex: 0,
  178. // 记录上一个播放器下标
  179. oldActiveIndex: 0
  180. }
  181. },
  182. onReady() {},
  183. methods: {
  184. qiehuan(e) {
  185. if (this.oldActiveIndex < e.detail.current) {
  186. if (this.oldActiveIndex - e.detail.current != -2) {
  187. console.log('上滑');
  188. this.index++
  189. this.next(e.detail.current)
  190. } else {
  191. console.log("下滑");
  192. this.pre(e.detail.current)
  193. this.index--
  194. }
  195. } else if (this.oldActiveIndex > e.detail.current) {
  196. if (this.oldActiveIndex - e.detail.current != 2) {
  197. console.log('下滑');
  198. this.pre(e.detail.current)
  199. this.index--
  200. } else {
  201. console.log("上滑");
  202. this.index++
  203. this.next(e.detail.current)
  204. }
  205. }
  206. this.$nextTick(() => {
  207. this.oldActiveIndex = e.detail.current
  208. })
  209. },
  210. next(index) {
  211. if (Object.is(this.index, 2)) {
  212. }
  213. if (Object.is(index, 0)) {
  214. this.list[1] = this.allList[this.index + 1]
  215. }
  216. if (Object.is(index, 1)) {
  217. this.list[2] = this.allList[this.index + 1]
  218. }
  219. if (Object.is(index, 2)) {
  220. this.list[0] = this.allList[this.index + 1]
  221. }
  222. this.play(this.activeIndex)
  223. },
  224. pre(index) {
  225. if (Object.is(this.index, 2)) {
  226. }
  227. if (Object.is(index, 0)) {
  228. this.list[2] = this.allList[this.index - 2]
  229. }
  230. if (Object.is(index, 1)) {
  231. this.list[0] = this.allList[this.index - 2]
  232. }
  233. if (Object.is(index, 2)) {
  234. this.list[1] = this.allList[this.index - 2]
  235. }
  236. this.play(this.activeIndex)
  237. },
  238. play(index) {
  239. for (let i = 0; i < 3; i++) {
  240. uni.createVideoContext(`Video${i}`, this).pause()
  241. }
  242. this.videoContext = uni.createVideoContext('Video' + index, this)
  243. this.list[index].active = true
  244. this.videoContext.play()
  245. },
  246. swiper(e) {
  247. // console.log(e.detail.current);
  248. this.activeIndex = e.detail.current
  249. // this.play(e.detail.current)
  250. // this.play(this.activeIndex)
  251. },
  252. end(e) {
  253. this.activeIndex++
  254. if (Object.is(this.activeIndex, 3)) {
  255. this.activeIndex = 0
  256. }
  257. },
  258. long(e, item, index) {
  259. item.currentTime = e.detail.currentTime
  260. item.percent = (e.detail.currentTime / e.detail.duration) * 750
  261. },
  262. pause(item, index) {
  263. this.videoContext = uni.createVideoContext('Video' + index, this)
  264. if (item.active) {
  265. this.videoContext.pause()
  266. } else {
  267. this.videoContext.play()
  268. }
  269. this.list[index].active = !this.list[index].active
  270. },
  271. videoClick() {
  272. }
  273. }
  274. }
  275. </script>
  276. <style>
  277. .list {
  278. background-color: black;
  279. flex: 1;
  280. width: 750rpx;
  281. }
  282. .video {
  283. flex: 1;
  284. width: 750rpx;
  285. }
  286. .back {
  287. background-color: rgba(225, 225, 225, 0.3);
  288. flex: 1;
  289. width: 750rpx;
  290. justify-content: center;
  291. align-items: center;
  292. }
  293. .pauseIcon {
  294. position: absolute;
  295. left: calc(50% - 25px);
  296. top: calc(50% - 30px);
  297. width: 50px;
  298. height: 60px;
  299. }
  300. .duration {
  301. position: absolute;
  302. left: 0px;
  303. bottom: 0px;
  304. width: 100vw;
  305. height: 3px;
  306. }
  307. .long {
  308. height: 100%;
  309. background-color: white;
  310. }
  311. .formation {
  312. flex-direction: column;
  313. position: absolute;
  314. left: 10px;
  315. bottom: 10px;
  316. width: calc(100% - 80px);
  317. display: flex;
  318. }
  319. .header {
  320. width: 40px;
  321. height: 40px;
  322. border-radius: 50%;
  323. }
  324. .iconListClass {
  325. justify-content: center;
  326. align-items: center;
  327. }
  328. .context {
  329. white-space: nowrap;
  330. width: 100rpx;
  331. color: white;
  332. margin-left: 5px;
  333. font-size: 13px;
  334. font-weight: bold;
  335. }
  336. .title {
  337. width: 600rpx;
  338. margin-top: 10px;
  339. color: white;
  340. margin-left: 5px;
  341. font-size: 13px;
  342. font-weight: bold;
  343. text-overflow: ellipsis;
  344. overflow: hidden;
  345. white-space: nowrap;
  346. lines: 1;
  347. }
  348. .iconList {
  349. display: flex;
  350. flex-direction: column;
  351. justify-content: center;
  352. align-items: center;
  353. width: 40px;
  354. position: absolute;
  355. right: 10px;
  356. bottom: 10px;
  357. }
  358. .icon {
  359. margin-top: 10px;
  360. width: 40px;
  361. height: 40px;
  362. }
  363. .context {
  364. text-align: center;
  365. color: white;
  366. font-size: 13px;
  367. }
  368. </style>

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号