| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322 |
- <script>
- import ChatWrapper from "@/components/chat-wrapper/ChatWrapper.vue";
- import LiveFooter from "@/components/live-footer/LiveFooter.vue";
- import LiveHeader from "@/components/live-header/LiveHeader.vue";
- export default {
- name: "LiveInteract",
- model: {
- event: "change",
- prop: "value",
- },
- props: {
- value: {
- type: Boolean,
- default: false,
- }
- },
- components: {LiveHeader, LiveFooter, ChatWrapper},
- data() {
- return {
- swiperDom: null,
- currentOffset: 0,
- startX: 0,
- startY: 0,
- isDragging: false,
- slideWidth: 0,
- currentIndex: 1,
- // 是否是横屏
- isLandscape: false,
- }
- },
- mounted() {
- },
- methods: {
- // 点击返回
- handleClickBack() {
- // 判断当前状态是否全屏,如果是则退出全屏
- if (this.isLandscape) {
- this.isLandscape = false;
- return;
- }
- // 不是则退出直播页
- },
- // 获取横屏基础信息
- getLandscapeInfo() {
- this.swiperDom = document.querySelector(".van-swipe__track");
- // 设置 所有的van-swipe-item 的宽度为屏幕宽度
- const swiperItemList = document.querySelectorAll(".van-swipe-item");
- const width = this.$refs.interactRef.offsetWidth;
- swiperItemList.forEach(item => {
- item.style.width = `${width}px`;
- })
- this.slideWidth = width;
- this.currentOffset = -this.slideWidth * this.currentIndex; // 初始偏移量
- this.swiperDom.style.transform = `translateX(${this.currentOffset}px)`;
- },
- // 触摸开始
- handleTouchStart(event) {
- if(!this.isLandscape) return;
- this.startX = event.touches[0].clientX;
- this.startY = event.touches[0].clientY;
- this.isDragging = true;
- // this.swiperDom.style.transitionDuration = '0ms';
- },
- handleTouchEnd() {
- if (!this.isDragging) return;
- this.isDragging = false;
- // this.swiperDom.style.transitionDuration = "500ms";
- // 计算滑动距离
- const swipeDistance = this.currentOffset - (-this.slideWidth * this.currentIndex);
- // 判断滑动距离是否超过容器宽度的一半
- if (Math.abs(swipeDistance) >= this.slideWidth / 2) {
- if (swipeDistance < 0) {
- // 向右滑动超过一半,切换到下一个item
- if (this.currentIndex < 1) {
- this.currentIndex++;
- }
- } else {
- // 向左滑动超过一半,切换到上一个item
- if (this.currentIndex > 0) {
- this.currentIndex--;
- }
- }
- }
- // 根据新的索引计算新的偏移量
- this.currentOffset = -this.slideWidth * this.currentIndex;
- // 更新DOM的transform属性
- this.swiperDom.style.transform = `translateX(${this.currentOffset}px)`;
- },
- handleTouchMove(event) {
- if (!this.isDragging) return;
- const moveY = event.touches[0].clientY;
- const moveX = event.touches[0].clientX;
- const diffY = moveY - this.startY;
- const diffX = moveX - this.startX;
- // 如果主要是水平滑动,则不处理
- if (Math.abs(diffX) > Math.abs(diffY)) return;
- // 根据上下滑动调整水平偏移量
- this.currentOffset += diffY * 0.5; // 向上拖动时向左移动
- // 限制移动范围
- const maxWidth = -this.slideWidth;
- if (this.currentOffset >= this.slideWidth || this.currentOffset <= maxWidth) return;
- // 更新DOM的transform属性
- this.swiperDom.style.transform = `translateX(${this.currentOffset}px)`;
- }
- },
- watch: {
- isLandscape: {
- handler() {
- this.$emit("change", this.isLandscape);
- },
- immediate: true,
- },
- value: {
- handler() {
- this.isLandscape = this.value;
- this.$nextTick(() => {
- this.getLandscapeInfo();
- })
- },
- immediate: true,
- deep: true,
- }
- }
- }
- </script>
- <template>
- <div class="interact-container"
- ref="interactRef"
- :class="{'horiz-wrapper': isLandscape}"
- @touchmove="handleTouchMove"
- @touchend="handleTouchEnd"
- @touchstart="handleTouchStart">
- <van-swipe class="my-swipe"
- :autoplay="0"
- @change="(e) => currentIndex = e"
- :touchable="!isLandscape"
- :stop-propagation="false"
- :loop="false"
- :initial-swipe="currentIndex"
- :show-indicators="false">
- <van-swipe-item>
- <img src="@/assets/live/arrow-left.png" class="arrow-left" @click="handleClickBack" />
- <div class="exit-box">
- <img src="@/assets/live/exit.png" class="exit-icon" />
- <span>退出清屏</span>
- </div>
- </van-swipe-item>
- <van-swipe-item>
- <div class="interact-box">
- <live-header @back="handleClickBack" :is-full-screen="isLandscape" />
- <div class="footer">
- <chat-wrapper v-show="!isLandscape" />
- <live-footer :is-full-screen="isLandscape" />
- <div class="shop-and-gift">
- <div class="guide-box">
- <img src="https://picsum.photos/200" class="guide-cover" />
- <img src="@/assets/close.png" class="close-icon" />
- </div>
- <div class="gift-box">
- <img src="@/assets/live/gift.png" class="gift-icon" />
- </div>
- </div>
- </div>
- </div>
- <!-- 横屏显示-->
- <div v-show="isLandscape" class="live-horiz-tip">市场有风险 投资需谨慎</div>
- </van-swipe-item>
- </van-swipe>
- </div>
- </template>
- <style scoped>
- .interact-container {
- transition: all 0.3s;
- }
- .horiz-wrapper {
- width: 100vh;
- height: 100vw;
- position: fixed;
- top: calc(50% - 50vw);
- left: calc(50% - 50vh);
- z-index: 10;
- //background: yellowgreen;
- background: transparent;
- transform: rotate(90deg);
- }
- .my-swipe {
- color: #FFFFFF;
- height: 100%;
- }
- .interact-box {
- width: 100%;
- height: 100%;
- display: flex;
- flex-direction: column;
- justify-content: space-between;
- box-sizing: border-box;
- padding: 14px 0 0;
- }
- .shop-and-gift {
- width: fit-content;
- position: absolute;
- right: 8px;
- bottom: 62px;
- display: flex;
- flex-direction: column;
- align-items: flex-end;
- }
- .gift-box {
- width: 40px;
- height: 40px;
- border-radius: 20px;
- background: rgba(26, 40, 55, 0.5);
- box-shadow: 0px 0px 4px 0px rgba(0,0,0,0.24);
- display: flex;
- align-items: center;
- justify-content: center;
- }
- .gift-icon {
- width: 20px;
- height: 20px;
- }
- .guide-box {
- width: 80px;
- height: 100px;
- border-radius: 8px;
- position: relative;
- margin-bottom: 16px;
- }
- .guide-cover {
- width: 80px;
- height: 100px;
- border-radius: 8px;
- }
- .close-icon {
- width: 20px;
- height: 20px;
- position: absolute;
- top: 4px;
- right: 4px;
- }
- /*第一屏*/
- .arrow-left {
- width: 28px;
- height: 28px;
- position: absolute;
- left: 20px;
- top: 8px;
- }
- .exit-box {
- width: 112px;
- height: 36px;
- background: rgba(26, 40, 55, 0.5);
- border-radius: 18px;
- display: flex;
- align-items: center;
- justify-content: center;
- font-weight: 400;
- font-size: 14px;
- color: #FFFFFF;
- column-gap: 8px;
- position: absolute;
- bottom: 20px;
- right: 24px;
- }
- .exit-icon {
- width: 16px;
- height: 20px;
- }
- .live-horiz-tip {
- width: 100vh;
- box-sizing: border-box;
- padding: 6px 20px;
- position: absolute;
- bottom: 0;
- font-weight: 400;
- font-size: 12px;
- color: #FFFFFF;
- background: rgba(26, 40, 55, 0.5);
- }
- .horiz-wrapper .interact-box {
- width: 100vh;
- height: 100vw;
- }
- .horiz-wrapper .footer {
- //width: 100vh;
- position: relative;
- }
- </style>
|