Skip to content

Commit c85da11

Browse files
committed
fix link targetId top
1 parent 314a620 commit c85da11

File tree

3 files changed

+24
-20
lines changed

3 files changed

+24
-20
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "rc-scroll-anim",
3-
"version": "2.5.3",
3+
"version": "2.5.4",
44
"description": "scroll-anim anim component for react",
55
"keywords": [
66
"react",

src/EventDispatcher.js

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ EventDispatcher.prototype = {
1010
const types = type.split('.');
1111
const _type = types[0];
1212
const namespaces = types[1];
13-
const listName = `${_type}${target ? target.getAttribute('id') : ''}`;
13+
const listName = `${_type}${target ? `_${target.getAttribute('id')}` : ''}`;
1414
let list = this._listeners[listName];
1515
let index = 0;
1616
let listener;
@@ -28,14 +28,14 @@ EventDispatcher.prototype = {
2828
index = i + 1;
2929
}
3030
}
31-
31+
const $target = target || this._eventTarget;
3232
list.splice(index, 0, { c: callback, n: namespaces, t: _type });
3333
if (!this._listFun[listName]) {
34-
this._listFun[listName] = this._listFun[listName] || this.dispatchEvent.bind(this, _type, target);
35-
if (this._eventTarget.addEventListener) {
36-
(target || this._eventTarget).addEventListener(_type, this._listFun[listName], false);
37-
} else if (this._eventTarget.attachEvent) {
38-
(target || this._eventTarget).attachEvent(`on${_type}`, this._listFun[listName]);
34+
this._listFun[listName] = this._listFun[listName] || this.dispatchEvent.bind(this, _type);
35+
if ($target.addEventListener) {
36+
$target.addEventListener(_type, this._listFun[listName], false);
37+
} else if ($target.attachEvent) {
38+
$target.attachEvent(`on${_type}`, this._listFun[listName]);
3939
}
4040
}
4141
},
@@ -44,7 +44,7 @@ EventDispatcher.prototype = {
4444
const types = type.split('.');
4545
const _type = types[0];
4646
const namespaces = types[1];
47-
const listName = `${_type}${target ? target.getAttribute('id') : ''}`;
47+
const listName = `${_type}${target ? `_${target.getAttribute('id')}` : ''}`;
4848
const list = this._listeners[listName];
4949
let i;
5050
let _force = force;
@@ -53,17 +53,18 @@ EventDispatcher.prototype = {
5353
}
5454
if (list) {
5555
i = list.length;
56+
const $target = target || this._eventTarget;
5657
while (--i > -1) {
5758
if (list[i].c === callback && (_force || list[i].n === namespaces)) {
5859
list.splice(i, 1);
5960
if (!list.length) {
6061
const func = this._listFun[listName];
6162
delete this._listeners[listName];
6263
delete this._listFun[listName];
63-
if (this._eventTarget.removeEventListener) {
64-
(target || this._eventTarget).removeEventListener(_type, func);
65-
} else if (this._eventTarget.detachEvent) {
66-
(target || this._eventTarget).detachEvent(`on${_type}`, func);
64+
if ($target.removeEventListener) {
65+
$target.removeEventListener(_type, func);
66+
} else if ($target.detachEvent) {
67+
$target.detachEvent(`on${_type}`, func);
6768
}
6869
}
6970
if (!_force) {
@@ -74,8 +75,9 @@ EventDispatcher.prototype = {
7475
}
7576
},
7677

77-
dispatchEvent(type, target, e) {
78-
const listName = `${type}${target ? target.getAttribute('id') : ''}`;
78+
dispatchEvent(type, e) {
79+
const target = e.target;
80+
const listName = `${type}${target.getAttribute ? `_${target.getAttribute('id')}` : ''}`;
7981
const list = this._listeners[listName];
8082
let i;
8183
let t;
@@ -96,7 +98,7 @@ EventDispatcher.prototype = {
9698
const types = type.split('.');
9799
const _type = types[0];
98100
const namespaces = types[1];
99-
const listName = `${type}${target ? target.getAttribute('id') : ''}`;
101+
const listName = `${_type}${target ? `_${target.getAttribute('id')}` : ''}`;
100102
const list = this._listeners[listName];
101103
this.recoverLists = this.recoverLists.concat(dataToArray(list).filter(item =>
102104
item.n && item.n.match(namespaces)

src/ScrollLink.jsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,19 +81,20 @@ class ScrollLink extends React.Component {
8181
return;
8282
}
8383
this.scrollTop = this.target ? this.target.scrollTop : currentScrollTop();
84-
const toTop = Math.round(elementRect.top + this.scrollTop) - this.props.offsetTop;
84+
const targetTop = this.target ? this.target.getBoundingClientRect().top : 0;
85+
const toTop = Math.round(elementRect.top + this.scrollTop) - this.props.offsetTop - targetTop;
8586
const t = transformArguments(this.props.showHeightActive)[0];
8687
const toShow = t.match('%') ? this.clientHeight * parseFloat(t) / 100 : t;
8788
this.toTop = this.props.toShowHeight ?
8889
toTop - toShow + 0.5 : toTop;
8990
this.initTime = Date.now();
9091
this.rafID = requestAnimationFrame(this.raf);
91-
scrollLinkLists.forEach(item => {
92+
/* scrollLinkLists.forEach(item => {
9293
if (item !== this) {
9394
item.remActive();
9495
}
9596
});
96-
this.addActive();
97+
this.addActive(); */
9798
}
9899

99100
getElement = () => {
@@ -167,7 +168,8 @@ class ScrollLink extends React.Component {
167168
return;
168169
}
169170
const elementClientHeight = elementDom.clientHeight;
170-
const top = Math.round(- elementRect.top);
171+
const targetTop = this.target ? this.target.getBoundingClientRect().top : 0;
172+
const top = Math.round(- elementRect.top + targetTop);
171173
const showHeightActive = transformArguments(this.props.showHeightActive);
172174
const startShowHeight = showHeightActive[0].toString().indexOf('%') >= 0 ?
173175
parseFloat(showHeightActive[0]) / 100 * this.clientHeight :

0 commit comments

Comments
 (0)