Skip to content
This repository was archived by the owner on Mar 9, 2022. It is now read-only.

Commit

Permalink
feat(button): add 'comment' fixed button (#169)
Browse files Browse the repository at this point in the history
  • Loading branch information
dillonzq authored Mar 16, 2020
1 parent 371150c commit 6e091b6
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 32 deletions.
2 changes: 1 addition & 1 deletion assets/css/_core/_base.scss
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,4 @@ a {
}
}

@import "../_partial/dynamic-to-top";
@import "../_partial/fixed-button";
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
#dynamic-to-top {
.fixed-button {
display: none;
overflow: hidden;
width: auto;
z-index: 100;
position: fixed;
bottom: 2rem;
right: 2rem;
top: auto;
left: auto;
right: 1.5rem;
font-size: 1rem;
line-height: 1.3rem;
padding: .6rem .6rem;
Expand Down Expand Up @@ -35,3 +34,11 @@
}
}
}

#top-button {
bottom: 1.5rem;
}

#comment-button {
bottom: 4.5rem;
}
2 changes: 1 addition & 1 deletion assets/js/theme.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion assets/js/theme.min.js.map

Large diffs are not rendered by default.

10 changes: 6 additions & 4 deletions layouts/_default/baseof.html
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,12 @@
{{- partial "footer.html" . -}}
</div>

{{- /* Dynamic to top button */ -}}
<a href="#" class="dynamic-to-top animated faster" id="dynamic-to-top">
<i class="fas fa-arrow-up fa-fw"></i>
</a>
<div id="fixed-btn-container">
{{- /* top button */ -}}
<a href="#" id="top-button" class="fixed-button animated faster">
<i class="fas fa-arrow-up fa-fw"></i>
</a>
</div>

{{- /* Load JavaScript scripts and CSS */ -}}
{{- partial "assets.html" . -}}
Expand Down
8 changes: 4 additions & 4 deletions layouts/partials/comment.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

{{- /* Disqus Comment System */ -}}
{{- if .Site.Params.comment.disqus.enable -}}
<div id="disqus_thread"></div>
<div id="disqus_thread" class="comment"></div>
{{- $script := printf `<script defer src="https://%s.disqus.com/embed.js"></script>` .Site.Params.comment.disqus.shortname -}}
{{- slice $script | $scratch.Add "scriptCDN" -}}
<noscript>
Expand All @@ -16,7 +16,7 @@
{{- /* Gitalk Comment System */ -}}
{{- if .Site.Params.comment.gitalk.enable -}}
{{- $gitalk := .Site.Params.comment.gitalk -}}
<div id="gitalk"></div>
<div id="gitalk" class="comment"></div>
{{- with $CDN.gitalkCSS -}}
{{- slice . | $scratch.Add "linkCDN" -}}
{{- else -}}
Expand Down Expand Up @@ -50,7 +50,7 @@
{{- /* Valine Comment System */ -}}
{{- if .Site.Params.comment.valine.enable -}}
{{- $valine := .Site.Params.comment.valine -}}
<div id="valine"></div>
<div id="valine" class="comment"></div>
{{- slice "lib/valine/valine.scss" | $scratch.Add "linkLocal" -}}
{{- with $CDN.valineJS -}}
{{- slice . | $scratch.Add "scriptCDN" -}}
Expand Down Expand Up @@ -99,7 +99,7 @@
{{- /* Facebook Comment System */ -}}
{{- if .Site.Params.comment.facebook.enable -}}
{{- $facebook := .Site.Params.comment.facebook -}}
<div id="fb-root"></div>
<div id="fb-root" class="comment"></div>
<div
class="fb-comments"
data-href="{{ .Site.Params.baseURL }}{{ .Permalink | absURL }}"
Expand Down
8 changes: 3 additions & 5 deletions layouts/posts/single.html
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,8 @@ <h2 class="toc-title">{{ T "contents" }}</h2>
{{- partial "single/footer.html" . -}}

{{- /* Comment */ -}}
<div class="comment">
{{- if ( .Params.comment | default true ) -}}
{{- partial "comment.html" . -}}
{{- end -}}
</div>
{{- if ( .Params.comment | default true ) -}}
{{- partial "comment.html" . -}}
{{- end -}}
</article>
{{- end -}}

Large diffs are not rendered by default.

36 changes: 24 additions & 12 deletions src/js/theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,17 @@
header.classList.add('animated');
header.classList.add('faster');
});
const toTopButton = document.getElementById('dynamic-to-top');
const comments = document.getElementsByClassName('comment') || [];
if (comments.length) {
const container = document.getElementById('fixed-btn-container');
const button = document.createElement('a');
button.href = `#${comments[0].id}`;
button.id = 'comment-button';
button.className = 'fixed-button animated faster';
button.innerHTML = '<i class="fas fa-comment fa-fw"></i>';
container.appendChild(button);
}
const buttons = document.getElementsByClassName('fixed-button');
const MIN_SCROLL = 10;
window.addEventListener('scroll', () => {
this.newScrollTop = this.util.getScrollTop();
Expand All @@ -262,18 +272,20 @@
header.classList.add('fadeInDown');
}
});
if (this.newScrollTop > 200) {
if (scroll > MIN_SCROLL) {
toTopButton.classList.remove('fadeIn');
toTopButton.classList.add('fadeOut');
} else if (scroll < - MIN_SCROLL) {
toTopButton.style.display = 'block';
toTopButton.classList.remove('fadeOut');
toTopButton.classList.add('fadeIn');
this.util.forEach(buttons, (button) => {
if (this.newScrollTop > 20) {
if (scroll > MIN_SCROLL) {
button.classList.remove('fadeIn');
button.classList.add('fadeOut');
} else if (scroll < - MIN_SCROLL) {
button.style.display = 'block';
button.classList.remove('fadeOut');
button.classList.add('fadeIn');
}
} else {
button.style.display = 'none';
}
} else {
toTopButton.style.display = 'none';
}
});
if (!this._scrollTimeout) {
this._scrollTimeout = window.setTimeout(() => {
this._scrollTimeout = null;
Expand Down

0 comments on commit 6e091b6

Please sign in to comment.