LiveInteract.vue 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. <script>
  2. import ChatWrapper from "@/components/chat-wrapper/ChatWrapper.vue";
  3. import LiveFooter from "@/components/live-footer/LiveFooter.vue";
  4. import LiveHeader from "@/components/live-header/LiveHeader.vue";
  5. export default {
  6. name: "LiveInteract",
  7. model: {
  8. event: "change",
  9. prop: "value",
  10. },
  11. props: {
  12. value: {
  13. type: Boolean,
  14. default: false,
  15. }
  16. },
  17. components: {LiveHeader, LiveFooter, ChatWrapper},
  18. data() {
  19. return {
  20. swiperDom: null,
  21. currentOffset: 0,
  22. startX: 0,
  23. startY: 0,
  24. isDragging: false,
  25. slideWidth: 0,
  26. currentIndex: 1,
  27. // 是否是横屏
  28. isLandscape: false,
  29. }
  30. },
  31. mounted() {
  32. },
  33. methods: {
  34. // 点击返回
  35. handleClickBack() {
  36. // 判断当前状态是否全屏,如果是则退出全屏
  37. if (this.isLandscape) {
  38. this.isLandscape = false;
  39. return;
  40. }
  41. // 不是则退出直播页
  42. },
  43. // 获取横屏基础信息
  44. getLandscapeInfo() {
  45. this.swiperDom = document.querySelector(".van-swipe__track");
  46. // 设置 所有的van-swipe-item 的宽度为屏幕宽度
  47. const swiperItemList = document.querySelectorAll(".van-swipe-item");
  48. const width = this.$refs.interactRef.offsetWidth;
  49. swiperItemList.forEach(item => {
  50. item.style.width = `${width}px`;
  51. })
  52. this.slideWidth = width;
  53. this.currentOffset = -this.slideWidth * this.currentIndex; // 初始偏移量
  54. this.swiperDom.style.transform = `translateX(${this.currentOffset}px)`;
  55. },
  56. // 触摸开始
  57. handleTouchStart(event) {
  58. if(!this.isLandscape) return;
  59. this.startX = event.touches[0].clientX;
  60. this.startY = event.touches[0].clientY;
  61. this.isDragging = true;
  62. // this.swiperDom.style.transitionDuration = '0ms';
  63. },
  64. handleTouchEnd() {
  65. if (!this.isDragging) return;
  66. this.isDragging = false;
  67. // this.swiperDom.style.transitionDuration = "500ms";
  68. // 计算滑动距离
  69. const swipeDistance = this.currentOffset - (-this.slideWidth * this.currentIndex);
  70. // 判断滑动距离是否超过容器宽度的一半
  71. if (Math.abs(swipeDistance) >= this.slideWidth / 2) {
  72. if (swipeDistance < 0) {
  73. // 向右滑动超过一半,切换到下一个item
  74. if (this.currentIndex < 1) {
  75. this.currentIndex++;
  76. }
  77. } else {
  78. // 向左滑动超过一半,切换到上一个item
  79. if (this.currentIndex > 0) {
  80. this.currentIndex--;
  81. }
  82. }
  83. }
  84. // 根据新的索引计算新的偏移量
  85. this.currentOffset = -this.slideWidth * this.currentIndex;
  86. // 更新DOM的transform属性
  87. this.swiperDom.style.transform = `translateX(${this.currentOffset}px)`;
  88. },
  89. handleTouchMove(event) {
  90. if (!this.isDragging) return;
  91. const moveY = event.touches[0].clientY;
  92. const moveX = event.touches[0].clientX;
  93. const diffY = moveY - this.startY;
  94. const diffX = moveX - this.startX;
  95. // 如果主要是水平滑动,则不处理
  96. if (Math.abs(diffX) > Math.abs(diffY)) return;
  97. // 根据上下滑动调整水平偏移量
  98. this.currentOffset += diffY * 0.5; // 向上拖动时向左移动
  99. // 限制移动范围
  100. const maxWidth = -this.slideWidth;
  101. if (this.currentOffset >= this.slideWidth || this.currentOffset <= maxWidth) return;
  102. // 更新DOM的transform属性
  103. this.swiperDom.style.transform = `translateX(${this.currentOffset}px)`;
  104. }
  105. },
  106. watch: {
  107. isLandscape: {
  108. handler() {
  109. this.$emit("change", this.isLandscape);
  110. },
  111. immediate: true,
  112. },
  113. value: {
  114. handler() {
  115. this.isLandscape = this.value;
  116. this.$nextTick(() => {
  117. this.getLandscapeInfo();
  118. })
  119. },
  120. immediate: true,
  121. deep: true,
  122. }
  123. }
  124. }
  125. </script>
  126. <template>
  127. <div class="interact-container"
  128. ref="interactRef"
  129. :class="{'horiz-wrapper': isLandscape}"
  130. @touchmove="handleTouchMove"
  131. @touchend="handleTouchEnd"
  132. @touchstart="handleTouchStart">
  133. <van-swipe class="my-swipe"
  134. :autoplay="0"
  135. @change="(e) => currentIndex = e"
  136. :touchable="!isLandscape"
  137. :stop-propagation="false"
  138. :loop="false"
  139. :initial-swipe="currentIndex"
  140. :show-indicators="false">
  141. <van-swipe-item>
  142. <img src="@/assets/live/arrow-left.png" class="arrow-left" @click="handleClickBack" />
  143. <div class="exit-box">
  144. <img src="@/assets/live/exit.png" class="exit-icon" />
  145. <span>退出清屏</span>
  146. </div>
  147. </van-swipe-item>
  148. <van-swipe-item>
  149. <div class="interact-box">
  150. <live-header @back="handleClickBack" :is-full-screen="isLandscape" />
  151. <div class="footer">
  152. <chat-wrapper v-show="!isLandscape" />
  153. <live-footer :is-full-screen="isLandscape" />
  154. <div class="shop-and-gift">
  155. <div class="guide-box">
  156. <img src="https://picsum.photos/200" class="guide-cover" />
  157. <img src="@/assets/close.png" class="close-icon" />
  158. </div>
  159. <div class="gift-box">
  160. <img src="@/assets/live/gift.png" class="gift-icon" />
  161. </div>
  162. </div>
  163. </div>
  164. </div>
  165. <!-- 横屏显示-->
  166. <div v-show="isLandscape" class="live-horiz-tip">市场有风险 投资需谨慎</div>
  167. </van-swipe-item>
  168. </van-swipe>
  169. </div>
  170. </template>
  171. <style scoped>
  172. .interact-container {
  173. transition: all 0.3s;
  174. }
  175. .horiz-wrapper {
  176. width: 100vh;
  177. height: 100vw;
  178. position: fixed;
  179. top: calc(50% - 50vw);
  180. left: calc(50% - 50vh);
  181. z-index: 10;
  182. //background: yellowgreen;
  183. background: transparent;
  184. transform: rotate(90deg);
  185. }
  186. .my-swipe {
  187. color: #FFFFFF;
  188. height: 100%;
  189. }
  190. .interact-box {
  191. width: 100%;
  192. height: 100%;
  193. display: flex;
  194. flex-direction: column;
  195. justify-content: space-between;
  196. box-sizing: border-box;
  197. padding: 14px 0 0;
  198. }
  199. .shop-and-gift {
  200. width: fit-content;
  201. position: absolute;
  202. right: 8px;
  203. bottom: 62px;
  204. display: flex;
  205. flex-direction: column;
  206. align-items: flex-end;
  207. }
  208. .gift-box {
  209. width: 40px;
  210. height: 40px;
  211. border-radius: 20px;
  212. background: rgba(26, 40, 55, 0.5);
  213. box-shadow: 0px 0px 4px 0px rgba(0,0,0,0.24);
  214. display: flex;
  215. align-items: center;
  216. justify-content: center;
  217. }
  218. .gift-icon {
  219. width: 20px;
  220. height: 20px;
  221. }
  222. .guide-box {
  223. width: 80px;
  224. height: 100px;
  225. border-radius: 8px;
  226. position: relative;
  227. margin-bottom: 16px;
  228. }
  229. .guide-cover {
  230. width: 80px;
  231. height: 100px;
  232. border-radius: 8px;
  233. }
  234. .close-icon {
  235. width: 20px;
  236. height: 20px;
  237. position: absolute;
  238. top: 4px;
  239. right: 4px;
  240. }
  241. /*第一屏*/
  242. .arrow-left {
  243. width: 28px;
  244. height: 28px;
  245. position: absolute;
  246. left: 20px;
  247. top: 8px;
  248. }
  249. .exit-box {
  250. width: 112px;
  251. height: 36px;
  252. background: rgba(26, 40, 55, 0.5);
  253. border-radius: 18px;
  254. display: flex;
  255. align-items: center;
  256. justify-content: center;
  257. font-weight: 400;
  258. font-size: 14px;
  259. color: #FFFFFF;
  260. column-gap: 8px;
  261. position: absolute;
  262. bottom: 20px;
  263. right: 24px;
  264. }
  265. .exit-icon {
  266. width: 16px;
  267. height: 20px;
  268. }
  269. .live-horiz-tip {
  270. width: 100vh;
  271. box-sizing: border-box;
  272. padding: 6px 20px;
  273. position: absolute;
  274. bottom: 0;
  275. font-weight: 400;
  276. font-size: 12px;
  277. color: #FFFFFF;
  278. background: rgba(26, 40, 55, 0.5);
  279. }
  280. .horiz-wrapper .interact-box {
  281. width: 100vh;
  282. height: 100vw;
  283. }
  284. .horiz-wrapper .footer {
  285. //width: 100vh;
  286. position: relative;
  287. }
  288. </style>