Skip to content

Commit ad6e895

Browse files
feat: 优化 Swup 实例管理和过渡效果样式
Signed-off-by: wangsimiao1 <wangsimiao1@xiaomi.com>
1 parent 07b4334 commit ad6e895

2 files changed

Lines changed: 10 additions & 12 deletions

File tree

_javascript/modules/layouts/swup-setup.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
11
import Swup from 'swup';
22
import SwupHeadPlugin from '@swup/head-plugin';
3-
import SwupScriptsPlugin from '@swup/scripts-plugin';
3+
4+
let swupInstance = null;
45

56
export function initSwup() {
6-
const swup = new Swup({
7+
// Prevent multiple instances
8+
if (swupInstance) return;
9+
10+
swupInstance = new Swup({
711
containers: ['#swup'],
812
animationSelector: '.swup-transition-main',
913
cache: true,
14+
animateHistoryBrowsing: true,
1015
plugins: [
1116
new SwupHeadPlugin({
12-
persistAssets: true // keep existing CSS/JS, avoid re-downloading
13-
}),
14-
new SwupScriptsPlugin({
15-
head: true,
16-
body: true
17+
persistAssets: true
1718
})
1819
]
1920
});
2021

2122
// Update sidebar active state after navigation
22-
swup.hooks.on('page:view', () => {
23+
swupInstance.hooks.on('page:view', () => {
2324
const currentPath = window.location.pathname;
2425
const navItems = document.querySelectorAll('#sidebar .nav-item');
2526

_sass/components/_swup.scss

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
/* Swup page transition */
22
.swup-transition-main {
3+
opacity: 1;
34
transition: opacity 0.2s ease-in-out;
45
}
56

67
html.is-changing .swup-transition-main {
78
opacity: 0;
89
}
9-
10-
html.is-animating .swup-transition-main {
11-
opacity: 1;
12-
}

0 commit comments

Comments
 (0)