Skip to content

Commit 0278cd1

Browse files
author
Ben M
committed
Tooltip position can be set on a per-step basis by setting an element's data-position to 'top', 'right', 'bottom', or 'left.
The "arrow" was refactored into its own element for cross-browser compatibility since :before can be problematic in <= IE8. Removed the timeout for moving the tooltip, as the delay was causing the tooltip not to size properly. Added _placeTooltip method that uses targetElement, tooltipLayer, and arrowLayer as parameters, then sets the appropriate CSS styles for the tooltipLayer.
1 parent 0476114 commit 0278cd1

File tree

2 files changed

+69
-19
lines changed

2 files changed

+69
-19
lines changed

intro.js

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@
4343
introItems.push({
4444
element: currentElement,
4545
intro: currentElement.getAttribute("data-intro"),
46-
step: parseInt(currentElement.getAttribute("data-step"))
46+
step: parseInt(currentElement.getAttribute("data-step")),
47+
position: currentElement.getAttribute("data-position") || 'bottom'
4748
});
4849
}
4950

@@ -141,6 +142,33 @@
141142
targetElement.onkeydown = null;
142143
}
143144

145+
function _placeTooltip(targetElement, tooltipLayer, arrowLayer){
146+
var tooltipLayerPosition = _getOffset(tooltipLayer);
147+
tooltipLayer.style.top = null;
148+
tooltipLayer.style.right = null;
149+
tooltipLayer.style.bottom = null;
150+
tooltipLayer.style.left = null;
151+
switch(targetElement.getAttribute('data-position')){
152+
case 'top':
153+
tooltipLayer.style.top = "-" + (tooltipLayerPosition.height + 10) + "px";
154+
arrowLayer.className = 'introjs-arrow bottom';
155+
break;
156+
case 'right':
157+
console.log(tooltipLayerPosition);
158+
tooltipLayer.style.right = "-" + (tooltipLayerPosition.width + 10) + "px";
159+
arrowLayer.className = 'introjs-arrow left';
160+
break;
161+
case 'bottom':
162+
tooltipLayer.style.bottom = "-" + (tooltipLayerPosition.height + 10) + "px";
163+
arrowLayer.className = 'introjs-arrow top';
164+
break;
165+
case 'left':
166+
tooltipLayer.style.left = "-" + (tooltipLayerPosition.width + 10) + "px";
167+
arrowLayer.className = 'introjs-arrow right';
168+
break;
169+
}
170+
}
171+
144172
/**
145173
* Show an element on the page
146174
*
@@ -158,6 +186,7 @@
158186
if(oldHelperLayer != null) {
159187
var oldHelperNumberLayer = oldHelperLayer.querySelector(".introjs-helperNumberLayer"),
160188
oldtooltipLayer = oldHelperLayer.querySelector(".introjs-tooltiptext"),
189+
oldArrowLayer = oldHelperLayer.querySelector(".introjs-arrow"),
161190
oldtooltipContainer = oldHelperLayer.querySelector(".introjs-tooltip")
162191

163192
//set new position to helper layer
@@ -173,17 +202,13 @@
173202
oldShowElement.className = oldShowElement.className.replace(/introjs-showElement/,'').trim();
174203
//change to new intro item
175204
targetElement.className += " introjs-showElement";
176-
177-
//wait until the animation is completed
178-
setTimeout(function() {
179-
oldtooltipContainer.style.bottom = "-" + (_getOffset(oldtooltipContainer).height + 10) + "px";
180-
}, 300);
181-
205+
_placeTooltip(targetElement, oldtooltipContainer, oldArrowLayer);
182206
} else {
183207
targetElement.className += " introjs-showElement";
184208

185209
var helperLayer = document.createElement("div"),
186210
helperNumberLayer = document.createElement("span"),
211+
arrowLayer = document.createElement("div"),
187212
tooltipLayer = document.createElement("div");
188213

189214
helperLayer.className = "introjs-helperLayer";
@@ -195,11 +220,13 @@
195220
document.body.appendChild(helperLayer);
196221

197222
helperNumberLayer.className = "introjs-helperNumberLayer";
223+
arrowLayer.className = 'introjs-arrow';
198224
tooltipLayer.className = "introjs-tooltip";
199225

200226
helperNumberLayer.innerHTML = targetElement.getAttribute("data-step");
201227
tooltipLayer.innerHTML = "<div class='introjs-tooltiptext'>" + targetElement.getAttribute("data-intro") + "</div><div class='introjs-tooltipbuttons'></div>";
202228
helperLayer.appendChild(helperNumberLayer);
229+
tooltipLayer.appendChild(arrowLayer);
203230
helperLayer.appendChild(tooltipLayer);
204231

205232
var skipTooltipButton = document.createElement("a");
@@ -224,10 +251,9 @@
224251
var tooltipButtonsLayer = tooltipLayer.querySelector('.introjs-tooltipbuttons');
225252
tooltipButtonsLayer.appendChild(skipTooltipButton);
226253
tooltipButtonsLayer.appendChild(nextTooltipButton);
227-
228-
254+
229255
//set proper position
230-
tooltipLayer.style.bottom = "-" + (_getOffset(tooltipLayer).height + 10) + "px";
256+
_placeTooltip(targetElement, tooltipLayer, arrowLayer);
231257
}
232258

233259
//scroll the page to the element position

introjs.css

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,40 @@
5050
left: -16px;
5151
top: -16px;
5252
}
53-
.introjs-tooltip:before {
53+
.introjs-arrow {
5454
border: 5px solid white;
55-
content:'';
56-
border-top-color:transparent;
57-
border-right-color:transparent;
58-
border-bottom-color:white;
59-
border-left-color:transparent;
60-
position: absolute;
61-
top: -10px;
62-
55+
content:'';
56+
position: absolute;
57+
}
58+
.introjs-arrow.top{
59+
top: -10px;
60+
border-top-color:transparent;
61+
border-right-color:transparent;
62+
border-bottom-color:white;
63+
border-left-color:transparent;
64+
}
65+
.introjs-arrow.right{
66+
right: -10px;
67+
top: 10px;
68+
border-top-color:transparent;
69+
border-right-color:transparent;
70+
border-bottom-color:transparent;
71+
border-left-color:white;
72+
}
73+
.introjs-arrow.bottom{
74+
bottom: -10px;
75+
border-top-color:white;
76+
border-right-color:transparent;
77+
border-bottom-color:transparent;
78+
border-left-color:transparent;
79+
}
80+
.introjs-arrow.left{
81+
left: -10px;
82+
top: 10px;
83+
border-top-color:transparent;
84+
border-right-color:white;
85+
border-bottom-color:transparent;
86+
border-left-color:transparent;
6387
}
6488
.introjs-tooltip {
6589
position: absolute;

0 commit comments

Comments
 (0)