Skip to content

Commit 8c0b160

Browse files
committed
Fix arrow key bug in specific element mode + code refactoring
1 parent 5170c7b commit 8c0b160

File tree

1 file changed

+25
-18
lines changed

1 file changed

+25
-18
lines changed

intro.js

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
var skipButton = targetElm.querySelector(".introjs-skipbutton"),
6565
nextStepButton = targetElm.querySelector(".introjs-nextbutton");
6666

67-
targetElm.onkeydown = function(e) {
67+
window.onkeydown = function(e) {
6868
if (e.keyCode == 27) {
6969
//escape key pressed, exit the intro
7070
_exitIntro.call(self, targetElm);
@@ -96,7 +96,7 @@
9696
if((this._introItems.length) <= this._currentStep) {
9797
//end of the intro
9898
//check if any callback is defined
99-
if (this._introCompleteCallback != undefined){
99+
if (this._introCompleteCallback != undefined) {
100100
this._introCompleteCallback.call(this);
101101
}
102102
_exitIntro.call(this, this._targetElement);
@@ -113,7 +113,7 @@
113113
* @method _nextStep
114114
*/
115115
function _previousStep() {
116-
if (this._currentStep == 0){
116+
if (this._currentStep == 0) {
117117
return false;
118118
}
119119

@@ -150,7 +150,7 @@
150150
//clean listeners
151151
targetElement.onkeydown = null;
152152
//check if any callback is defined
153-
if (this._introExitCallback != undefined){
153+
if (this._introExitCallback != undefined) {
154154
this._introExitCallback.call(this);
155155
}
156156
}
@@ -215,10 +215,10 @@
215215
oldtooltipContainer = oldHelperLayer.querySelector(".introjs-tooltip")
216216

217217
//set new position to helper layer
218-
oldHelperLayer.setAttribute("style", "width: " + (elementPosition.width + 10) + "px; " +
219-
"height:" + (elementPosition.height + 10) + "px; " +
220-
"top:" + (elementPosition.top - 5) + "px;" +
221-
"left: " + (elementPosition.left - 5) + "px;");
218+
oldHelperLayer.setAttribute("style", "width: " + (elementPosition.width + 10) + "px; " +
219+
"height:" + (elementPosition.height + 10) + "px; " +
220+
"top:" + (elementPosition.top - 5) + "px;" +
221+
"left: " + (elementPosition.left - 5) + "px;");
222222
//set current step to the label
223223
oldHelperNumberLayer.innerHTML = targetElement.getAttribute("data-step");
224224
//set current tooltip text
@@ -237,13 +237,14 @@
237237
tooltipLayer = document.createElement("div");
238238

239239
helperLayer.className = "introjs-helperLayer";
240-
helperLayer.setAttribute("style", "width: " + (elementPosition.width + 10) + "px; " +
240+
helperLayer.setAttribute("style", "width: " + (elementPosition.width + 10) + "px; " +
241241
"height:" + (elementPosition.height + 10) + "px; " +
242-
"top:" + (elementPosition.top - 5) + "px;" +
243-
"left: " + (elementPosition.left - 5) + "px;");
242+
"top:" + (elementPosition.top - 5) + "px;" +
243+
"left: " + (elementPosition.left - 5) + "px;");
244+
245+
//add helper layer to target element
246+
this._targetElement.appendChild(helperLayer);
244247

245-
document.body.appendChild(helperLayer);
246-
247248
helperNumberLayer.className = "introjs-helperNumberLayer";
248249
arrowLayer.className = 'introjs-arrow';
249250
tooltipLayer.className = "introjs-tooltip";
@@ -282,7 +283,7 @@
282283
}
283284

284285
//scroll the page to the element position
285-
if(typeof(targetElement.scrollIntoViewIfNeeded) === "function") {
286+
if (typeof(targetElement.scrollIntoViewIfNeeded) === "function") {
286287
//awesome method guys: https://bugzilla.mozilla.org/show_bug.cgi?id=403510
287288
//but I think this method has some problems with IE < 7.0, I should find a proper failover way
288289
targetElement.scrollIntoViewIfNeeded();
@@ -304,11 +305,17 @@
304305
//set css class name
305306
overlayLayer.className = "introjs-overlay";
306307

307-
//set overlay layer position
308-
var elementPosition = _getOffset(targetElm);
309-
if(elementPosition) {
310-
styleText += "width: " + elementPosition.width + "px; height:" + elementPosition.height + "px; top:" + elementPosition.top + "px;left: " + elementPosition.left + "px;";
308+
//check if the target element is body, we should calculate the size of overlay layer in a better way
309+
if (targetElm.tagName.toLowerCase() == "body") {
310+
styleText += "top: 0;bottom: 0; left: 0;right: 0;position: fixed;";
311311
overlayLayer.setAttribute("style", styleText);
312+
} else {
313+
//set overlay layer position
314+
var elementPosition = _getOffset(targetElm);
315+
if(elementPosition) {
316+
styleText += "width: " + elementPosition.width + "px; height:" + elementPosition.height + "px; top:" + elementPosition.top + "px;left: " + elementPosition.left + "px;";
317+
overlayLayer.setAttribute("style", styleText);
318+
}
312319
}
313320

314321
targetElm.appendChild(overlayLayer);

0 commit comments

Comments
 (0)