Skip to content

Commit c10fb5f

Browse files
authored
Merge pull request #33 from xehpuk/master
#9 implemented: fixed cross-browser compatibility, added propType
2 parents 0868cd1 + 8d23328 commit c10fb5f

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

src/helpers.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,21 @@ export const animateScroll = (function () {
55
let resolvePrevious;
66

77
return function animateScroll (id, targetId, animate) {
8-
let targetElement = document.getElementById(targetId);
9-
if (!targetElement) {
10-
targetElement = 'scrollTop' in document.documentElement
11-
? document.documentElement
12-
: document.body;
13-
}
8+
const targetElement = document.getElementById(targetId);
149

1510
function getScrollTop () {
1611
// like jQuery -> $('html, body').scrollTop
17-
return targetElement.scrollTop;
12+
return targetElement
13+
? targetElement.scrollTop
14+
: document.documentElement.scrollTop || document.body.scrollTop;
1815
}
1916

2017
function setScrollTop (position) {
21-
targetElement.scrollTop = position;
18+
if (targetElement) {
19+
targetElement.scrollTop = position;
20+
} else {
21+
document.documentElement.scrollTop = document.body.scrollTop = position;
22+
}
2223
}
2324

2425
return new Promise((resolve, reject) => {
@@ -29,7 +30,10 @@ export const animateScroll = (function () {
2930
}
3031

3132
function getOffsetTop () {
32-
return element.getBoundingClientRect().top - targetElement.getBoundingClientRect().top + getScrollTop();
33+
const parentOffsetTop = targetElement
34+
? targetElement.getBoundingClientRect().top
35+
: 0;
36+
return element.getBoundingClientRect().top - parentOffsetTop + getScrollTop();
3337
}
3438

3539
const { offset, duration, easing } = animate;

src/scrollchor.jsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export default class Scrollchor extends React.Component {
1111

1212
static propTypes = {
1313
to: PropTypes.string.isRequired,
14+
target: PropTypes.string,
1415
animate: PropTypes.shape({
1516
offset: PropTypes.number,
1617
duration: PropTypes.number,

0 commit comments

Comments
 (0)