Skip to content

Commit c11c44f

Browse files
committed
Fixed bug with elements offset top
1 parent 55d2d63 commit c11c44f

File tree

3 files changed

+41
-6
lines changed

3 files changed

+41
-6
lines changed

dist/vue-scrollactive.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -680,7 +680,7 @@ exports.default = {
680680
scrollactiveItem.classList.remove(this.activeClass);
681681
var target = document.getElementById(scrollactiveItem.hash.substr(1));
682682

683-
if (distanceFromTop >= target.offsetTop - this.offset) {
683+
if (distanceFromTop >= this.getOffsetTop(target) - this.offset) {
684684
currentItem = scrollactiveItem;
685685
}
686686
}
@@ -842,7 +842,7 @@ exports.default = {
842842
}
843843

844844
var vm = this;
845-
var targetDistanceFromTop = document.getElementById(event.target.hash.substr(1)).offsetTop;
845+
var targetDistanceFromTop = this.getOffsetTop(document.getElementById(event.target.hash.substr(1)));
846846
var startingY = window.pageYOffset;
847847
var difference = targetDistanceFromTop - startingY;
848848
var start = null;
@@ -870,6 +870,24 @@ exports.default = {
870870
}
871871

872872
window.requestAnimationFrame(step);
873+
},
874+
875+
876+
/**
877+
* Gets the top offset position of an element in the document.
878+
*
879+
* @param {Element} element
880+
* @return {Number}
881+
*/
882+
getOffsetTop: function getOffsetTop(element) {
883+
var yPosition = 0;
884+
885+
while (element) {
886+
yPosition += element.offsetTop;
887+
element = element.offsetParent;
888+
}
889+
890+
return yPosition;
873891
}
874892
},
875893

0 commit comments

Comments
 (0)