Skip to content

Commit 92296bc

Browse files
committed
refactor(sticky): 重构获取节点逻辑 (#800)
1 parent ca54a72 commit 92296bc

File tree

3 files changed

+29
-43
lines changed

3 files changed

+29
-43
lines changed

dist/sticky-item/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.

examples/dist/sticky-item/index.js

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import nodeUtil from '../utils/node-util';
12
Component({
23
externalClasses: ['l-class', 'l-header-wrapper-class', 'l-header-class', 'l-header-sticky-class', 'l-body-class'],
34
options: {
@@ -93,48 +94,40 @@ Component({
9394
* 更新sticky-item组件的基础数据
9495
* @param {Number} index 当前sticky-item的索引值
9596
*/
96-
updateStickyItemBaseData(index) {
97+
async updateStickyItemBaseData(index) {
9798
// 设置索引值
9899
this.setData({index});
99100
// 从父级组件获取页面垂直滚动距离
100101
const parent = this.getParentComponent();
101102
const scrollTop = parent.data.scrollTop;
102103

103104

104-
const query = wx.createSelectorQuery().in(this);
105-
106105
/**
107106
* 设置sticky-item组件距页面顶部的距离
108107
* 和sticky-item组件的高度
109108
*/
110-
query
111-
.select('.l-sticky-item')
112-
.boundingClientRect((res) => {
113-
this.setData({
114-
stickyItemTop: res.top + scrollTop,
115-
stickyItemHeight: res.height
116-
});
117-
}).exec();
109+
const stickyItemNodeRect = await nodeUtil.getNodeRectFromComponent(this,'.l-sticky-item')
110+
this.setData({
111+
stickyItemTop: stickyItemNodeRect.top + scrollTop,
112+
stickyItemHeight: stickyItemNodeRect.height
113+
})
118114

119115
// 设置sticky-item-header外层容器高度
120-
query
121-
.select('.l-sticky-item-header')
122-
.boundingClientRect((res) => {
123-
this.setData({
124-
stickyItemWrapperHeight: res.height
125-
});
126-
}).exec();
116+
const stickyItemHeaderNodeRect = await nodeUtil.getNodeRectFromComponent(this,'.l-sticky-item-header')
117+
this.setData({
118+
stickyItemWrapperHeight: stickyItemHeaderNodeRect.height
119+
});
127120
},
128121

129122
/**
130123
* 获取父级组件-sticky实例
131124
*/
132125
getParentComponent() {
133-
const stickys = this.getRelationNodes('../sticky/index');
134-
if (stickys.length === 0) {
126+
const sticks = this.getRelationNodes('../sticky/index');
127+
if (sticks.length === 0) {
135128
return;
136129
}
137-
return stickys[0];
130+
return sticks[0];
138131
}
139132
}
140133
});

src/sticky-item/index.js

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import nodeUtil from '../utils/node-util';
12
Component({
23
externalClasses: ['l-class', 'l-header-wrapper-class', 'l-header-class', 'l-header-sticky-class', 'l-body-class'],
34
options: {
@@ -93,48 +94,40 @@ Component({
9394
* 更新sticky-item组件的基础数据
9495
* @param {Number} index 当前sticky-item的索引值
9596
*/
96-
updateStickyItemBaseData(index) {
97+
async updateStickyItemBaseData(index) {
9798
// 设置索引值
9899
this.setData({index});
99100
// 从父级组件获取页面垂直滚动距离
100101
const parent = this.getParentComponent();
101102
const scrollTop = parent.data.scrollTop;
102103

103104

104-
const query = wx.createSelectorQuery().in(this);
105-
106105
/**
107106
* 设置sticky-item组件距页面顶部的距离
108107
* 和sticky-item组件的高度
109108
*/
110-
query
111-
.select('.l-sticky-item')
112-
.boundingClientRect((res) => {
113-
this.setData({
114-
stickyItemTop: res.top + scrollTop,
115-
stickyItemHeight: res.height
116-
});
117-
}).exec();
109+
const stickyItemNodeRect = await nodeUtil.getNodeRectFromComponent(this,'.l-sticky-item')
110+
this.setData({
111+
stickyItemTop: stickyItemNodeRect.top + scrollTop,
112+
stickyItemHeight: stickyItemNodeRect.height
113+
})
118114

119115
// 设置sticky-item-header外层容器高度
120-
query
121-
.select('.l-sticky-item-header')
122-
.boundingClientRect((res) => {
123-
this.setData({
124-
stickyItemWrapperHeight: res.height
125-
});
126-
}).exec();
116+
const stickyItemHeaderNodeRect = await nodeUtil.getNodeRectFromComponent(this,'.l-sticky-item-header')
117+
this.setData({
118+
stickyItemWrapperHeight: stickyItemHeaderNodeRect.height
119+
});
127120
},
128121

129122
/**
130123
* 获取父级组件-sticky实例
131124
*/
132125
getParentComponent() {
133-
const stickys = this.getRelationNodes('../sticky/index');
134-
if (stickys.length === 0) {
126+
const sticks = this.getRelationNodes('../sticky/index');
127+
if (sticks.length === 0) {
135128
return;
136129
}
137-
return stickys[0];
130+
return sticks[0];
138131
}
139132
}
140133
});

0 commit comments

Comments
 (0)