Skip to content

Commit 07b4334

Browse files
feat: 添加 Swup 页面过渡效果及相关功能
Signed-off-by: wangsimiao1 <wangsimiao1@xiaomi.com>
1 parent ac43092 commit 07b4334

8 files changed

Lines changed: 177 additions & 34 deletions

File tree

_javascript/commons.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { basic, initSidebar, initTopbar } from './modules/layouts';
1+
import { basic, initSidebar, initTopbar, initSwup } from './modules/layouts';
22

33
initSidebar();
44
initTopbar();
55
basic();
6+
initSwup();

_javascript/modules/layouts.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export { basic } from './layouts/basic';
22
export { initSidebar } from './layouts/sidebar';
33
export { initTopbar } from './layouts/topbar';
4+
export { initSwup } from './layouts/swup-setup';
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import Swup from 'swup';
2+
import SwupHeadPlugin from '@swup/head-plugin';
3+
import SwupScriptsPlugin from '@swup/scripts-plugin';
4+
5+
export function initSwup() {
6+
const swup = new Swup({
7+
containers: ['#swup'],
8+
animationSelector: '.swup-transition-main',
9+
cache: true,
10+
plugins: [
11+
new SwupHeadPlugin({
12+
persistAssets: true // keep existing CSS/JS, avoid re-downloading
13+
}),
14+
new SwupScriptsPlugin({
15+
head: true,
16+
body: true
17+
})
18+
]
19+
});
20+
21+
// Update sidebar active state after navigation
22+
swup.hooks.on('page:view', () => {
23+
const currentPath = window.location.pathname;
24+
const navItems = document.querySelectorAll('#sidebar .nav-item');
25+
26+
navItems.forEach((item) => {
27+
const link = item.querySelector('a');
28+
if (!link) return;
29+
30+
const href = link.getAttribute('href');
31+
const isActive =
32+
(href === '/' && currentPath === '/') ||
33+
(href !== '/' && currentPath.startsWith(href));
34+
35+
item.classList.toggle('active', isActive);
36+
});
37+
38+
// Scroll content area to top
39+
window.scrollTo({ top: 0 });
40+
});
41+
}

_layouts/default.html

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -23,40 +23,43 @@
2323
<div class="container d-flex flex-column px-xxl-5">
2424
{% include topbar.html lang=lang %}
2525

26-
<div class="row flex-grow-1">
27-
<main aria-label="Main Content" class="col-12 col-lg-11 col-xl-9 px-md-4">
28-
{% if layout.layout == 'default' %}
29-
{% include refactor-content.html content=content lang=lang %}
30-
{% else %}
31-
{{ content }}
32-
{% endif %}
33-
</main>
34-
35-
<!-- panel -->
36-
<aside aria-label="Panel" id="panel-wrapper" class="col-xl-3 ps-2 text-muted">
37-
<div class="access">
38-
{% include_cached update-list.html lang=lang %}
39-
{% include_cached trending-tags.html lang=lang %}
40-
</div>
41-
42-
{% for _include in layout.panel_includes %}
43-
{% assign _include_path = _include | append: '.html' %}
44-
{% include {{ _include_path }} lang=lang %}
45-
{% endfor %}
46-
</aside>
47-
</div>
26+
<div id="swup" class="swup-transition-main">
27+
<div class="row flex-grow-1">
28+
<main aria-label="Main Content" class="col-12 col-lg-11 col-xl-9 px-md-4">
29+
{% if layout.layout == 'default' %}
30+
{% include refactor-content.html content=content lang=lang %}
31+
{% else %}
32+
{{ content }}
33+
{% endif %}
34+
</main>
35+
36+
<!-- panel -->
37+
<aside aria-label="Panel" id="panel-wrapper" class="col-xl-3 ps-2 text-muted">
38+
<div class="access">
39+
{% include_cached update-list.html lang=lang %}
40+
{% include_cached trending-tags.html lang=lang %}
41+
</div>
42+
43+
{% for _include in layout.panel_includes %}
44+
{% assign _include_path = _include | append: '.html' %}
45+
{% include {{ _include_path }} lang=lang %}
46+
{% endfor %}
47+
</aside>
48+
</div>
4849

49-
<div class="row">
50-
<!-- tail -->
51-
<div id="tail-wrapper" class="col-12 col-lg-11 col-xl-9 px-md-4">
52-
{% for _include in layout.tail_includes %}
53-
{% assign _include_path = _include | append: '.html' %}
54-
{% include {{ _include_path }} lang=lang %}
55-
{% endfor %}
50+
<div class="row">
51+
<!-- tail -->
52+
<div id="tail-wrapper" class="col-12 col-lg-11 col-xl-9 px-md-4">
53+
{% for _include in layout.tail_includes %}
54+
{% assign _include_path = _include | append: '.html' %}
55+
{% include {{ _include_path }} lang=lang %}
56+
{% endfor %}
5657

57-
{% include_cached footer.html lang=lang %}
58+
{% include_cached footer.html lang=lang %}
59+
</div>
5860
</div>
5961
</div>
62+
<!-- /#swup -->
6063

6164
{% include_cached search-results.html lang=lang %}
6265
</div>

_sass/components/_index.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
@forward 'buttons';
22
@forward 'popups';
3+
@forward 'swup';

_sass/components/_swup.scss

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/* Swup page transition */
2+
.swup-transition-main {
3+
transition: opacity 0.2s ease-in-out;
4+
}
5+
6+
html.is-changing .swup-transition-main {
7+
opacity: 0;
8+
}
9+
10+
html.is-animating .swup-transition-main {
11+
opacity: 1;
12+
}

package-lock.json

Lines changed: 82 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,18 @@
1515
},
1616
"devDependencies": {
1717
"@babel/core": "^7.24.0",
18-
"@babel/preset-env": "^7.24.0",
1918
"@babel/plugin-transform-class-properties": "^7.24.0",
2019
"@babel/plugin-transform-private-methods": "^7.24.0",
20+
"@babel/preset-env": "^7.24.0",
2121
"@rollup/plugin-babel": "^6.0.4",
2222
"@rollup/plugin-node-resolve": "^15.2.3",
2323
"@rollup/plugin-terser": "^0.4.4",
2424
"rollup": "^4.12.0"
2525
},
2626
"dependencies": {
27-
"bootstrap": "^5.3.3"
27+
"@swup/head-plugin": "^2.3.1",
28+
"@swup/scripts-plugin": "^2.1.0",
29+
"bootstrap": "^5.3.3",
30+
"swup": "^4.8.3"
2831
}
2932
}

0 commit comments

Comments
 (0)