@@ -25,6 +25,21 @@ color.getBackgroundColor = function getBackgroundColor(
25
25
const clientHeight = elm . getBoundingClientRect ( ) . height ;
26
26
const alignToTop = clientHeight - 2 >= window . innerHeight * 2 ;
27
27
elm . scrollIntoView ( alignToTop ) ;
28
+
29
+ // ensure element is scrolled into view horizontally
30
+ let center , scrollParent ;
31
+ do {
32
+ const rect = elm . getBoundingClientRect ( ) ;
33
+
34
+ // 'x' does not exist in IE11
35
+ const x = 'x' in rect ? rect . x : rect . left ;
36
+ center = x + rect . width / 2 ;
37
+
38
+ if ( center < 0 ) {
39
+ scrollParent = getScrollParent ( elm ) ;
40
+ scrollParent . scrollLeft = 0 ;
41
+ }
42
+ } while ( center < 0 && scrollParent !== document . documentElement ) ;
28
43
}
29
44
30
45
let bgColors = [ ] ;
@@ -378,6 +393,36 @@ function contentOverlapping(targetElement, bgNode) {
378
393
return false ;
379
394
}
380
395
396
+ /**
397
+ * Return the scrolling parent element
398
+ * @see https://stackoverflow.com/questions/35939886/find-first-scrollable-parent#42543908
399
+ * @param {Element } element
400
+ * @param {Boolean } includeHidden
401
+ * @return {Element }
402
+ */
403
+ function getScrollParent ( element , includeHidden ) {
404
+ var style = getComputedStyle ( element ) ;
405
+ var excludeStaticParent = style . position === 'absolute' ;
406
+ var overflowRegex = includeHidden ? / ( a u t o | s c r o l l | h i d d e n ) / : / ( a u t o | s c r o l l ) / ;
407
+
408
+ if ( style . position === 'fixed' ) {
409
+ return document . documentElement ;
410
+ }
411
+ for ( var parent = element ; ( parent = parent . parentElement ) ; ) {
412
+ style = getComputedStyle ( parent ) ;
413
+ if ( excludeStaticParent && style . position === 'static' ) {
414
+ continue ;
415
+ }
416
+ if (
417
+ overflowRegex . test ( style . overflow + style . overflowY + style . overflowX )
418
+ ) {
419
+ return parent ;
420
+ }
421
+ }
422
+
423
+ return document . documentElement ;
424
+ }
425
+
381
426
/**
382
427
* Determines whether an element has a fully opaque background, whether solid color or an image
383
428
* @param {Element } node
0 commit comments