Skip to content

Commit c8fe69f

Browse files
committed
refactor(IndexList): 将 sidebar 整体扩大
BREAKING CHANGE: sidebar 样式整体变大
1 parent e2faa0e commit c8fe69f

File tree

12 files changed

+106
-34
lines changed

12 files changed

+106
-34
lines changed

dist/core/utils/node-util.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index-list/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index-list/index.wxml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
</block>
77

88

9-
<view class="tip l-tip-class" style="top:{{tipTop}}px;{{showTip?'':'display:none'}}">
9+
<view class="tip l-tip-class" style="top:{{tipTop}}px;{{showTip?'':'opacity:0;'}}transform: rotate(-45deg) translateY({{-tipHeight/2-tipHeightOverflow}}px);">
1010
<view class="tip-text l-tip-text-class">{{tipText}}</view>
1111
</view>
1212
</view>

dist/index-list/index.wxss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
.index-list .sidebar{font-size:20rpx;position:fixed;right:20rpx;width:20rpx;top:50%;transform:translateY(-50%);display:flex;flex-direction:column;align-items:center}.index-list .sidebar-item{width:16px;height:16px;border-radius:50%;display:flex;justify-content:center;align-items:center;margin-top:6rpx}.index-list .sidebar-item-active{color:#fff;background-color:#3963bc}.index-list .tip{width:90rpx;height:90rpx;background-color:#d8d8d8;border-radius:90px 90px 0;display:flex;justify-content:center;align-items:center;position:absolute;left:-160rpx;transform:rotate(-45deg) translateY(-35%)}.index-list .tip-text{font-size:30rpx;transform:rotate(45deg)}
1+
.index-list .sidebar{font-size:24rpx;position:fixed;right:30rpx;width:20rpx;top:50%;transform:translateY(-50%);display:flex;flex-direction:column;align-items:center}.index-list .sidebar-item{width:40rpx;height:40rpx;border-radius:50%;display:flex;justify-content:center;align-items:center;margin-top:8rpx}.index-list .sidebar-item-active{color:#fff;background-color:#3963bc}.index-list .tip{width:90rpx;height:90rpx;background-color:#d8d8d8;border-radius:90px 90px 0;display:flex;justify-content:center;align-items:center;position:absolute;left:-160rpx;transform:rotate(-45deg) translateY(-35%)}.index-list .tip-text{font-size:30rpx;transform:rotate(45deg)}

examples/dist/core/utils/node-util.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,17 @@ class NodeUtil {
3232
}).exec()
3333
})
3434
}
35+
36+
async getNodeFieldsFromComponent(component, selector, fields) {
37+
return await new Promise((resolve) => {
38+
component
39+
.createSelectorQuery()
40+
.select(selector)
41+
.fields(fields, (res) => {
42+
resolve(res)
43+
}).exec()
44+
})
45+
}
3546
}
3647

3748
const nodeUtil = new NodeUtil()

examples/dist/index-list/index.js

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Component({
2828
},
2929

3030
lifetimes: {
31-
async attached() {
31+
attached() {
3232
this.init()
3333
}
3434
},
@@ -75,7 +75,9 @@ Component({
7575
// 改变量用于标识是否正在滑动 Sidebar
7676
// 滑动侧栏的时候需要禁止页面滚动去改变 Sidebar 激活项
7777
// 不然会出现 Sidebar 激活项乱跳动的问题
78-
isMoving: false
78+
isMoving: false,
79+
// sidebar-item 节点 rect 信息
80+
sidebarItemRect: {}
7981
},
8082
// Anchor 节点信息
8183
_anchor: {
@@ -95,7 +97,9 @@ Component({
9597
// Tip 提示绝对定位的top值
9698
tipTop: 0,
9799
// 是否显示 Tip
98-
showTip: false
100+
showTip: false,
101+
// tip 高度
102+
tipHeight: 0
99103
},
100104

101105
observers: {
@@ -122,7 +126,7 @@ Component({
122126
// 解析 Sidebar Rect 信息
123127
await this.parseSidebarRect()
124128
// 解析 SidebarItem Rect 信息
125-
this.parseSidebarItemRect()
129+
await this.parseSidebarItemRect()
126130
// 获取 index-anchor 所有组件实例
127131
await this.parseIndexAnchors()
128132
// 解析 Anchor Rect 信息
@@ -152,18 +156,39 @@ Component({
152156
* 把 Sidebar 每个 Item 的中点位置存到 data 中
153157
* 用于 Tip 定位使用
154158
*/
155-
parseSidebarItemRect() {
159+
async parseSidebarItemRect() {
156160
// Sidebar 索引个数
157161
const sidebarLength = this.data.sidebarData.length
158-
// Sidebar 单个索引高度
162+
// 获取 sidebar-item 节点
163+
const sidebarItemRect = await nodeUtil.getNodeRectFromComponent(this, '.sidebar-item')
164+
// Sidebar 单个索引高度(包含了 margin 空隙)
159165
const sidebarItemHeight = this.data._sidebar.height / sidebarLength
160-
166+
// Sidebar 单个索引真实高度
167+
const sidebarItemRealHeight = sidebarItemRect.height
168+
// 获取 sidebar-item margin-top 属性
169+
const sidebarItemFields = await nodeUtil.getNodeFieldsFromComponent(this, '.sidebar-item', {
170+
computedStyle: ['margin-top']
171+
})
172+
// 获取 tip height 属性
173+
// 只能用 height 获取高度,因为 tip 旋转后,rect的宽高发生了变化
174+
const tipFields = await nodeUtil.getNodeFieldsFromComponent(this, '.tip', {
175+
computedStyle: ['height']
176+
})
161177
const sidebarItemCenterPoints = []
162-
for (let i = 0; i < sidebarLength; i++) {
163-
sidebarItemCenterPoints.push(i * sidebarItemHeight)
178+
const sidebarItemMarginTop = sidebarItemFields['margin-top'].replace('px', '')
179+
180+
for (let i = 1; i <= sidebarLength; i++) {
181+
sidebarItemCenterPoints.push((i * 2 - 1) * sidebarItemRealHeight / 2 + i * parseInt(sidebarItemMarginTop))
164182
}
183+
184+
const tipHeight = parseInt(tipFields.height.replace('px', ''))
165185
this.setData({
186+
tipHeight,
187+
// tip 旋转后,中线位置下移了 20.5%
188+
tipHeightOverflow: tipHeight * 0.205,
189+
['_sidebar.sidebarItemRect']: sidebarItemRect,
166190
['_sidebar.sidebarItemHeight']: sidebarItemHeight,
191+
['_sidebar.sidebarItemRealHeight']: sidebarItemRealHeight,
167192
['_sidebar.sidebarItemCenterPoints']: sidebarItemCenterPoints
168193
})
169194
},
@@ -403,14 +428,14 @@ Component({
403428
// 300 毫秒后隐藏 Tip
404429
setTimeout(() => {
405430
this.switchTipShow(false)
406-
}, 300)
431+
}, 500)
407432
this.switchIsMovingSidebar(false)
408433
},
409434

410435
/**
411436
* 监听 点击侧边栏
412437
*/
413-
onTapSidebar(event){
438+
onTapSidebar(event) {
414439
// 把事件对象传入触摸滑动监听函数即可
415440
this.onTouchMove(event)
416441
}

examples/dist/index-list/index.wxml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
</block>
77

88
<!--Tip 提示-->
9-
<view class="tip l-tip-class" style="top:{{tipTop}}px;{{showTip?'':'display:none'}}">
9+
<view class="tip l-tip-class" style="top:{{tipTop}}px;{{showTip?'':'opacity:0;'}}transform: rotate(-45deg) translateY({{-tipHeight/2-tipHeightOverflow}}px);">
1010
<view class="tip-text l-tip-text-class">{{tipText}}</view>
1111
</view>
1212
</view>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
.index-list .sidebar{font-size:20rpx;position:fixed;right:20rpx;width:20rpx;top:50%;transform:translateY(-50%);display:flex;flex-direction:column;align-items:center}.index-list .sidebar-item{width:16px;height:16px;border-radius:50%;display:flex;justify-content:center;align-items:center;margin-top:6rpx}.index-list .sidebar-item-active{color:#fff;background-color:#3963bc}.index-list .tip{width:90rpx;height:90rpx;background-color:#d8d8d8;border-radius:90px 90px 0;display:flex;justify-content:center;align-items:center;position:absolute;left:-160rpx;transform:rotate(-45deg) translateY(-35%)}.index-list .tip-text{font-size:30rpx;transform:rotate(45deg)}
1+
.index-list .sidebar{font-size:24rpx;position:fixed;right:30rpx;width:20rpx;top:50%;transform:translateY(-50%);display:flex;flex-direction:column;align-items:center}.index-list .sidebar-item{width:40rpx;height:40rpx;border-radius:50%;display:flex;justify-content:center;align-items:center;margin-top:8rpx}.index-list .sidebar-item-active{color:#fff;background-color:#3963bc}.index-list .tip{width:90rpx;height:90rpx;background-color:#d8d8d8;border-radius:90px 90px 0;display:flex;justify-content:center;align-items:center;position:absolute;left:-160rpx;transform:rotate(-45deg) translateY(-35%)}.index-list .tip-text{font-size:30rpx;transform:rotate(45deg)}

src/core/utils/node-util.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,17 @@ class NodeUtil {
3232
}).exec()
3333
})
3434
}
35+
36+
async getNodeFieldsFromComponent(component, selector, fields) {
37+
return await new Promise((resolve) => {
38+
component
39+
.createSelectorQuery()
40+
.select(selector)
41+
.fields(fields, (res) => {
42+
resolve(res)
43+
}).exec()
44+
})
45+
}
3546
}
3647

3748
const nodeUtil = new NodeUtil()

0 commit comments

Comments
 (0)