Shubham Sawant commited on
Commit
edbe668
·
1 Parent(s): 4db42b4

Change bringInView() implementation to support unsupported browsers

Browse files
Files changed (1) hide show
  1. src/core/element.js +23 -12
src/core/element.js CHANGED
@@ -71,6 +71,17 @@ export default class Element {
71
  );
72
  }
73
 
 
 
 
 
 
 
 
 
 
 
 
74
  /**
75
  * Brings the element to middle of the view port if not in view
76
  */
@@ -79,20 +90,20 @@ export default class Element {
79
  return;
80
  }
81
 
82
- // If browser supports scrollIntoView, use that otherwise center it manually
83
- if (this.node.scrollIntoView) {
84
- const scrollIntoViewOptions = this.options.scrollIntoViewOptions || {
 
 
 
 
 
85
  behavior: 'smooth',
86
  block: 'center',
87
- };
88
-
89
- this.node.scrollIntoView(scrollIntoViewOptions);
90
- } else {
91
- const elementRect = this.node.getBoundingClientRect();
92
- const absoluteElementTop = elementRect.top + this.window.pageYOffset;
93
- const middle = absoluteElementTop - (this.window.innerHeight / 2);
94
-
95
- this.window.scrollTo(0, middle);
96
  }
97
  }
98
 
 
71
  );
72
  }
73
 
74
+ /**
75
+ * Manually scrolls to the position of element if `scrollIntoView` fails
76
+ */
77
+ scrollManually() {
78
+ const elementRect = this.node.getBoundingClientRect();
79
+ const absoluteElementTop = elementRect.top + this.window.pageYOffset;
80
+ const middle = absoluteElementTop - (this.window.innerHeight / 2);
81
+
82
+ this.window.scrollTo(0, middle);
83
+ }
84
+
85
  /**
86
  * Brings the element to middle of the view port if not in view
87
  */
 
90
  return;
91
  }
92
 
93
+ // If browser does not support scrollIntoView
94
+ if (!this.node.scrollIntoView) {
95
+ this.scrollManually();
96
+ return;
97
+ }
98
+
99
+ try {
100
+ this.node.scrollIntoView(this.options.scrollIntoViewOptions || {
101
  behavior: 'smooth',
102
  block: 'center',
103
+ });
104
+ } catch (e) {
105
+ // `block` option is not allowed in older versions of firefox, scroll manually
106
+ this.scrollManually();
 
 
 
 
 
107
  }
108
  }
109