@@ -12,17 +12,18 @@ namespace Majorsoft.Blazor.Components.Common.JsInterop.Scroll
12
12
public static class ElementReferenceScrollExtensions
13
13
{
14
14
/// <summary>
15
- /// Scrolls HTML page to given element
15
+ /// Scrolls HTML page to given element.
16
16
/// </summary>
17
17
/// <param name="elementReference">Blazor reference to an HTML element</param>
18
+ /// <param name="smooth">Scroll should jump or smoothly scroll Note: might not all browsers support it</param>
18
19
/// <returns>Async Task</returns>
19
- public static async Task ScrollToElementAsync ( this ElementReference elementReference )
20
+ public static async Task ScrollToElementAsync ( this ElementReference elementReference , bool smooth = false )
20
21
{
21
22
await using ( var module = await elementReference . GetJsObject ( ) )
22
23
{
23
24
if ( module is not null )
24
25
{
25
- await module . InvokeVoidAsync ( "scrollToElement" , elementReference ) ;
26
+ await module . InvokeVoidAsync ( "scrollToElement" , elementReference , smooth ) ;
26
27
}
27
28
}
28
29
}
@@ -31,14 +32,15 @@ public static async Task ScrollToElementAsync(this ElementReference elementRefer
31
32
/// Scrolls inside the given element to the bottom (end).
32
33
/// </summary>
33
34
/// <param name="elementReference">Blazor reference to an HTML element</param>
35
+ /// <param name="smooth">Scroll should jump or smoothly scroll Note: might not all browsers support it</param>
34
36
/// <returns>Async Task</returns>
35
- public static async Task ScrollToEndAsync ( this ElementReference elementReference )
37
+ public static async Task ScrollToEndAsync ( this ElementReference elementReference , bool smooth = false )
36
38
{
37
39
await using ( var module = await elementReference . GetJsObject ( ) )
38
40
{
39
41
if ( module is not null )
40
42
{
41
- await module . InvokeVoidAsync ( "scrollToEnd" , elementReference ) ;
43
+ await module . InvokeVoidAsync ( "scrollToEnd" , elementReference , smooth ) ;
42
44
}
43
45
}
44
46
}
@@ -47,14 +49,15 @@ public static async Task ScrollToEndAsync(this ElementReference elementReference
47
49
/// Scrolls inside the given element to the beginning (top).
48
50
/// </summary>
49
51
/// <param name="elementReference">Blazor reference to an HTML element</param>
52
+ /// <param name="smooth">Scroll should jump or smoothly scroll Note: might not all browsers support it</param>
50
53
/// <returns>Async Task</returns>
51
- public static async Task ScrollToTopAsync ( this ElementReference elementReference )
54
+ public static async Task ScrollToTopAsync ( this ElementReference elementReference , bool smooth = false )
52
55
{
53
56
await using ( var module = await elementReference . GetJsObject ( ) )
54
57
{
55
58
if ( module is not null )
56
59
{
57
- await module . InvokeVoidAsync ( "scrollToTop" , elementReference ) ;
60
+ await module . InvokeVoidAsync ( "scrollToTop" , elementReference , smooth ) ;
58
61
}
59
62
}
60
63
}
@@ -64,14 +67,15 @@ public static async Task ScrollToTopAsync(this ElementReference elementReference
64
67
/// </summary>
65
68
/// <param name="elementReference">Blazor reference to an HTML element</param>
66
69
/// <param name="xPos">Scroll X position</param>
70
+ /// <param name="smooth">Scroll should jump or smoothly scroll Note: might not all browsers support it</param>
67
71
/// <returns>Async Task</returns>
68
- public static async Task ScrollToXAsync ( this ElementReference elementReference , double xPos )
72
+ public static async Task ScrollToXAsync ( this ElementReference elementReference , double xPos , bool smooth = false )
69
73
{
70
74
await using ( var module = await elementReference . GetJsObject ( ) )
71
75
{
72
76
if ( module is not null )
73
77
{
74
- await module . InvokeVoidAsync ( "scrollToX" , elementReference , xPos ) ;
78
+ await module . InvokeVoidAsync ( "scrollToX" , elementReference , xPos , smooth ) ;
75
79
}
76
80
}
77
81
}
@@ -81,20 +85,40 @@ public static async Task ScrollToXAsync(this ElementReference elementReference,
81
85
/// </summary>
82
86
/// <param name="elementReference">Blazor reference to an HTML element</param>
83
87
/// <param name="yPos">Scroll Y position</param>
88
+ /// <param name="smooth">Scroll should jump or smoothly scroll Note: might not all browsers support it</param>
84
89
/// <returns>Async Task</returns>
85
- public static async Task ScrollToYAsync ( this ElementReference elementReference , double yPos )
90
+ public static async Task ScrollToYAsync ( this ElementReference elementReference , double yPos , bool smooth = false )
86
91
{
87
92
await using ( var module = await elementReference . GetJsObject ( ) )
88
93
{
89
94
if ( module is not null )
90
95
{
91
- await module . InvokeVoidAsync ( "scrollToY" , elementReference , yPos ) ;
96
+ await module . InvokeVoidAsync ( "scrollToY" , elementReference , yPos , smooth ) ;
92
97
}
93
98
}
94
99
}
95
100
96
101
/// <summary>
97
- /// Returns given element scroll X position.
102
+ /// Scrolls inside the given element to the given X and Y positions.
103
+ /// </summary>
104
+ /// <param name="elementReference">Blazor reference to an HTML element</param>
105
+ /// <param name="xPos">Scroll X position</param>
106
+ /// <param name="yPos">Scroll Y position</param>
107
+ /// <param name="smooth">Scroll should jump or smoothly scroll Note: might not all browsers support it</param>
108
+ /// <returns>Async Task</returns>
109
+ public static async Task ScrollToAsync ( this ElementReference elementReference , double xPos , double yPos , bool smooth = false )
110
+ {
111
+ await using ( var module = await elementReference . GetJsObject ( ) )
112
+ {
113
+ if ( module is not null )
114
+ {
115
+ await module . InvokeVoidAsync ( "scrollTo" , elementReference , xPos , yPos , smooth ) ;
116
+ }
117
+ }
118
+ }
119
+
120
+ /// <summary>
121
+ /// Returns given element scroll X (left) position.
98
122
/// </summary>
99
123
/// <param name="elementReference">Blazor reference to an HTML element</param>
100
124
/// <returns>Async Task with X pos</returns>
@@ -110,6 +134,23 @@ public static async Task<double> GetScrollXPositionAsync(this ElementReference e
110
134
111
135
return 0 ;
112
136
}
137
+ /// <summary>
138
+ /// Returns given element scroll Y (top) position.
139
+ /// </summary>
140
+ /// <param name="elementReference">Blazor reference to an HTML element</param>
141
+ /// <returns>Async Task with Y pos</returns>
142
+ public static async Task < double > GetScrollYPositionAsync ( this ElementReference elementReference )
143
+ {
144
+ await using ( var module = await elementReference . GetJsObject ( ) )
145
+ {
146
+ if ( module is not null )
147
+ {
148
+ return await module . InvokeAsync < double > ( "getScrollYPosition" , elementReference ) ;
149
+ }
150
+ }
151
+
152
+ return 0 ;
153
+ }
113
154
114
155
/// <summary>
115
156
/// Returns given element is visible on HTML document or not.
0 commit comments