Skip to content

Commit 0ea46bb

Browse files
authored
refactor(ScrollIntoView): use nearest value instead of center (#5049)
* fix: 更新滚动脚本 * doc: 更新 TableSection 示例滚动逻辑 * doc: 更新文档 * chore: bump version 9.2.5-beta02 * revert: 撤销代码 * doc: 更新注释
1 parent f5f401d commit 0ea46bb

File tree

6 files changed

+14
-26
lines changed

6 files changed

+14
-26
lines changed
Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
export function scroll(id) {
22
const element = document.getElementById(id);
33
if (element) {
4-
const selectedRow = element.querySelector('.form-check.is-checked');
4+
const selectedRow = element.querySelector('tr.active');
55
if (selectedRow) {
6-
const row = selectedRow.closest('tr');
7-
if (row) {
8-
row.scrollIntoView({ behavior: 'smooth', block: 'center' });
9-
}
6+
selectedRow.scrollIntoView({ behavior: 'smooth', block: 'center' });
107
}
118
}
129
}

src/BootstrapBlazor/BootstrapBlazor.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk.Razor">
22

33
<PropertyGroup>
4-
<Version>9.2.5-beta01</Version>
4+
<Version>9.2.5-beta02</Version>
55
</PropertyGroup>
66

77
<ItemGroup>

src/BootstrapBlazor/Components/AutoComplete/AutoComplete.razor.js

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -127,26 +127,12 @@ const handlerKeyup = (ac, e) => {
127127
else if (index > items.length - 1) {
128128
index = 0;
129129
}
130-
items[index].classList.add('active');
131-
const top = getTop(menu, index);
132-
const hehavior = el.getAttribute('data-bb-scroll-behavior') ?? 'smooth';
133-
menu.scrollTo({ top: top, left: 0, behavior: hehavior });
130+
current = items[index];
131+
current.classList.add('active');
132+
scrollIntoView(el, current);
134133
}
135134
}
136135

137-
const getTop = (menu, index) => {
138-
const styles = getComputedStyle(menu)
139-
const maxHeight = parseInt(styles.maxHeight) / 2
140-
const itemHeight = getHeight(menu.querySelector('.dropdown-item'))
141-
const height = itemHeight * index
142-
const count = Math.floor(maxHeight / itemHeight);
143-
let top = 0;
144-
if (height > maxHeight) {
145-
top = itemHeight * (index > count ? index - count : index)
146-
}
147-
return top;
148-
}
149-
150136
export function showList(id) {
151137
const ac = Data.get(id)
152138
if (ac) {
@@ -177,4 +163,9 @@ export function dispose(id) {
177163
}
178164
}
179165

166+
const scrollIntoView = (el, item) => {
167+
const behavior = el.getAttribute('data-bb-scroll-behavior') ?? 'smooth';
168+
item.scrollIntoView({ behavior: behavior, block: "nearest", inline: "start" });
169+
}
170+
180171
export { handleKeyUp, select, selectAllByFocus, selectAllByEnter }

src/BootstrapBlazor/Components/Select/Select.razor.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,5 +141,5 @@ function indexOf(el, element) {
141141

142142
const scrollIntoView = (el, item) => {
143143
const behavior = el.getAttribute('data-bb-scroll-behavior') ?? 'smooth';
144-
item.scrollIntoView({ behavior: behavior, block: "center", inline: "nearest" });
144+
item.scrollIntoView({ behavior: behavior, block: "nearest", inline: "start" });
145145
}

src/BootstrapBlazor/Components/TreeView/TreeView.razor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ public partial class TreeView<TItem> : IModelEqualityComparer<TItem>
258258
public bool EnableKeyboard { get; set; }
259259

260260
/// <summary>
261-
/// 获得/设置 是否键盘上下键操作当前选中节点与视窗关系配置 默认 null 使用 { behavior: "smooth", block: "center", inline: "nearest" }
261+
/// 获得/设置 是否键盘上下键操作当前选中节点与视窗关系配置 默认 null 使用 { behavior: "smooth", block: "nearest", inline: "start" }
262262
/// </summary>
263263
[Parameter]
264264
public ScrollIntoViewOptions? ScrollIntoViewOptions { get; set; }

src/BootstrapBlazor/Components/TreeView/TreeView.razor.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export function scroll(id, options) {
3232
const el = document.getElementById(id);
3333
const item = el.querySelector(".tree-content.active");
3434
if (item) {
35-
item.scrollIntoView(options ?? { behavior: 'smooth', block: 'start', inline: 'nearest' });
35+
item.scrollIntoView(options ?? { behavior: 'smooth', block: 'nearest', inline: 'start' });
3636
}
3737
}
3838

0 commit comments

Comments
 (0)