Skip to content

Commit fc3f88b

Browse files
committed
fix: 更新滚动脚本
1 parent f5f401d commit fc3f88b

File tree

3 files changed

+13
-19
lines changed

3 files changed

+13
-19
lines changed

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

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,16 @@ export function init(id, invoke) {
2626
if (duration > 0) {
2727
ac.debounce = true
2828
EventHandler.on(input, 'keyup', debounce(e => {
29+
e.preventDefault();
2930
handlerKeyup(ac, e);
3031
}, duration, e => {
3132
return ['ArrowUp', 'ArrowDown', 'Escape', 'Enter', 'NumpadEnter'].indexOf(e.key) > -1
3233
}))
3334
}
3435
else {
3536
EventHandler.on(input, 'keyup', e => {
37+
e.stopPropagation();
38+
e.preventDefault();
3639
handlerKeyup(ac, e);
3740
})
3841
}
@@ -127,26 +130,12 @@ const handlerKeyup = (ac, e) => {
127130
else if (index > items.length - 1) {
128131
index = 0;
129132
}
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 });
133+
current = items[index];
134+
current.classList.add('active');
135+
scrollIntoView(el, current);
134136
}
135137
}
136138

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-
150139
export function showList(id) {
151140
const ac = Data.get(id)
152141
if (ac) {
@@ -177,4 +166,9 @@ export function dispose(id) {
177166
}
178167
}
179168

169+
const scrollIntoView = (el, item) => {
170+
const behavior = el.getAttribute('data-bb-scroll-behavior') ?? 'smooth';
171+
item.scrollIntoView({ behavior: behavior, block: "nearest", inline: "start" });
172+
}
173+
180174
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.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)