@@ -71,6 +71,16 @@ const getOptions = (options: any): StandardBehaviorOptions => {
71
71
return { block : 'start' , inline : 'nearest' }
72
72
}
73
73
74
+ const getScrollMargins = ( target : Element ) => {
75
+ const computedStyle = window . getComputedStyle ( target )
76
+ return {
77
+ top : parseFloat ( computedStyle . scrollMarginTop ) || 0 ,
78
+ right : parseFloat ( computedStyle . scrollMarginRight ) || 0 ,
79
+ bottom : parseFloat ( computedStyle . scrollMarginBottom ) || 0 ,
80
+ left : parseFloat ( computedStyle . scrollMarginLeft ) || 0 ,
81
+ }
82
+ }
83
+
74
84
// Determine if the element is part of the document (including shadow dom)
75
85
// Derived from code of Andy Desmarais
76
86
// https://terodox.tech/how-to-tell-if-an-element-is-in-the-dom-including-the-shadow-dom/
@@ -127,14 +137,18 @@ function scrollIntoView<T = unknown>(
127
137
return
128
138
}
129
139
140
+ const margins = getScrollMargins ( target )
141
+
130
142
if ( isCustomScrollBehavior < T > ( options ) ) {
131
143
return options . behavior ( compute ( target , options ) )
132
144
}
133
145
134
146
const behavior = typeof options === 'boolean' ? undefined : options ?. behavior
135
147
136
148
for ( const { el, top, left } of compute ( target , getOptions ( options ) ) ) {
137
- el . scroll ( { top, left, behavior } )
149
+ const adjustedTop = top - margins . top + margins . bottom
150
+ const adjustedLeft = left - margins . left + margins . right
151
+ el . scroll ( { top : adjustedTop , left : adjustedLeft , behavior } )
138
152
}
139
153
}
140
154
0 commit comments