Skip to content

Commit ad28e40

Browse files
committed
simplify _placeTooltip by calling _getOffset less
1 parent b5005cb commit ad28e40

File tree

2 files changed

+44
-48
lines changed

2 files changed

+44
-48
lines changed

intro.js

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,8 @@
419419
var tooltipCssClass = '',
420420
currentStepObj,
421421
tooltipOffset,
422-
targetElementOffset,
422+
targetOffset,
423+
windowSize,
423424
currentTooltipPosition;
424425

425426
//reset the old style
@@ -456,22 +457,22 @@
456457
currentTooltipPosition = _determineAutoPosition.call(this, targetElement, tooltipLayer, currentTooltipPosition)
457458
}
458459
}
459-
var targetOffset = _getOffset(targetElement);
460-
var tooltipHeight = _getOffset(tooltipLayer).height;
461-
var windowSize = _getWinSize();
460+
targetOffset = _getOffset(targetElement);
461+
tooltipOffset = _getOffset(tooltipLayer);
462+
windowSize = _getWinSize();
462463
switch (currentTooltipPosition) {
463464
case 'top':
464465
tooltipLayer.style.left = '15px';
465-
tooltipLayer.style.top = '-' + (tooltipHeight + 10) + 'px';
466+
tooltipLayer.style.top = '-' + (tooltipOffset.height + 10) + 'px';
466467
arrowLayer.className = 'introjs-arrow bottom';
467468
break;
468469
case 'right':
469-
tooltipLayer.style.left = (_getOffset(targetElement).width + 20) + 'px';
470-
if (targetOffset.top + tooltipHeight > windowSize.height) {
470+
tooltipLayer.style.left = (targetOffset.width + 20) + 'px';
471+
if (targetOffset.top + tooltipOffset.height > windowSize.height) {
471472
// In this case, right would have fallen below the bottom of the screen.
472473
// Modify so that the bottom of the tooltip connects with the target
473474
arrowLayer.className = "introjs-arrow left-bottom";
474-
tooltipLayer.style.top = "-" + (tooltipHeight - targetOffset.height - 20) + "px"
475+
tooltipLayer.style.top = "-" + (tooltipOffset.height - targetOffset.height - 20) + "px"
475476
}
476477
arrowLayer.className = 'introjs-arrow left';
477478
break;
@@ -480,10 +481,10 @@
480481
tooltipLayer.style.top = '15px';
481482
}
482483

483-
if (targetOffset.top + tooltipHeight > windowSize.height) {
484+
if (targetOffset.top + tooltipOffset.height > windowSize.height) {
484485
// In this case, left would have fallen below the bottom of the screen.
485486
// Modify so that the bottom of the tooltip connects with the target
486-
tooltipLayer.style.top = "-" + (tooltipHeight - targetOffset.height - 20) + "px";
487+
tooltipLayer.style.top = "-" + (tooltipOffset.height - targetOffset.height - 20) + "px";
487488
arrowLayer.className = 'introjs-arrow right-bottom';
488489
} else {
489490
arrowLayer.className = 'introjs-arrow right';
@@ -496,8 +497,6 @@
496497
arrowLayer.style.display = 'none';
497498

498499
//we have to adjust the top and left of layer manually for intro items without element
499-
tooltipOffset = _getOffset(tooltipLayer);
500-
501500
tooltipLayer.style.left = '50%';
502501
tooltipLayer.style.top = '50%';
503502
tooltipLayer.style.marginLeft = '-' + (tooltipOffset.width / 2) + 'px';
@@ -512,28 +511,25 @@
512511
case 'bottom-right-aligned':
513512
arrowLayer.className = 'introjs-arrow top-right';
514513
tooltipLayer.style.right = '0px';
515-
tooltipLayer.style.bottom = '-' + (_getOffset(tooltipLayer).height + 10) + 'px';
514+
tooltipLayer.style.bottom = '-' + (tooltipOffset.height + 10) + 'px';
516515
break;
517516
case 'bottom-middle-aligned':
518-
targetElementOffset = _getOffset(targetElement);
519-
tooltipOffset = _getOffset(tooltipLayer);
520-
521517
arrowLayer.className = 'introjs-arrow top-middle';
522-
var tooltipLayerStyleLeft = targetElementOffset.width / 2 - tooltipOffset.width / 2;
518+
var tooltipLayerStyleLeft = targetOffset.width / 2 - tooltipOffset.width / 2;
523519
// off the right side of the window
524-
if (targetElementOffset.left + tooltipLayerStyleLeft + tooltipOffset.width > windowSize.width)
525-
tooltipLayer.style.left = (windowSize.width - tooltipOffset.width - targetElementOffset.left) + 'px';
520+
if (targetOffset.left + tooltipLayerStyleLeft + tooltipOffset.width > windowSize.width)
521+
tooltipLayer.style.left = (windowSize.width - tooltipOffset.width - targetOffset.left) + 'px';
526522
else
527523
tooltipLayer.style.left = tooltipLayerStyleLeft + 'px';
528-
tooltipLayer.style.top = (targetElementOffset.height + 20) + 'px';
524+
tooltipLayer.style.top = (targetOffset.height + 20) + 'px';
529525
break;
530526
case 'bottom-left-aligned':
531527
// Bottom-left-aligned is the same as the default bottom
532528
case 'bottom':
533529
// Bottom going to follow the default behavior
534530
default:
535-
tooltipLayer.style.bottom = '-' + (_getOffset(tooltipLayer).height + 10) + 'px';
536-
tooltipLayer.style.left = (_getOffset(targetElement).width / 2 - _getOffset(tooltipLayer).width / 2) + 'px';
531+
tooltipLayer.style.bottom = '-' + (tooltipOffset.height + 10) + 'px';
532+
tooltipLayer.style.left = (targetOffset.width / 2 - tooltipOffset.width / 2) + 'px';
537533

538534
arrowLayer.className = 'introjs-arrow top';
539535
break;

0 commit comments

Comments
 (0)