app-video.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <template>
  2. <view class="app-video app-content" :style="{width: width, height: height}">
  3. <view class="app-video app-image" @click="play" v-if="!start">
  4. <image :src="picUrl" class="app-image" style="width: 100%;height:100%"></image>
  5. <view class="app-play app-image">
  6. <icon class="app-play-icon"></icon>
  7. </view>
  8. </view>
  9. <video id="myVideo" @fullscreenchange="fullscreenChange" v-else class="app-video video" :style="{width: width, height: height}" :autoplay="start" show-center-play-btn @error="error" :src="url"></video>
  10. </view>
  11. </template>
  12. <script>
  13. import * as event from '../../../core/event.js';
  14. export default {
  15. name: "app-video",
  16. data() {
  17. return {
  18. start: false,
  19. fullScreen: false,
  20. }
  21. },
  22. props: {
  23. picUrl: {
  24. type: String,
  25. default: () => {
  26. return '';
  27. }
  28. },
  29. url: {
  30. type: String,
  31. default() {
  32. return '';
  33. }
  34. },
  35. width: {
  36. type: String,
  37. default() {
  38. return `750rpx`;
  39. }
  40. },
  41. height: {
  42. type: String,
  43. default() {
  44. return `422rpx`;
  45. }
  46. },
  47. },
  48. methods: {
  49. fullscreenChange(e) {
  50. this.fullScreen = e.detail.fullScreen;
  51. },
  52. play() {
  53. this.$nextTick().then(() => {
  54. this.start = true;
  55. });
  56. event.trigger(this.$const.EVENT_VIDEO_END);
  57. this.$emit('videoStart', true);
  58. event.on(this.$const.EVENT_VIDEO_END, true).then(() => {
  59. this.start = false;
  60. this.$emit('videoStart', false);
  61. });
  62. },
  63. autoEnd() {
  64. if (!this.start || this.fullScreen) return;
  65. let maxTop = uni.getSystemInfoSync().windowHeight;
  66. let query = null;
  67. /* #ifndef MP-ALIPAY */
  68. query = this.createSelectorQuery();
  69. /* #endif */
  70. /* #ifdef MP-ALIPAY */
  71. query = uni.createSelectorQuery();
  72. /* #endif */
  73. query.select('.video').boundingClientRect();
  74. query.selectViewport().scrollOffset();
  75. query.exec(res => {
  76. if (res[0].top <= -200 || res[0].top >= maxTop - 57) {
  77. event.trigger(this.$const.EVENT_VIDEO_END);
  78. }
  79. })
  80. }
  81. },
  82. computed: {
  83. scrollTop() {
  84. return this.$store.state.page.scrollTop;
  85. }
  86. },
  87. watch: {
  88. scrollTop: {
  89. handler(v) {
  90. this.autoEnd();
  91. },
  92. immediate: true
  93. }
  94. }
  95. }
  96. </script>
  97. <style scoped lang="scss">
  98. .app-content {
  99. position: relative;
  100. }
  101. .app-image {
  102. position: absolute;
  103. top: 0;
  104. left: 0;
  105. z-index: 100;
  106. width: 100%;
  107. height:100%;
  108. }
  109. .app-play-icon {
  110. width:#{130rpx};
  111. height: #{130rpx};
  112. background-image: url("../../../static/image/icon/play.png");
  113. background-size:100% 100%;
  114. position: absolute;
  115. top: 50%;
  116. z-index: 100;
  117. left: 50%;
  118. transform: translate(-50%, -50%);
  119. }
  120. </style>