Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
DCloud
hello uni-app x
提交
8c7ac2b5
H
hello uni-app x
项目概览
DCloud
/
hello uni-app x
通知
5995
Star
90
Fork
162
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
18
列表
看板
标记
里程碑
合并请求
1
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
H
hello uni-app x
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
18
Issue
18
列表
看板
标记
里程碑
合并请求
1
合并请求
1
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
8c7ac2b5
编写于
8月 29, 2023
作者:
H
hdx
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
swiper-list: 优化tab布局;调整蛇形线算法,同时兼容微信webview和skyline
上级
6e20ac9d
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
36 addition
and
53 deletion
+36
-53
pages/template/swiper-list/swiper-list.uvue
pages/template/swiper-list/swiper-list.uvue
+36
-53
未找到文件。
pages/template/swiper-list/swiper-list.uvue
浏览文件 @
8c7ac2b5
...
@@ -3,11 +3,10 @@
...
@@ -3,11 +3,10 @@
<scroll-view class="swiper-tabs" :scroll-left="tabsScrollLeft" :scroll-x="true" :show-scrollbar="false">
<scroll-view class="swiper-tabs" :scroll-left="tabsScrollLeft" :scroll-x="true" :show-scrollbar="false">
<view>
<view>
<view class="flex-row">
<view class="flex-row">
<view class="swiper-tabs-item" v-for="(item, index) in swiperList" :id="'swipertab' + index" ref="swipertab"
<text class="swiper-tabs-item" :class="swiperIndex==index ? 'swiper-tabs-item-active' : ''"
:key="index" @click="onTabClick(index)">
v-for="(item, index) in swiperList" ref="swipertab" :key="index" @click="onTabClick(index)">
<text class="swiper-tabs-item-text"
{{item.title}}
:class="swiperIndex==index ? 'uni-tab-item-title-active' : ''">{{item.title}}</text>
</text>
</view>
</view>
</view>
<view class="swiper-tabs-indicator">
<view class="swiper-tabs-indicator">
<view class="swiper-tabs-underline"
<view class="swiper-tabs-underline"
...
@@ -15,8 +14,8 @@
...
@@ -15,8 +14,8 @@
</view>
</view>
</view>
</view>
</scroll-view>
</scroll-view>
<swiper class="swiper-view" ref="swiper" :current="swiperIndex"
:duration="300" @change="onSwiperChange
"
<swiper class="swiper-view" ref="swiper" :current="swiperIndex"
@transition="onSwiperTransition
"
@
transition="onSwiperTransition" @
animationfinish="onSwiperAnimationfinish">
@animationfinish="onSwiperAnimationfinish">
<swiper-item class="swiper-item" v-for="(_, index) in swiperList" :key="index">
<swiper-item class="swiper-item" v-for="(_, index) in swiperList" :key="index">
<text class="swiper-item-text">{{index}}</text>
<text class="swiper-item-text">{{index}}</text>
</swiper-item>
</swiper-item>
...
@@ -31,7 +30,7 @@
...
@@ -31,7 +30,7 @@
}
}
type SwiperViewItem = {
type SwiperViewItem = {
title : string
title : string
,
}
}
export default {
export default {
...
@@ -42,53 +41,49 @@
...
@@ -42,53 +41,49 @@
tabsScrollLeft: 0,
tabsScrollLeft: 0,
swiperIndicatorLineLeft: 0,
swiperIndicatorLineLeft: 0,
swiperIndicatorLineWidth: 0,
swiperIndicatorLineWidth: 0,
$
lastSwiper
Index: 0,
$
animationFinish
Index: 0,
$swiperWidth: 0,
$swiperWidth: 0,
$swiperTabsRect: [] as SwiperTabsItem[],
$swiperTabsRect: [] as SwiperTabsItem[]
$isTap: false
}
}
},
},
onLoad() {
onLoad() {
for (let i = 0; i < 8; i++) {
for (let i = 0; i < 8; i++) {
const space = " ".repeat(i)
this.swiperList.push({
this.swiperList.push({
title: "T
AB "
+ i
title: "T
ab " + space
+ i
} as SwiperViewItem)
} as SwiperViewItem)
}
}
},
},
onReady() {
onReady() {
this.$swiperWidth = (this.$refs["swiper"]
! as INode)!?.offsetWidth! as number;
this.$swiperWidth = (this.$refs["swiper"]
as INode)!.offsetWidth! as number
this.queryTabItemsSize()
;
this.queryTabItemsSize()
this.setSwiperIndex(0, true)
;
this.setSwiperIndex(0, true)
},
},
methods: {
methods: {
onTabClick(index : number) {
onTabClick(index : number) {
this.setSwiperIndex(index, false);
this.setSwiperIndex(index, false)
},
onSwiperChange(e : SwiperChangeEvent) {
this.setSwiperIndex(e.detail.current, false);
console.log("onSwiperChange", e.detail.current);
},
},
onSwiperTransition(e : SwiperTransitionEvent) {
onSwiperTransition(e : SwiperTransitionEvent) {
const offsetX = e.detail.dx;
const offsetX = e.detail.dx
// 兼容微信 skyline 和 webview
const offsetIndex = Math.abs(offsetX.toInt()) == this.$swiperWidth ? 0 : offsetX / this.$swiperWidth
const currentIndex = this.$animationFinishIndex + Math.abs(offsetIndex.toInt())
let moveToIndex = offsetX > 0 ?
this.$lastSwiperIndex + 1 : this.$lastSwiper
Index - 1
let moveToIndex = offsetX > 0 ?
currentIndex + 1 : current
Index - 1
if (moveToIndex < 0) { moveToIndex = 0 }
if (moveToIndex < 0) { moveToIndex = 0 }
if (moveToIndex > this.$swiperTabsRect.length - 1) { moveToIndex = this.$swiperTabsRect.length - 1 }
if (moveToIndex > this.$swiperTabsRect.length - 1) { moveToIndex = this.$swiperTabsRect.length - 1 }
const percentage = Math.abs(offsetX) / this.$swiperWidth
;
const percentage = Math.abs(offsetX) / this.$swiperWidth
const currentSize = this.$swiperTabsRect[
this.$lastSwiperIndex];
const currentSize = this.$swiperTabsRect[
currentIndex]
const moveToSize = this.$swiperTabsRect[moveToIndex]
;
const moveToSize = this.$swiperTabsRect[moveToIndex]
const indicatorlineL = currentSize.left + (moveToSize.left - currentSize.left) * percentage
;
const indicatorlineL = currentSize.left + (moveToSize.left - currentSize.left) * percentage
const indicatorlineW = currentSize.width + (moveToSize.width - currentSize.width) * percentage
;
const indicatorlineW = currentSize.width + (moveToSize.width - currentSize.width) * percentage
this.updateTabIndicator(indicatorlineL, indicatorlineW);
this.updateTabIndicator(indicatorlineL, indicatorlineW)
//console.log(this.$lastSwiperIndex, moveToIndex, offsetX, this.$swiperWidth, percentage);
},
},
// TODO 事件触发时机不对
onSwiperAnimationfinish(e : SwiperAnimationFinishEvent) {
onSwiperAnimationfinish(e : SwiperAnimationFinishEvent) {
this.$lastSwiperIndex = e.detail.current;
this.setSwiperIndex(e.detail.current, true)
console.log("onSwiperAnimationfinish", e.detail.current);
this.$animationFinishIndex = e.detail.current
// this.setSwiperIndex(e.detail.current, true);
},
},
queryTabItemsSize() {
queryTabItemsSize() {
this.$swiperTabsRect.length = 0;
this.$swiperTabsRect.length = 0;
...
@@ -102,21 +97,18 @@
...
@@ -102,21 +97,18 @@
},
},
setSwiperIndex(index : Number, updateIndicator : Boolean) {
setSwiperIndex(index : Number, updateIndicator : Boolean) {
if (this.swiperIndex === index) {
if (this.swiperIndex === index) {
return
;
return
}
}
this.swiperIndex = index
;
this.swiperIndex = index
if (updateIndicator) {
if (updateIndicator) {
this.updateTabIndicator(this.$swiperTabsRect[index].left, this.$swiperTabsRect[index].width)
;
this.updateTabIndicator(this.$swiperTabsRect[index].left, this.$swiperTabsRect[index].width)
}
}
},
},
updateTabIndicator(left : Number, width : Number) {
updateTabIndicator(left : Number, width : Number) {
this.swiperIndicatorLineLeft = left;
this.swiperIndicatorLineLeft = left
this.swiperIndicatorLineWidth = width;
this.swiperIndicatorLineWidth = width
const offset = left + width / 2;
this.tabsScrollLeft = left + width / 2 - this.$swiperWidth / 2
if (offset > this.$swiperWidth / 2) {
this.tabsScrollLeft = offset - this.$swiperWidth / 2;
}
}
}
}
}
}
}
...
@@ -136,15 +128,12 @@
...
@@ -136,15 +128,12 @@
}
}
.swiper-tabs-item {
.swiper-tabs-item {
padding: 12px 25px;
}
.swiper-tabs-item-text {
color: #555;
color: #555;
font-size: 16px;
font-size: 16px;
padding: 12px 25px;
}
}
.swiper-tabs-item-
text-
active {
.swiper-tabs-item-active {
color: #007AFF;
color: #007AFF;
}
}
...
@@ -161,11 +150,6 @@
...
@@ -161,11 +150,6 @@
background-color: #007AFF;
background-color: #007AFF;
}
}
.swiper-tabs-animation {
transition-duration: 0.2s;
transition-property: left;
}
.swiper-view {
.swiper-view {
flex: 1;
flex: 1;
}
}
...
@@ -179,6 +163,5 @@
...
@@ -179,6 +163,5 @@
.swiper-item-text {
.swiper-item-text {
font-size: 72px;
font-size: 72px;
font-weight: bold;
font-weight: bold;
margin: auto;
}
}
</style>
</style>
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录