Skip to content

Commit b6d1b9b

Browse files
iwangulenkostipsan
authored andcommitted
Feat: Set offset (#127 @iwangulenko)
New argument added that allows setting offsets in all 4 directions.
1 parent 858f2af commit b6d1b9b

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

src/index.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import animate from 'amator'
22

3-
export default function (elem, centerIfNeeded, options, finalElement) {
3+
export default function (elem, centerIfNeeded, options, finalElement, config) {
44

55
if (!elem) throw new Error('Element is required in scrollIntoViewIfNeeded')
66

@@ -12,16 +12,21 @@ export default function (elem, centerIfNeeded, options, finalElement) {
1212
}
1313
}
1414

15+
const offsetTop = config.offsetTop || 0;
16+
const offsetLeft = config.offsetLeft || 0;
17+
const offsetBottom = config.offsetBottom || 0;
18+
const offsetRight = config.offsetRight || 0;
19+
1520
function makeArea(left, top, width, height) {
16-
return { "left": left, "top": top, "width": width, "height": height
17-
, "right": left + width, "bottom": top + height
21+
return { "left": left + offsetLeft, "top": top + offsetTop, "width": width, "height": height
22+
, "right": left + offsetLeft + width + offsetRight, "bottom": top + offsetTop + height + offsetBottom
1823
, "translate":
1924
function (x, y) {
20-
return makeArea(x + left, y + top, width, height);
25+
return makeArea(x + left + offsetLeft, y + top + offsetTop, width, height);
2126
}
2227
, "relativeFromTo":
2328
function (lhs, rhs) {
24-
var newLeft = left, newTop = top;
29+
var newLeft = left + offsetLeft, newTop = top + offsetTop;
2530
lhs = lhs.offsetParent;
2631
rhs = rhs.offsetParent;
2732
if (lhs === rhs) {

0 commit comments

Comments
 (0)