Skip to content

Commit c013508

Browse files
committed
Adds new scrolling functionality
1 parent 324c982 commit c013508

File tree

1 file changed

+27
-7
lines changed

1 file changed

+27
-7
lines changed

intro.js

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -297,13 +297,33 @@
297297
//change to new intro item
298298
targetElement.className += " introjs-relativePosition";
299299
}
300-
301-
//scroll the page to the element position
302-
if (typeof(targetElement.scrollIntoViewIfNeeded) === "function") {
303-
//awesome method guys: https://bugzilla.mozilla.org/show_bug.cgi?id=403510
304-
//but I think this method has some problems with IE < 7.0, I should find a proper failover way
305-
targetElement.scrollIntoViewIfNeeded();
306-
}
300+
301+
if (!elementInViewport(targetElement)) {
302+
var rect = targetElement.getBoundingClientRect()
303+
top = rect.bottom-rect.height,
304+
bottom = rect.bottom-window.innerHeight;
305+
306+
// Scroll up
307+
if (top < 0) {
308+
window.scrollBy(0, top-30); // 30px padding from edge to look nice
309+
310+
// Scroll down
311+
} else {
312+
window.scrollBy(0, bottom+100); // 70px + 30px padding from edge to look nice
313+
}
314+
}
315+
}
316+
317+
// http://stackoverflow.com/questions/123999/how-to-tell-if-a-dom-element-is-visible-in-the-current-viewport
318+
function elementInViewport(el) {
319+
var rect = el.getBoundingClientRect();
320+
321+
return (
322+
rect.top >= 0 &&
323+
rect.left >= 0 &&
324+
(rect.bottom+80) <= window.innerHeight && // add 80 to get the text right
325+
rect.right <= window.innerWidth
326+
);
307327
}
308328

309329
/**

0 commit comments

Comments
 (0)