直播系统搭建,实现页面多级来回切换支持滑动和点击操作

直播系统搭建,实现页面多级来回切换支持滑动和点击操作

通过触发pagechange1方法中的事件对currentIndex来进行赋值,又通过currentIndex的改变使前端wxml对应更改,这个部分对滑动和点击的操作都一样,无非就是使currentIndex对应到各自的位置,通过数字来决定位置

 


//滑动
  pagechange1: function (ee) {
    if ("touch" === ee.detail.source) {
      let currentPageIndex = this.data.currentIndex;
      currentPageIndex = (currentPageIndex + 1) % 2;
     
      this.setData({
        currentIndex: currentPageIndex,
      })
    }
  },
//点击tab时触发
  titleClick: function (e) {
    this.setData({
      //拿到当前索引并动态改变
      currentIndex: e.currentTarget.dataset.idx
    })
  },

 

这个部分完整代码如下:

wxml


<view>
    <!-- Tab布局 -->
    <view class='navBox'>
        <view class='titleBox' bindtap='titleClick' data-idx='0'>
            <text class="{{0 == currentIndex ? 'fontColorBox' : ''}}">安卓</text>
            <hr class="{{0 == currentIndex ? 'lineBox' : 'notLineBox'}}" />
        </view>
        <view class='titleBox' bindtap='titleClick' data-idx='1'>
            <text class="{{1 == currentIndex ? 'fontColorBox1' : ''}}">苹果</text>
            <hr class="{{1 == currentIndex ? 'lineBox' : 'notLineBox'}} " />
        </view>
    </view>
    <!-- 内容布局 -->
    <swiper class='swiperTtemBox' bindchange='pagechange1' current='{{currentIndex}}'>
        <swiper-item class='swiperTtemBox'>
            <view>内容1</view>
        </swiper-item>
        <swiper-item class='swiperTtemBox'>
            <view>内容2</view>
        </swiper-item>
    </swiper>
</view>

 

wxss

 


Page {
  /* 全局样式 */
  background: rgb(244, 245, 249);
  height: 100%;
  position: fixed;
}
.fontColorBox,
.fontColorBox1 {
  /* 文字默认颜色 */
  color: black;
}
.navBox {
  /* 顶部tab盒子样式 */
  width: 100%;
  height: 108rpx;
  background: white;
  display: flex;
  align-items: center;
  justify-content: center;
}
.navBox view:last-child {
  /* 最后一个tab标题的样式 */
  padding-left: 20%;
}
.titleBox {
  /* 未选中文字的样式 */
  color: rgb(168, 170, 175);
  font-size: 30rpx;
  display: flex;
  flex-direction: column;
  align-items: center;
}
.lineBox,.notLineBox{
  /* 选中及未选中底线共同样式 */
  width: 32rpx;
  height: 8rpx;
}
.lineBox {
  /* 选中底线样式 */
  background: rgb(43, 44, 45);
  margin-top: 16rpx;
  border-radius: 4rpx;
}
.notLineBox {
  /* 未选中底线样式 */
  background: transparent;
}
.swiperTtemBox {
  /* 底部内容样式 */
  height: 100vh;
  overflow: scroll;
  margin: 12rpx 0rpx;
  background: white;
  font-size: 28rpx;
}

 

js

 


const app = getApp()
Page({
  data: {
    currentIndex: 0, //默认第一个
  },
  pagechange1: function (ee) {
    if ("touch" === ee.detail.source) {
      let currentPageIndex = this.data.currentIndex;
      currentPageIndex = (currentPageIndex + 1) % 2;
     
      this.setData({
        currentIndex: currentPageIndex,
      })
    }
  },
  //点击tab时触发
  titleClick: function (e) {
    this.setData({
      //拿到当前索引并动态改变
      currentIndex: e.currentTarget.dataset.idx
    })
  },
})

 

以上就是直播系统搭建,实现页面多级来回切换支持滑动和点击操作, 更多内容欢迎关注之后的文章

 

posted @ 2022-11-09 14:20  云豹科技-苏凌霄  阅读(109)  评论(0)    收藏  举报