mirror of
https://github.com/CaiJimmy/hugo-theme-stack.git
synced 2025-04-29 03:53:30 +08:00
feat(sidebar): sticky sidebar using vursen/FloatSidebar.js
This commit is contained in:
parent
8242a0bafc
commit
e341e089b7
@ -74,4 +74,4 @@ Your support is greatly appreciated :)
|
|||||||
- [MunifTanjim/minimo](https://github.com/MunifTanjim/minimo)
|
- [MunifTanjim/minimo](https://github.com/MunifTanjim/minimo)
|
||||||
- [lepture/yue.css](https://github.com/lepture/yue.css)
|
- [lepture/yue.css](https://github.com/lepture/yue.css)
|
||||||
- Markdown gallery syntax from [Typlog](https://typlog.com/)
|
- Markdown gallery syntax from [Typlog](https://typlog.com/)
|
||||||
- [somewebmedia/hc-sticky](https://github.com/somewebmedia/hc-sticky)
|
- [vursen/FloatSidebar.js](https://github.com/vursen/FloatSidebar.js)
|
||||||
|
@ -39,6 +39,10 @@
|
|||||||
.right-sidebar {
|
.right-sidebar {
|
||||||
flex-shrink: 0;
|
flex-shrink: 0;
|
||||||
display: none;
|
display: none;
|
||||||
|
|
||||||
|
/// Take full max-width, set in grid.scss
|
||||||
|
width: 100%;
|
||||||
|
position: relative;
|
||||||
|
|
||||||
&.sticky {
|
&.sticky {
|
||||||
top: 0;
|
top: 0;
|
||||||
@ -49,11 +53,6 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// For HC Sticky plugin, since it doesn't provide a option to customize class name
|
|
||||||
.sticky-spacer {
|
|
||||||
max-width: var(--right-sidebar-max-width);
|
|
||||||
}
|
|
||||||
|
|
||||||
.site-info {
|
.site-info {
|
||||||
z-index: 1;
|
z-index: 1;
|
||||||
transition: box-shadow 0.5s ease;
|
transition: box-shadow 0.5s ease;
|
||||||
|
@ -37,8 +37,8 @@ KaTeX:
|
|||||||
type: script
|
type: script
|
||||||
defer: true
|
defer: true
|
||||||
|
|
||||||
HCSticky:
|
FloatSidebar:
|
||||||
- src: https://cdn.jsdelivr.net/npm/hc-sticky@2.2.7/dist/hc-sticky.js
|
- src: https://cdn.jsdelivr.net/npm/float-sidebar@1.2.3/dist/float-sidebar.min.js
|
||||||
integrity: sha256-ZQb/nC8ArgZEjGauwabpmi57wvKmjsuXZbFcIKw4G/Y=
|
integrity: sha256-mOZMic3UnZLVOJXhVOhJ2zGsyrGHEQ3LVT5O98ya9kI=
|
||||||
type: script
|
type: script
|
||||||
defer: true
|
defer: true
|
||||||
|
@ -42,16 +42,18 @@
|
|||||||
{{ define "right-sidebar" }}
|
{{ define "right-sidebar" }}
|
||||||
{{ if (.Scratch.Get "hasTOC") }}
|
{{ if (.Scratch.Get "hasTOC") }}
|
||||||
<aside class="sidebar right-sidebar sticky">
|
<aside class="sidebar right-sidebar sticky">
|
||||||
<section class="widget archives">
|
<div class="sidebar__inner">
|
||||||
<div class="widget-icon">
|
<section class="widget">
|
||||||
{{ partial "helper/icon" "hash" }}
|
<div class="widget-icon">
|
||||||
</div>
|
{{ partial "helper/icon" "hash" }}
|
||||||
<h2 class="widget-title section-title">{{ T "article.tableOfContents" }}</h2>
|
</div>
|
||||||
|
<h2 class="widget-title section-title">{{ T "article.tableOfContents" }}</h2>
|
||||||
<div class="widget--toc">
|
|
||||||
{{ .TableOfContents }}
|
<div class="widget--toc">
|
||||||
</div>
|
{{ .TableOfContents }}
|
||||||
</section>
|
</div>
|
||||||
|
</section>
|
||||||
|
</div>
|
||||||
</aside>
|
</aside>
|
||||||
{{ end }}
|
{{ end }}
|
||||||
{{ end }}
|
{{ end }}
|
@ -1,17 +1,5 @@
|
|||||||
{{- partial "helper/external" (dict "Context" . "Namespace" "Vibrant") -}}
|
{{- partial "helper/external" (dict "Context" . "Namespace" "Vibrant") -}}
|
||||||
|
{{- partial "footer/components/sticky-sidebar" . -}}}
|
||||||
{{- partial "helper/external" (dict "Context" . "Namespace" "HCSticky") -}}
|
|
||||||
<script>
|
|
||||||
window.addEventListener("load", () => {
|
|
||||||
const rightSiderbar = document.querySelector(".right-sidebar");
|
|
||||||
if (rightSiderbar) {
|
|
||||||
new hcSticky(".right-sidebar", {
|
|
||||||
stickTo: '.main-container',
|
|
||||||
stickyClass: 'js-sticked'
|
|
||||||
});
|
|
||||||
}
|
|
||||||
})
|
|
||||||
</script>
|
|
||||||
|
|
||||||
{{- $opts := dict "minify" hugo.IsProduction -}}
|
{{- $opts := dict "minify" hugo.IsProduction -}}
|
||||||
{{- $script := resources.Get "ts/main.ts" | js.Build $opts -}}
|
{{- $script := resources.Get "ts/main.ts" | js.Build $opts -}}
|
||||||
|
31
layouts/partials/footer/components/sticky-sidebar.html
Normal file
31
layouts/partials/footer/components/sticky-sidebar.html
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
{{- partial "helper/external" (dict "Context" . "Namespace" "FloatSidebar") -}}
|
||||||
|
|
||||||
|
<script>
|
||||||
|
window.addEventListener("load", () => {
|
||||||
|
const rightSiderbar = document.querySelector(".right-sidebar");
|
||||||
|
const mainContainer = document.querySelector(".main-container");
|
||||||
|
const rightSidebarInner = rightSiderbar.querySelector(".sidebar__inner");
|
||||||
|
const padding = window.getComputedStyle(document.documentElement).getPropertyValue('--main-top-padding').replace('px', '');
|
||||||
|
|
||||||
|
if (rightSiderbar) {
|
||||||
|
const floatSidebar = FloatSidebar({
|
||||||
|
sidebar: rightSiderbar,
|
||||||
|
relative: mainContainer,
|
||||||
|
sidebarInner: rightSidebarInner,
|
||||||
|
topSpacing: padding,
|
||||||
|
bottomSpacing: padding
|
||||||
|
});
|
||||||
|
|
||||||
|
const updateMaxWidth = () => {
|
||||||
|
rightSidebarInner.style.maxWidth = `${rightSiderbar.offsetWidth}px`;
|
||||||
|
}
|
||||||
|
updateMaxWidth();
|
||||||
|
|
||||||
|
/**
|
||||||
|
Because the right sidebar does not have a fixed width,
|
||||||
|
we need to update the max-width of the sidebarInner when the window is resized.
|
||||||
|
*/
|
||||||
|
window.addEventListener("resize", updateMaxWidth);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
</script>
|
@ -1,8 +1,10 @@
|
|||||||
{{ if .Site.Params.widgets.enabled }}
|
{{ if .Site.Params.widgets.enabled }}
|
||||||
{{ $context := . }}
|
{{ $context := . }}
|
||||||
<aside class="sidebar right-sidebar sticky">
|
<aside class="sidebar right-sidebar">
|
||||||
{{ range $widget := .Site.Params.widgets.enabled }}
|
<div class="sidebar__inner">
|
||||||
{{ partial (printf "widget/%s" $widget) $context }}
|
{{ range $widget := .Site.Params.widgets.enabled }}
|
||||||
{{ end }}
|
{{ partial (printf "widget/%s" $widget) $context }}
|
||||||
|
{{ end }}
|
||||||
|
</div>
|
||||||
</aside>
|
</aside>
|
||||||
{{ end }}
|
{{ end }}
|
Loading…
Reference in New Issue
Block a user