Skip to content

Commit a0270ac

Browse files
committed
feat(Scroll): 添加平滑滚动到底部的功能,用于聊天框和动态条目展示等
1 parent ad698e2 commit a0270ac

File tree

7 files changed

+65
-7
lines changed

7 files changed

+65
-7
lines changed

src/BootstrapBlazor.Server/Components/Samples/Scrolls.razor

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,19 @@
99

1010
<DemoBlock Title="@Localizer["ScrollNormalTitle"]" Introduction="@Localizer["ScrollNormalIntro"]" Name="Normal">
1111
<div class="scroll-demo border">
12-
<Scroll Height="200px">
13-
<div style="height: 400px;">@Localizer["ScrollNormalDescription"]</div>
14-
<div>@Localizer["ScrollNormalBottom"]</div>
12+
<Scroll @ref="_scroll" Height="200px">
13+
<div class="m-1">@Localizer["ScrollNormalDescription"]</div>
14+
<div style="height: 100px; max-width: 200px; background-color: var(--bs-primary)"></div>
15+
<div style="height: 100px; max-width: 200px; background-color: var(--bs-secondary)"></div>
16+
<div style="height: 100px; max-width: 200px; background-color: var(--bs-warning)"></div>
17+
<div style="height: 100px; max-width: 200px; background-color: var(--bs-success)"></div>
18+
<div style="height: 100px; max-width: 200px; background-color: var(--bs-info)"></div>
19+
<div class="m-1">@Localizer["ScrollNormalBottom"]</div>
1520
</Scroll>
21+
<Divider/>
22+
<div class="m-3">
23+
<Button OnClick="ScrollToBottom" Text="@Localizer["ScrollToBottom"]"></Button>
24+
</div>
1625
</div>
1726
</DemoBlock>
1827

src/BootstrapBlazor.Server/Components/Samples/Scrolls.razor.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,15 @@ private AttributeItem[] GetAttributes() =>
3333
DefaultValue = " — "
3434
}
3535
];
36+
37+
private Scroll? _scroll;
38+
39+
private async Task ScrollToBottom()
40+
{
41+
if (_scroll != null)
42+
{
43+
await _scroll.ScrollToBottom();
44+
}
45+
46+
}
3647
}

src/BootstrapBlazor.Server/Locales/en-US.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1335,8 +1335,9 @@
13351335
"ScrollTitle": "Scroll",
13361336
"ScrollDescription": "Add scroll bars to components whose height or width exceeds the standard",
13371337
"ScrollTips": "The scroll bar can be rendered only when its element has a fixed height. Its <code>height</code> attribute can be set through the coat element",
1338+
"ScrollToBottom": "Scroll to bottom",
13381339
"ScrollNormalTitle": "Common usage",
1339-
"ScrollNormalIntro": "Add a scroll bar to the component. By setting the height of <code>Height</code> to 200px, the scroll bar appears when the height of the inner child element is 400px",
1340+
"ScrollNormalIntro": "Add a scroll bar to the component. By setting the height of <code>Height</code> to 200px, the scroll bar appears when the height of the inner child element is 500px",
13401341
"Desc1": "Subassembly",
13411342
"Desc2": "Component height",
13421343
"ScrollNormalDescription": "Please scroll the scroll bar on the right",

src/BootstrapBlazor.Server/Locales/zh-CN.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1335,8 +1335,9 @@
13351335
"ScrollTitle": "Scroll 滚动条",
13361336
"ScrollDescription": "给高度或者宽度超标的组件增加滚动条",
13371337
"ScrollTips": "其元素必须拥有固定高度时才可呈现滚动条,可以通过外套元素设置其 <code>height</code> 属性",
1338+
"ScrollToBottom": "滚动到底部",
13381339
"ScrollNormalTitle": "普通用法",
1339-
"ScrollNormalIntro": "给组件增加滚动条,通过设置 <code>Height</code> 高度值为 200px 使内部子元素高度为 400px 时出现滚动条",
1340+
"ScrollNormalIntro": "给组件增加滚动条,通过设置 <code>Height</code> 高度值为 200px 使内部子元素高度为 500px 时出现滚动条",
13401341
"Desc1": "子组件",
13411342
"Desc2": "组件高度",
13421343
"ScrollNormalDescription": "请滚动右侧滚动条",
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
@namespace BootstrapBlazor.Components
2-
@inherits BootstrapComponentBase
2+
@inherits BootstrapModuleComponentBase
3+
@attribute [BootstrapModuleAutoLoader(AutoInvokeDispose = false)]
34

4-
<div @attributes="AdditionalAttributes" class="@ClassString" style="@StyleString">
5+
<div @attributes="AdditionalAttributes" id="@Id" class="@ClassString" style="@StyleString">
56
@ChildContent
67
</div>

src/BootstrapBlazor/Components/Scroll/Scroll.razor.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,12 @@ public partial class Scroll
5959
[Inject]
6060
[NotNull]
6161
private IOptionsMonitor<BootstrapBlazorOptions>? Options { get; set; }
62+
63+
/// <summary>
64+
/// 滚动到底部
65+
/// </summary>
66+
public async Task ScrollToBottom()
67+
{
68+
await InvokeVoidAsync("scrollToBottom", Id);
69+
}
6270
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
export function scrollToBottom(id) {
2+
let el = document.getElementById(id);
3+
const start = el.scrollTop;
4+
const end = el.scrollHeight;
5+
const distance = end - start;
6+
const duration = 500; // 动画持续时间(毫秒)
7+
const startTime = performance.now();
8+
9+
function scrollStep(currentTime) {
10+
const elapsed = currentTime - startTime;
11+
const progress = Math.min(elapsed / duration, 1); // 确保进度不超过1
12+
el.scrollTop = start + distance * progress;
13+
14+
if (progress < 1) {
15+
window.requestAnimationFrame(scrollStep);
16+
}
17+
}
18+
19+
window.requestAnimationFrame(scrollStep);
20+
}
21+
22+
export function init(id) {
23+
const el = document.getElementById(id)
24+
if (el === null) {
25+
26+
}
27+
}

0 commit comments

Comments
 (0)