Skip to content

Commit 53f9529

Browse files
committed
fix(main): Corrected and improved the issue #299 around the positioning of the popover on scrolling
1 parent 610246a commit 53f9529

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

src/main.js

+23-4
Original file line numberDiff line numberDiff line change
@@ -130,19 +130,38 @@ textAngular.directive("textAngular", [
130130
$animate.addClass(scope.displayElements.popover, 'in');
131131
oneEvent($document.find('body'), 'click keyup', function(){scope.hidePopover();});
132132
};
133+
var getScrollTop = function (_el) {
134+
var scrollTop = _el.scrollTop;
135+
if (scrollTop !== 0) {
136+
return { node:_el.nodeName, top:scrollTop };
137+
}
138+
/* istanbul ignore else: catches only if no scroll */
139+
if (_el.parentNode) {
140+
return getScrollTop(_el.parentNode);
141+
} else {
142+
return { node:'<none>', top:0 };
143+
}
144+
};
133145
scope.reflowPopover = function(_el){
146+
var scrollTop = getScrollTop(scope.displayElements.scrollWindow[0]);
147+
var spaceAboveImage = _el[0].offsetTop-scrollTop.top;
148+
134149
/* istanbul ignore if: catches only if near bottom of editor */
135-
if(scope.displayElements.text[0].offsetHeight - 51 > _el[0].offsetTop){
150+
if(spaceAboveImage < 51) {
136151
scope.displayElements.popover.css('top', _el[0].offsetTop + _el[0].offsetHeight + scope.displayElements.scrollWindow[0].scrollTop + 'px');
137152
scope.displayElements.popover.removeClass('top').addClass('bottom');
138-
}else{
153+
} else {
139154
scope.displayElements.popover.css('top', _el[0].offsetTop - 54 + scope.displayElements.scrollWindow[0].scrollTop + 'px');
140155
scope.displayElements.popover.removeClass('bottom').addClass('top');
141156
}
142157
var _maxLeft = scope.displayElements.text[0].offsetWidth - scope.displayElements.popover[0].offsetWidth;
143158
var _targetLeft = _el[0].offsetLeft + (_el[0].offsetWidth / 2.0) - (scope.displayElements.popover[0].offsetWidth / 2.0);
144-
scope.displayElements.popover.css('left', Math.max(0, Math.min(_maxLeft, _targetLeft)) + 'px');
145-
scope.displayElements.popoverArrow.css('margin-left', (Math.min(_targetLeft, (Math.max(0, _targetLeft - _maxLeft))) - 11) + 'px');
159+
var _rleft = Math.max(0, Math.min(_maxLeft, _targetLeft));
160+
var _marginLeft = (Math.min(_targetLeft, (Math.max(0, _targetLeft - _maxLeft))) - 11);
161+
_rleft += window.scrollX;
162+
_marginLeft -= window.scrollX;
163+
scope.displayElements.popover.css('left', _rleft + 'px');
164+
scope.displayElements.popoverArrow.css('margin-left', _marginLeft + 'px');
146165
};
147166
scope.hidePopover = function(){
148167
scope.displayElements.popover.css('display', 'none');

0 commit comments

Comments
 (0)