Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
DCloud
hello uni-app x
提交
73e5e579
H
hello uni-app x
项目概览
DCloud
/
hello uni-app x
通知
6005
Star
91
Fork
164
代码
文件
提交
分支
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看板
提交
73e5e579
编写于
8月 30, 2023
作者:
H
hdx
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
swiper: 优化边界检查, 补充注释
上级
652e9bd0
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
76 addition
and
41 deletion
+76
-41
pages/template/long-list/long-list.uvue
pages/template/long-list/long-list.uvue
+37
-20
pages/template/swiper-list/swiper-list.uvue
pages/template/swiper-list/swiper-list.uvue
+39
-21
未找到文件。
pages/template/long-list/long-list.uvue
浏览文件 @
73e5e579
...
...
@@ -32,8 +32,8 @@
import longPage from './long-list-page.uvue';
type SwiperTabsItem = {
left
: number,
w
idth
: number
x
: number,
w : number
}
type SwiperViewItem = {
...
...
@@ -88,25 +88,42 @@
onTabClick(index : number) {
this.setSwiperIndex(index, false)
},
/// 兼容微信 skyline 和 webview, uni-app-x 和 webview 行为一致
/// skyline 每项完成触发 Animationfinish,偏移值重置
/// webview 全部完成触发 Animationfinish,偏移值累加
/// 在滑动到下一个项的过程中,再次反向滑动,偏移值递减
onSwiperTransition(e : SwiperTransitionEvent) {
const offsetX = e.detail.dx
// 兼容微信 skyline 和 webview
const offsetIndex = offsetX.toInt() / this.$swiperWidth
const currentIndex = this.$animationFinishIndex + offsetIndex.toInt()
let moveToIndex = offsetX > 0 ? currentIndex + 1 : currentIndex - 1
if (moveToIndex < 0) { moveToIndex = 0 }
if (moveToIndex > this.$swiperTabsRect.length - 1) { moveToIndex = this.$swiperTabsRect.length - 1 }
const offset_x = e.detail.dx
// 重置差异,计算当前实时索引位置
const current_offset_x = offset_x % this.$swiperWidth
const current_offset_i = offset_x / this.$swiperWidth
const current_index = this.$animationFinishIndex + current_offset_i.toInt()
// 边界检查
let move_to_index = current_index
if (current_offset_x > 0) {
if (move_to_index < this.$swiperTabsRect.length - 1) {
move_to_index += 1
}
} else if (current_offset_x < 0) {
if (move_to_index > 0) {
move_to_index -= 1
}
}
const percentage = Math.abs(offsetX % this.$swiperWidth) / this.$swiperWidth
const currentSize = this.$swiperTabsRect[currentIndex]
const moveToSize = this.$swiperTabsRect[moveToIndex]
const indicatorlineL = currentSize.left + (moveToSize.left - currentSize.left) * percentage
const indicatorlineW = currentSize.width + (moveToSize.width - currentSize.width) * percentage
// 计算蛇形线位置和大小
const percentage = Math.abs(current_offset_x) / this.$swiperWidth
const current_size = this.$swiperTabsRect[current_index]
const move_to_size = this.$swiperTabsRect[move_to_index]
const indicator_line_l = current_size.x + (move_to_size.x - current_size.x) * percentage
const indicator_line_w = current_size.w + (move_to_size.w - current_size.w) * percentage
this.updateTabIndicator(indicatorlineL, indicatorlineW)
// 更新蛇形线位置和大小
this.updateTabIndicator(indicator_line_l, indicator_line_w)
this.initSwiperItemPage(moveToIndex)
// 首次可见时初始化数据
this.initSwiperItemPage(move_to_index)
},
onSwiperAnimationfinish(e : SwiperAnimationFinishEvent) {
this.setSwiperIndex(e.detail.current, true)
...
...
@@ -117,8 +134,8 @@
const tabs = this.$refs["swipertab"] as INode[]
tabs.forEach((node) => {
this.$swiperTabsRect.push({
left
: node.offsetLeft,
w
idth
: node.offsetWidth
x
: node.offsetLeft,
w: node.offsetWidth
} as SwiperTabsItem)
})
},
...
...
@@ -131,7 +148,7 @@
this.initSwiperItemPage(index)
if (updateIndicator) {
this.updateTabIndicator(this.$swiperTabsRect[index].
left, this.$swiperTabsRect[index].width
)
this.updateTabIndicator(this.$swiperTabsRect[index].
x, this.$swiperTabsRect[index].w
)
}
},
updateTabIndicator(left : Number, width : Number) {
...
...
pages/template/swiper-list/swiper-list.uvue
浏览文件 @
73e5e579
...
...
@@ -25,8 +25,8 @@
<script>
type SwiperTabsItem = {
left
: number,
w
idth
: number
x
: number,
w : number
}
type SwiperViewItem = {
...
...
@@ -63,23 +63,41 @@
onTabClick(index : number) {
this.setSwiperIndex(index, false)
},
/// 兼容微信 skyline 和 webview, uni-app-x 和 webview 行为一致
/// skyline 每项完成触发 Animationfinish,偏移值重置
/// webview 全部完成触发 Animationfinish,偏移值累加
/// 在滑动到下一个项的过程中,再次反向滑动,偏移值递减
onSwiperTransition(e : SwiperTransitionEvent) {
const offsetX = e.detail.dx
// 兼容微信 skyline 和 webview
const offsetIndex = offsetX.toInt() / this.$swiperWidth
const currentIndex = this.$animationFinishIndex + offsetIndex.toInt()
let moveToIndex = offsetX > 0 ? currentIndex + 1 : currentIndex - 1
if (moveToIndex < 0) { moveToIndex = 0 }
if (moveToIndex > this.$swiperTabsRect.length - 1) { moveToIndex = this.$swiperTabsRect.length - 1 }
const percentage = Math.abs(offsetX % this.$swiperWidth) / this.$swiperWidth
const currentSize = this.$swiperTabsRect[currentIndex]
const moveToSize = this.$swiperTabsRect[moveToIndex]
const indicatorlineL = currentSize.left + (moveToSize.left - currentSize.left) * percentage
const indicatorlineW = currentSize.width + (moveToSize.width - currentSize.width) * percentage
this.updateTabIndicator(indicatorlineL, indicatorlineW)
const offset_x = e.detail.dx
// 重置差异,计算当前实时索引位置
const current_offset_x = offset_x % this.$swiperWidth
const current_offset_i = offset_x / this.$swiperWidth
const current_index = this.$animationFinishIndex + current_offset_i.toInt()
// 边界检查
let move_to_index = current_index
if (current_offset_x > 0) {
if (move_to_index < this.$swiperTabsRect.length - 1) {
move_to_index += 1
}
} else if (current_offset_x < 0) {
if (move_to_index > 0) {
move_to_index -= 1
}
}
// 计算蛇形线位置和大小
const percentage = Math.abs(current_offset_x) / this.$swiperWidth
const current_size = this.$swiperTabsRect[current_index]
const move_to_size = this.$swiperTabsRect[move_to_index]
const indicator_line_l = current_size.x + (move_to_size.x - current_size.x) * percentage
const indicator_line_w = current_size.w + (move_to_size.w - current_size.w) * percentage
// 更新蛇形线位置和大小
this.updateTabIndicator(indicator_line_l, indicator_line_w)
// console.log(current_index + "->" + move_to_index + " x=" + offset_x + " p=" + percentage)
},
onSwiperAnimationfinish(e : SwiperAnimationFinishEvent) {
this.setSwiperIndex(e.detail.current, true)
...
...
@@ -90,8 +108,8 @@
const tabs = this.$refs["swipertab"] as INode[]
tabs.forEach((node) => {
this.$swiperTabsRect.push({
left
: node.offsetLeft,
w
idth
: node.offsetWidth
x
: node.offsetLeft,
w: node.offsetWidth
} as SwiperTabsItem)
})
},
...
...
@@ -102,7 +120,7 @@
this.swiperIndex = index
if (updateIndicator) {
this.updateTabIndicator(this.$swiperTabsRect[index].
left, this.$swiperTabsRect[index].width
)
this.updateTabIndicator(this.$swiperTabsRect[index].
x, this.$swiperTabsRect[index].w
)
}
},
updateTabIndicator(left : Number, width : Number) {
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录