Skip to content

Commit 0038b11

Browse files
committed
Merge branch 'add_position_options' of https://github.com/casiotone/intro.js into casiotone-add_position_options
2 parents 430e66d + f180dc5 commit 0038b11

File tree

3 files changed

+76
-24
lines changed

3 files changed

+76
-24
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ Intro.js can be added to your site in three simple steps:
88

99
**1)** Include `intro.js` and `introjs.css` (or the minified version for production) in your page.
1010

11-
**2)** Add `data-intro` and `data-step` to your HTML elements.
11+
**2)** Add `data-intro` and `data-step` to your HTML elements. You can optionally set the tooltip's position by adding `data-position`. The default position is 'bottom'.
1212
For example:
1313
```html
14-
<a href='http://google.com/' data-intro='Hello step one!' data-step='1'></a>
14+
<a href='http://google.com/' data-intro='Hello step one!' data-step='1' data-position='right'></a>
1515
````
1616

1717
**3)** Call this JavaScript function:

intro.js

Lines changed: 41 additions & 13 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

@@ -76,8 +77,8 @@
7677
//right arrow
7778
_nextStep.call(self);
7879
}
79-
};
80-
}
80+
}
81+
};
8182
}
8283
return false;
8384
}
@@ -114,8 +115,9 @@
114115
* @method _nextStep
115116
*/
116117
function _previousStep() {
117-
if(this._currentStep == 0)
118+
if(this._currentStep == 0){
118119
return;
120+
}
119121

120122
_showElement.call(this, this._introItems[--this._currentStep].element);
121123
}
@@ -155,6 +157,34 @@
155157
}
156158
}
157159

160+
function _placeTooltip(targetElement, tooltipLayer, arrowLayer){
161+
var tooltipLayerPosition = _getOffset(tooltipLayer);
162+
tooltipLayer.style.top = null;
163+
tooltipLayer.style.right = null;
164+
tooltipLayer.style.bottom = null;
165+
tooltipLayer.style.left = null;
166+
switch(targetElement.getAttribute('data-position')){
167+
case 'top':
168+
tooltipLayer.style.top = "-" + (tooltipLayerPosition.height + 10) + "px";
169+
arrowLayer.className = 'introjs-arrow bottom';
170+
break;
171+
case 'right':
172+
console.log(tooltipLayerPosition);
173+
tooltipLayer.style.right = "-" + (tooltipLayerPosition.width + 10) + "px";
174+
arrowLayer.className = 'introjs-arrow left';
175+
break;
176+
case 'left':
177+
tooltipLayer.style.left = "-" + (tooltipLayerPosition.width + 10) + "px";
178+
arrowLayer.className = 'introjs-arrow right';
179+
break;
180+
case 'bottom':
181+
default:
182+
tooltipLayer.style.bottom = "-" + (tooltipLayerPosition.height + 10) + "px";
183+
arrowLayer.className = 'introjs-arrow top';
184+
break;
185+
}
186+
}
187+
158188
/**
159189
* Show an element on the page
160190
*
@@ -172,6 +202,7 @@
172202
if(oldHelperLayer != null) {
173203
var oldHelperNumberLayer = oldHelperLayer.querySelector(".introjs-helperNumberLayer"),
174204
oldtooltipLayer = oldHelperLayer.querySelector(".introjs-tooltiptext"),
205+
oldArrowLayer = oldHelperLayer.querySelector(".introjs-arrow"),
175206
oldtooltipContainer = oldHelperLayer.querySelector(".introjs-tooltip")
176207

177208
//set new position to helper layer
@@ -187,17 +218,13 @@
187218
oldShowElement.className = oldShowElement.className.replace(/introjs-showElement/,'').trim();
188219
//change to new intro item
189220
targetElement.className += " introjs-showElement";
190-
191-
//wait until the animation is completed
192-
setTimeout(function() {
193-
oldtooltipContainer.style.bottom = "-" + (_getOffset(oldtooltipContainer).height + 10) + "px";
194-
}, 300);
195-
221+
_placeTooltip(targetElement, oldtooltipContainer, oldArrowLayer);
196222
} else {
197223
targetElement.className += " introjs-showElement";
198224

199225
var helperLayer = document.createElement("div"),
200226
helperNumberLayer = document.createElement("span"),
227+
arrowLayer = document.createElement("div"),
201228
tooltipLayer = document.createElement("div");
202229

203230
helperLayer.className = "introjs-helperLayer";
@@ -209,11 +236,13 @@
209236
document.body.appendChild(helperLayer);
210237

211238
helperNumberLayer.className = "introjs-helperNumberLayer";
239+
arrowLayer.className = 'introjs-arrow';
212240
tooltipLayer.className = "introjs-tooltip";
213241

214242
helperNumberLayer.innerHTML = targetElement.getAttribute("data-step");
215243
tooltipLayer.innerHTML = "<div class='introjs-tooltiptext'>" + targetElement.getAttribute("data-intro") + "</div><div class='introjs-tooltipbuttons'></div>";
216244
helperLayer.appendChild(helperNumberLayer);
245+
tooltipLayer.appendChild(arrowLayer);
217246
helperLayer.appendChild(tooltipLayer);
218247

219248
var skipTooltipButton = document.createElement("a");
@@ -238,10 +267,9 @@
238267
var tooltipButtonsLayer = tooltipLayer.querySelector('.introjs-tooltipbuttons');
239268
tooltipButtonsLayer.appendChild(skipTooltipButton);
240269
tooltipButtonsLayer.appendChild(nextTooltipButton);
241-
242-
270+
243271
//set proper position
244-
tooltipLayer.style.bottom = "-" + (_getOffset(tooltipLayer).height + 10) + "px";
272+
_placeTooltip(targetElement, tooltipLayer, arrowLayer);
245273
}
246274

247275
//scroll the page to the element position

introjs.css

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

0 commit comments

Comments
 (0)