Skip to content

Commit ec559ab

Browse files
authored
feat(Menu): improve scrollIntoView performance (#7423)
* refactor: 增强滚动视口逻辑 * refactor: 增强 dispose 逻辑 * refactor: 优化代码消除提示信息 * doc: 更新锚点滚动逻辑 * refactor: 重构代码
1 parent b852288 commit ec559ab

4 files changed

Lines changed: 42 additions & 28 deletions

File tree

src/BootstrapBlazor.Server/Components/Layout/ComponentLayout.razor.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
export function scrollToAnchor() {
1+
export function scrollToAnchor() {
22
const hash = decodeURI(location.hash)
33
if (hash) {
44
const anchor = hash.split('-')[0]
55
const el = document.querySelector(anchor)
66
if (el) {
77
const handler = setTimeout(() => {
8-
el.scrollIntoView({ behavior: 'smooth', block: 'start' })
98
clearTimeout(handler)
9+
el.scrollIntoView({ behavior: 'smooth', block: 'start' })
1010
}, 1000)
1111
}
1212
}
Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
1-
import Data from "../../_content/BootstrapBlazor/modules/data.js"
1+
import Data from "../../_content/BootstrapBlazor/modules/data.js"
22
import EventHandler from "../../_content/BootstrapBlazor/modules/event-handler.js"
33

44
export function init(id) {
5-
const navmenu = {
6-
navbar: document.querySelector('.navbar-toggler'),
7-
menu: document.querySelector('.sidebar-content')
8-
}
9-
Data.set(id, navmenu)
5+
const navbar = document.querySelector('.navbar-toggler');
6+
const menu = document.querySelector('.sidebar-content');
7+
Data.set(id, { navbar, menu });
108

11-
EventHandler.on(navmenu.navbar, 'click', () => {
12-
navmenu.menu.classList.toggle('show')
9+
EventHandler.on(navbar, 'click', () => {
10+
menu.classList.toggle('show')
1311
})
14-
EventHandler.on(navmenu.menu, 'click', '.nav-link', e => {
12+
EventHandler.on(menu, 'click', '.nav-link', e => {
1513
const link = e.delegateTarget
1614
const url = link.getAttribute('href');
1715
if (url !== '#') {
18-
navmenu.menu.classList.remove('show')
16+
menu.classList.remove('show')
1917
}
2018
})
2119
}
@@ -24,6 +22,7 @@ export function dispose(id) {
2422
const data = Data.get(id);
2523
Data.remove(id);
2624

27-
EventHandler.off(data.navbar, 'click');
28-
EventHandler.off(data.menu, 'click', '.nav-link');
25+
const { navbar, menu } = data;
26+
EventHandler.off(navbar, 'click');
27+
EventHandler.off(menu, 'click', '.nav-link');
2928
}

src/BootstrapBlazor/Components/Menu/Menu.razor.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Licensed to the .NET Foundation under one or more agreements.
1+
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the Apache 2.0 License
33
// See the LICENSE file in the project root for more information.
44
// Maintainer: Argo Zhang(argo@live.ca) Website: https://www.blazor.zone
@@ -144,11 +144,11 @@ private string GetUrl()
144144
var url = Navigator.ToBaseRelativePath(Navigator.Uri);
145145
if (url.Contains('?'))
146146
{
147-
url = url[..url.IndexOf("?")];
147+
url = url[..url.IndexOf('?')];
148148
}
149149
if (url.Contains('#'))
150150
{
151-
url = url[..url.IndexOf("#")];
151+
url = url[..url.IndexOf('#')];
152152
}
153153
return url;
154154
}

src/BootstrapBlazor/Components/Menu/Menu.razor.js

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getTargetElement, getTransitionDelayDurationFromElement } from "../../modules/utility.js"
1+
import { getTargetElement, getTransitionDelayDurationFromElement } from "../../modules/utility.js"
22
import Data from "../../modules/data.js"
33

44
export function init(id) {
@@ -48,7 +48,7 @@ export function init(id) {
4848
}
4949
Data.set(id, menu)
5050

51-
scrollElementToView(menu.element)
51+
scrollElementToView(menu)
5252
}
5353

5454
export function update(id) {
@@ -74,19 +74,24 @@ export function update(id) {
7474
}
7575
});
7676

77-
scrollElementToView(menu.element)
77+
scrollElementToView(menu)
7878
}
7979

8080
export function scrollToView(id) {
8181
const menu = Data.get(id)
8282
if (menu) {
83-
scrollElementToView(menu.element);
83+
scrollElementToView(menu);
8484
}
8585
}
8686

8787
export function dispose(id) {
8888
const menu = Data.get(id)
89-
Data.remove(id)
89+
Data.remove(id);
90+
91+
if (menu.handler) {
92+
clearInterval(menu.handler);
93+
menu.handler = null;
94+
}
9095

9196
menu.collapses.forEach(el => {
9297
const target = getTargetElement(el)
@@ -97,13 +102,23 @@ export function dispose(id) {
97102
})
98103
}
99104

100-
const scrollElementToView = element => {
105+
const scrollElementToView = menu => {
106+
const { element } = menu;
101107
const scroll = element.hasAttribute('data-bb-scroll-view')
102108
if (scroll) {
103-
var links = [...element.querySelectorAll('.nav-link.active')]
104-
if (links.length > 0) {
105-
var link = links.pop()
106-
link.scrollIntoView({ behavior: 'smooth', block: 'center', inline: 'start' });
107-
}
109+
let count = 0;
110+
menu.handler = setInterval(() => {
111+
if (count < 3) {
112+
var links = [...element.querySelectorAll('.nav-link.active')]
113+
if (links.length > 0) {
114+
clearInterval(menu.handler);
115+
menu.handler = null;
116+
117+
var link = links.pop()
118+
link.scrollIntoView({ behavior: 'smooth', block: 'center', inline: 'start' });
119+
}
120+
}
121+
count++;
122+
}, 100);
108123
}
109124
}

0 commit comments

Comments
 (0)