File tree Expand file tree Collapse file tree 1 file changed +27
-7
lines changed Expand file tree Collapse file tree 1 file changed +27
-7
lines changed Original file line number Diff line number Diff line change 297
297
//change to new intro item
298
298
targetElement . className += " introjs-relativePosition" ;
299
299
}
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
+ ) ;
307
327
}
308
328
309
329
/**
You can’t perform that action at this time.
0 commit comments