scro-view
<!-- 顶部选项卡 -->
<!-- scroll-row设置宽度100%,且不能换行 自定义样式-->
<!-- border-bottom加上这个下边线,元素将可以滚动了 border-white隐藏border 自定义样式-->
<!-- scroll-into-view保存对应子元素id,当子元素id与scroll-into-view相同时,会
让轮播图自动滚动到对应id的子元素 -->
// scroll-with-animation为组件添加过渡动画
<scroll-view scroll-x="true"
:scroll-into-view="scrollInto"
scroll-with-animation
class="scroll-row border-bottom border-white">
<!-- scroll-row-item设置元素为行内块 -->
<view v-for="(item,index) in tabBars"
class="scroll-row-item px-2 py-2 font-md "
:id="'tab' + index"
@click="clickTab(index)"
:class="tabIndex === index ? 'text-main font-lg font-weight-bold' : ''"
:key="index">
{{item.name}}
</view>
</scroll-view>
swiper
如果swiper没有显示,可能是没有高度,或宽度
swiper和scroll-view组合,形成左右滑动翻页,和上下滑动,外面依层swiper管理左右滑动,内部的scroll-view管理上下滑动
<!-- tabbar内容区 -->
<!-- indicator-dots指示点,一般轮播图有用到 -->
<!-- autoplay是否自动播放 interval自动播放的事件间隔-->
<!-- duration滑块过度时间 -->
<!-- circular是否采用衔接滑动 -->
<!-- vertical是否设置滑动方向为纵向 -->
<!-- current当前滑块的index值 可以通过current属性一顶部选项卡关联-->
<swiper
class="w-100"
:style="'height:'+scrollHeight + 'px;'"
:duration="150" :current="tabIndex" @change="onchangeTab">
<swiper-item v-for="(item,index) in newsList"
:key="index">
<scroll-view scroll-y="true" :style="'height:'+scrollHeight + 'px;'">
<block v-for="(item1,index1) in item.list" :key="index1">
<!-- 通过父组件向子组件传值的方式将数据传递给子组件 -->
<commonList :item="item1"
@follow="follow"
@doSupport="doSupport"
ref="commonList"
:index="index1"></commonList>
<!-- 分割线 -->
<divider></divider>
</block>
</scroll-view>
</swiper-item>
</swiper>
计算页面高度
onLoad() {
// 获取系统信息
// 有两个方法,一个异步,一个同步
//获取到的高度不包括顶部导航栏和底部tabBar
uni.getSystemInfo({
success:res => {
// 成功获取系统信息时,返回的数据
// res.windowHeight获取到的高度时px
// uni.upx2px()将高度转换为px
this.scrollHeight = res.windowHeight - uni.upx2px(100)
// 将数据绑定到列表上面
}
})
},
上拉加载更多
//@scrolltolower="loadmore(index)" 是scroll-view自带的触底监听事件
<swiper
class="w-100"
:style="'height:'+scrollHeight + 'px;'"
:duration="150" :current="tabIndex" @change="onchangeTab">
<swiper-item v-for="(item,index) in newsList"
:key="index">
<scroll-view scroll-y="true"
@scrolltolower="loadmore(index)"
:style="'height:'+scrollHeight + 'px;'">
<block v-for="(item1,index1) in item.list" :key="index1">
<!-- 通过父组件向子组件传值的方式将数据传递给子组件 -->
<commonList :item="item1"
@follow="follow"
@doSupport="doSupport"
ref="commonList"
:index="index1"></commonList>
<!-- 分割线 -->
<divider></divider>
</block>
<!-- 上拉加载 -->
<view class="flex align-center justify-center py-3 ">
<text class="font text-light-muted">{{item.loadmore}}</text>
</view>
</scroll-view>
</swiper-item>
</swiper>
// 上拉加载更多
loadmore(index){
// 获取当前列表
let item = this.newsList[index]
// 出发后改变显示信息,为"加载中",修改当前列表的列表加载状态
this.newsList[index].loadmore = "加载中..."
// 模拟数据请求
setTimeout(() => {
// 加载数据
item.list = [...item.list,...item.list]
// 恢复加载状态
this.newsList[index].loadmore = "上拉加载更多"
},2000)
}