Skip to content

Commit 0b7179e

Browse files
authored
Merge pull request usablica#792 from tianyong90/master
Use getBoundingClientRect method to improve _determineAutoPosition
2 parents 4b0a05d + 771fd69 commit 0b7179e

File tree

1 file changed

+7
-13
lines changed

1 file changed

+7
-13
lines changed

intro.js

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -792,17 +792,10 @@
792792
// Take a clone of position precedence. These will be the available
793793
var possiblePositions = this._options.positionPrecedence.slice();
794794

795-
var scrollTop = window.pageYOffset || document.documentElement.scrollTop;
796-
var scrollLeft = window.pageXOffset || document.documentElement.scrollLeft;
797-
798795
var windowSize = _getWinSize();
799796
var tooltipHeight = _getOffset(tooltipLayer).height + 10;
800797
var tooltipWidth = _getOffset(tooltipLayer).width + 20;
801-
var targetOffset = _getOffset(targetElement);
802-
803-
// adjust for scroll position
804-
targetOffset.top -= scrollTop;
805-
targetOffset.left -= scrollLeft;
798+
var targetElementRect = targetElement.getBoundingClientRect();
806799

807800
// If we check all the possible areas, and there are no valid places for the tooltip, the element
808801
// must take up most of the screen real estate. Show the tooltip floating in the middle of the screen.
@@ -813,22 +806,22 @@
813806
*/
814807

815808
// Check for space below
816-
if ((targetOffset.height + targetOffset.top + tooltipHeight) > windowSize.height) {
809+
if (targetElementRect.bottom + tooltipHeight + tooltipHeight > windowSize.height) {
817810
_removeEntry(possiblePositions, "bottom");
818811
}
819812

820813
// Check for space above
821-
if (targetOffset.top - tooltipHeight < 0) {
814+
if (targetElementRect.top - tooltipHeight < 0) {
822815
_removeEntry(possiblePositions, "top");
823816
}
824817

825818
// Check for space to the right
826-
if (targetOffset.width + targetOffset.left + tooltipWidth > windowSize.width) {
819+
if (targetElementRect.right + tooltipWidth > windowSize.width) {
827820
_removeEntry(possiblePositions, "right");
828821
}
829822

830823
// Check for space to the left
831-
if (targetOffset.left - tooltipWidth < 0) {
824+
if (targetElementRect.left - tooltipWidth < 0) {
832825
_removeEntry(possiblePositions, "left");
833826
}
834827

@@ -862,7 +855,7 @@
862855

863856
// only top and bottom positions have optional alignments
864857
if (['top', 'bottom'].indexOf(calculatedPosition) !== -1) {
865-
calculatedPosition += _determineAutoAlignment(targetOffset.left, tooltipWidth, windowSize, desiredAlignment);
858+
calculatedPosition += _determineAutoAlignment(targetElementRect.left, tooltipWidth, windowSize, desiredAlignment);
866859
}
867860

868861
return calculatedPosition;
@@ -1362,6 +1355,7 @@
13621355
* @param {Object} tooltipLayer
13631356
*/
13641357
function _scrollTo(scrollTo, targetElement, tooltipLayer) {
1358+
if (scrollTo === 'off') return;
13651359
var rect;
13661360

13671361
if (!this._options.scrollToElement) return;

0 commit comments

Comments
 (0)