效果图:
tab栏可以滑动,切换页面跟随tab栏同步滑动。这里需要注意的是使用swiper组件时,它会有一个默认的高度,你必须动态的获取数据列表的高度覆盖原来的默认高度。
经过上一篇的问题,切换页面高度都统一了,现在进行了处理切换每个页面高度都自适应当前高度。
最下面有精简代码
swiper 滑块视图容器
current 当前index索引
swiper-item 仅可放置在
<swiper>
组件中
示例
<swiper :current="currentTab" @change="bindChange">
<swiper-item>
//容器里的内容....
</swiper-item>
</swiper>
注:<u-alert>和<u-swiper> 为uview,需要到官方导入组件uView2.0组件导入
如不需要删掉<u-alert>和<u-swiper>代码,不然会报错
代码
html
<template>
<view>
<scroll-view class="scroll1" scroll-x="true">
<view :class="currentTab==index ? 'select' : 'select1'" :data-current="index" @click="swichNav"
v-for="(item,index) in scoll" :key='index'>{{item.txt}}
</view>
</scroll-view>
<view v-if="infos == 1" class="infos">
<swiper class="info" autoplay vertical circular interval="3000">
<swiper-item v-for="(item,index) in info" :key='index'>
<view class="info_item">
<image src="../../static/logo.png"></image>
<text>{{item.user}}</text><text>已下单</text>
</view>
</swiper-item>
</swiper>
</view>
<swiper :current="currentTab" @change="bindChange" class='swp' :style="{height:aheight?aheight+'px':'auto'}">
<swiper-item>
<view class="list-item">
<view class="list" v-for="(item,index) in list" :key='index'>
<view class="list-img">
<image src="/static/zatu/2.jpg" mode="aspectFit"></image>
</view>
<view class="list-con">
<view class="title">{{item.title}}</view>
<view class="list-foot">
<view style="width: 150rpx;">
<view class="price1">¥{{item.user}}</view>
</view>
<view style="width: 150rpx; text-align: right;display: inline-block;">
<view class="price2">¥{{item.like}}</view>
</view>
</view>
</view>
</view>
</view>
</swiper-item>
<swiper-item>
<view class="item_tv">直播间</view>
</swiper-item>
<swiper-item style="margin: 0rpx; 10rpx">
<view class="item_image">
<view class="marhin_view">
<u-alert :title="text1" type="error"></u-alert>
<u-swiper :list="list1" indicator indicatorMode="dot" circular></u-swiper>
</view>
<view class="marhin_view">
<u-alert :title="text2" type="error"></u-alert>
<u-swiper indicator :list="list2" keyName="url" indicatorMode="dot" :autoplay="false">
</u-swiper>
</view>
<view class="marhin_view">
<u-alert :title="text3" type="error"></u-alert>
<view style="display: flex;align-items: center;justify-content: center;margin-top:5rpx;">
<u--image :src="list3" mode="widthFix" height="100%" shape="square" radius="10"></u--image>
</view>
</view>
</view>
</swiper-item>
</swiper>
</view>
</template>
核心代码
js
uni.createSelectorQuery() 获取元素节点
query.select('选择元素').boundingClientRect
<script>
export default {
data() {
return {
list1: [
'https://cdn.uviewui.com/uview/swiper/swiper1.png',
'https://cdn.uviewui.com/uview/swiper/swiper2.png',
'https://cdn.uviewui.com/uview/swiper/swiper3.png',
],
list2: [{
url: 'https://cdn.uviewui.com/uview/resources/video.mp4',
poster: 'https://cdn.uviewui.com/uview/swiper/swiper1.png'
}, {
url: 'https://cdn.uviewui.com/uview/swiper/swiper2.png',
}, {
url: 'https://cdn.uviewui.com/uview/swiper/swiper3.png',
}],
list3: '/static/zatu/1.jpg',
text1: '福利一览',
text2: '攻略教程',
text3: '转发海报',
currentTab: 0,
aheight: '',
infos: '1',
info: [{
user: '大魔王',
}, {
user: '小魔女',
}, {
user: '名称长长的',
}],
scoll: [{
txt: '精选'
}, {
txt: '直播间'
}, {
txt: '代言福利'
}],
list: [{
user: '1000000',
title: 'ssssssssss',
like: '33333'
}, {
user: '2',
title: 'ssaassssssss',
like: '3333'
},
{
user: '3',
title: 'ss2ssssssss',
like: '33'
},
{
user: '4',
title: 'ss2ssssssss',
like: '33'
},
{
user: '5',
title: 'ss2ssssssss',
like: '33'
},
{
user: '6',
title: 'ss2ssssssss',
like: '33'
},
{
user: '7',
title: 'ss2ssssssss',
like: '33'
},
{
user: '8',
title: 'ss2ssssssss',
like: '33'
},
{
user: '9',
title: 'ss2ssssssss',
like: '33'
},
{
user: '10',
title: 'ss2ssssssss',
like: '33'
}
]
}
},
onShow() {
//当前的页面swiper高度
const query = uni.createSelectorQuery().in(this);
query.select('.list-item').boundingClientRect(data => {
this.aheight = data.height
}).exec();
},
methods: {
// 滑动页面同步tab栏
bindChange: function(e) {
this.currentTab = e.detail.current
// 动态获取滑动页面高度
const query = uni.createSelectorQuery().in(this);
if (this.currentTab == 0) {
query.select('.list-item').boundingClientRect(data => {
this.aheight = data.height
}).exec();
} else if (this.currentTab == 1) {
query.select('.item_tv').boundingClientRect(data => {
this.aheight = data.height
}).exec();
} else {
query.select('.item_image').boundingClientRect(data => {
this.aheight = data.height
}).exec();
}
},
//点击tab切换
swichNav: function(e) {
var that = this;
if (this.currentTab === e.target.dataset.current) {
return false;
} else {
this.currentTab = e.target.dataset.current
}
}
}
}
</script>
css
<style scoped lang="scss">
.scroll1 {
position: fixed;
top: 0rpx;
z-index: 99;
width: 100%;
white-space: nowrap;
padding: 10rpx 20rpx;
background-color: #ffffff;
& view {
white-space: normal;
display: inline-block;
}
& view:before {
content: '|';
color: #f4f4f4;
margin: 0 20rpx;
}
& view:first-child:before {
display: none;
}
.select {
color: #fb6583;
font-size: 40rpx;
}
.select1 {
color: $uni-text-color-placeholder;
}
}
.infos {
position: fixed;
top: 90rpx;
z-index: 99;
border-top-right-radius: 90rpx;
border-bottom-right-radius: 90rpx;
padding: 5rpx;
background-color: rgba(0, 0, 0, 0.5);
.info {
height: 40rpx;
width: 250rpx;
swiper-item {
.info_item {
display: flex;
align-items: center;
justify-content: center;
image {
border-radius: 90rpx;
width: 35rpx;
height: 35rpx;
}
text {
margin-left: 10rpx;
color: #ffffff;
font-size: 20rpx;
}
}
}
}
}
.swp {
margin-top: 70rpx;
.list-item {
overflow: hidden;
.list {
display: flex;
margin: 10rpx;
padding: 15rpx;
border-bottom: 1rpx solid #fb6583;
.list-img {
background-color: #fef0f0;
width: 400rpx;
height: 400rpx;
display: flex;
align-items: center;
justify-content: center;
border-radius: 12rpx;
image {
border-radius: 12rpx;
width: 380rpx;
height: 380rpx;
}
}
.list-con {
margin-left: 20rpx;
.title {
font-size: 45rpx;
}
.list-foot {
margin-top: 250rpx;
display: flex;
align-items: center;
.price1 {
font-size: 25rpx;
}
.price2 {
// position: fixed;
font-size: 45rpx;
color: #fb6583;
}
}
}
}
}
}
.item_image {
.marhin_view {
margin: 10rpx;
}
}
</style>
精简代码
HTML
<!-- 顶部导航 -->
<scroll-view class="scroll1" scroll-x="true">
<view :class="currentTab==index ? 'select' : 'select1'" :data-current="index" @click="swichNav" v-for="(item,index) in scoll" :key='index'>{{item.txt}}
</view>
</scroll-view>
<!-- 轮播 -->
<swiper :current="currentTab" @change="bindChange" class='swp' :style="{height:aheight?aheight+'px':'auto'}">
<!-- 轮播1 -->
<swiper-item>
<view class="list-item">//注意这里是节点
//....放你的内容
<image src="../../../../static/gwc.png"></image>
<view>大魔王</view>
<view>3652</view>
</view>
</swiper-item>
<!-- 轮播2 -->
<swiper-item>
<view class="item_tv">
//....内容
</view>
</swiper-item>
<!-- 轮播3 -->
<swiper-item>
<view class="item_image">
//....内容
</view>
</swiper-item>
</swiper>
JS
<script>
export default {
data() {
return {
currentTab: 0,//默认菜单
aheight: '',
scoll: [{
txt: '精选'
}, {
txt: '直播间'
}, {
txt: '代言福利'
}],
}
},
onShow() {
//当前的页面swiper高度,query.select('.list-item'),.list-item就是你的类节点
const query = uni.createSelectorQuery().in(this);
query.select('.list-item').boundingClientRect(data => {
this.aheight = data.height
}).exec();
},
methods: {
// 滑动页面同步tab栏query.select('.list-item'),类节点
bindChange: function(e) {
this.currentTab = e.detail.current
// 动态获取滑动页面高度
const query = uni.createSelectorQuery().in(this);
if (this.currentTab == 0) {
query.select('.list-item').boundingClientRect(data => {
this.aheight = data.height
}).exec();
} else if (this.currentTab == 1) {
query.select('.item_tv').boundingClientRect(data => {
this.aheight = data.height
}).exec();
} else {
query.select('.item_image').boundingClientRect(data => {
this.aheight = data.height
}).exec();
}
},
//点击tab切换
swichNav: function(e) {
var that = this;
if (this.currentTab === e.target.dataset.current) {
return false;
} else {
this.currentTab = e.target.dataset.current
}
}
}
}
</script>
CSS
<style lang="scss">
.scroll1 {
position: fixed;
top: 0rpx;
z-index: 99;
width: 100%;
white-space: nowrap;
padding: 10rpx 20rpx;
background-color: #ffffff;
& view {
white-space: normal;
display: inline-block;
}
& view:before {
content: '|';
color: #f4f4f4;
margin: 0 20rpx;
}
& view:first-child:before {
display: none;
}
.select {
color: #fb6583;
font-size: 40rpx;
}
.select1 {
color: $uni-text-color-placeholder;
}
}
.swp {
margin-top: 70rpx;
.list-item {
overflow: hidden;
.list {
display: flex;
margin: 10rpx;
padding: 15rpx;
border-bottom: 1rpx solid #fb6583;
}
}
}
</style>