Skip to content

Commit

Permalink
feat: autohide back-to-top toggle
Browse files Browse the repository at this point in the history
  • Loading branch information
reuixiy committed Dec 30, 2019
1 parent a4e4137 commit 6659f25
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 4 deletions.
10 changes: 10 additions & 0 deletions assets/js/back-to-top.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
backToTop();

function backToTop() {
var backToTop = document.getElementById('back-to-top');
if (backToTop !== null) {
window.addEventListener('scroll', function () {
window.scrollY > 100 ? backToTop.classList.add('show') : backToTop.classList.remove('show');
});
}
}
15 changes: 14 additions & 1 deletion assets/scss/_common/back-to-top.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,20 @@
font-size: 90%;
color: var(--color-contrast-medium);
position: fixed;
bottom: 1em;
right: 1em;
z-index: 1;
}

@if ($enableBackToTopAutoHide) {
.back-to-top {
bottom: -2em;
transition: bottom 0.3s ease-in-out;
&.show {
bottom: 1em;
}
}
} @else {
.back-to-top {
bottom: 1em;
}
}
6 changes: 6 additions & 0 deletions assets/scss/main.scss
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
$listWidth: {{ . }}em;
{{ end }}

{{ if .Site.Params.enableBackToTopAutoHide }}
$enableBackToTopAutoHide: true;
{{ else }}
$enableBackToTopAutoHide: false;
{{ end }}

{{ if .Site.Params.enableChinesePunctuationGlyphCorrection }}
{{ .Scratch.Set "glyph" "'glyph-correction', " }}
$glyph: true;
Expand Down
2 changes: 2 additions & 0 deletions config-examples/en-us/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -1082,6 +1082,8 @@ uglyURLs = false

enableBackToTop = true

enableBackToTopAutoHide = true

displayBackToTopInHome = true

backToTopIcon = "arrow-up"
Expand Down
3 changes: 3 additions & 0 deletions config-examples/zh-cn/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -1045,6 +1045,9 @@ uglyURLs = false
# 是否开启
enableBackToTop = true

# 是否开启自动隐藏
enableBackToTopAutoHide = true

# 是否在首页显示
displayBackToTopInHome = true

Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"Target":"css/meme.min.1a62b125ec50989ec305a5ba8a826c7705be1471ede622b2b26d2f8dcc536843.css","MediaType":"text/css","Data":{"Integrity":"sha256-GmKxJexQmJ7DBaW6ioJsdwW+FHHt5iKysm0vjcxTaEM="}}
{"Target":"css/meme.min.fab06b4f1edf8acfa72af56b03bef62f13de7a260e3750467664d4b093b1126f.css","MediaType":"text/css","Data":{"Integrity":"sha256-+rBrTx7fis+nKvVrA772LxPeeiYON1BGdmTUsJOxEm8="}}
2 changes: 1 addition & 1 deletion layouts/partials/back-to-top.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{{ if or (and .IsHome .Site.Params.displayBackToTopInHome) (and (not .IsHome) .Site.Params.enableBackToTop) }}
<a href="#" class="back-to-top">{{ index .Site.Data.SVG .Site.Params.backToTopIcon | safeHTML }}</a>
<a href="#" id="back-to-top" class="back-to-top">{{ index .Site.Data.SVG .Site.Params.backToTopIcon | safeHTML }}</a>
{{ end }}
5 changes: 5 additions & 0 deletions layouts/partials/script.html
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
{{- $main := resources.Get "js/main.js" | resources.ExecuteAsTemplate "js/main-rendered.js" . -}}
{{- $backToTop := resources.Get "js/back-to-top.js" | resources.ExecuteAsTemplate "js/back-to-top-rendered.js" . -}}
{{- $darkMode := resources.Get "js/dark-mode.js" | resources.ExecuteAsTemplate "js/dark-mode-rendered.js" . -}}
{{- $lang := resources.Get "js/multilingual.js" | resources.ExecuteAsTemplate "js/multilingual-rendered.js" . -}}
{{- $instantClick := resources.Get "js/instantclick.js" | resources.ExecuteAsTemplate "js/instantclick-rendered.js" . -}}
{{- $custom := resources.Get "js/custom.js" | resources.ExecuteAsTemplate "js/custom-rendered.js" . -}}

{{- .Scratch.Set "script" (slice $main) -}}

{{- if .Site.Params.enableBackToTopAutoHide -}}
{{- .Scratch.Add "script" (slice $backToTop) -}}
{{- end -}}

{{- if .Site.Params.enableDarkMode -}}
{{- .Scratch.Add "script" (slice $darkMode) -}}
{{- end -}}
Expand Down
8 changes: 8 additions & 0 deletions layouts/partials/third-party/instantclick.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
<script data-no-instant>InstantClick.init();</script>

{{ if .Site.Params.enableBackToTopAutoHide }}
<script data-no-instant>
InstantClick.on('change', function () {
backToTop();
});
</script>
{{ end }}

{{ if .Site.Params.enableDarkMode }}
<script data-no-instant>
InstantClick.on('change', function () {
Expand Down

0 comments on commit 6659f25

Please sign in to comment.