Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the nav bar of tabs with long content stick to the top #361

Merged
merged 4 commits into from
Sep 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions _config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -796,6 +796,8 @@ note:

# Tabs tag
tabs:
# Make the nav bar of tabs with long content stick to the top.
sticky: false
transition:
tabs: false
labels: true
Expand Down
1 change: 1 addition & 0 deletions scripts/helpers/next-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ hexo.extend.helper.register('next_config', function() {
lazyload : theme.lazyload,
pangu : theme.pangu,
comments : theme.comments,
stickytabs: theme.tabs.sticky,
motion : theme.motion,
prism : config.prismjs.enable && !config.prismjs.preprocess,
i18n : {
Expand Down
4 changes: 4 additions & 0 deletions source/css/_common/scaffolding/tags/tabs.styl
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@
padding-top: 10px;

ul.nav-tabs {
background: (($scheme == 'Muse') or ($scheme == 'Mist')) ? var(--body-bg-color) : var(--content-bg-color);
display: flex;
flex-wrap: wrap;
margin: 0;
padding: 0;
position: -webkit-sticky;
position: sticky;
top: 0;

+mobile-smallest() {
display: block;
Expand Down
11 changes: 10 additions & 1 deletion source/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,9 @@ NexT.utils = {
event.preventDefault();
// Prevent selected tab to select again.
if (element.classList.contains('active')) return;
const nav = element.parentNode;
// Add & Remove active class on `nav-tabs` & `tab-content`.
[...element.parentNode.children].forEach(target => {
[...nav.children].forEach(target => {
target.classList.toggle('active', target === element);
});
// https://stackoverflow.com/questions/20306204/using-queryselector-with-ids-that-are-numbers
Expand All @@ -189,6 +190,14 @@ NexT.utils = {
tActive.dispatchEvent(new Event('tabs:click', {
bubbles: true
}));
if (!CONFIG.stickytabs) return;
const offset = nav.parentNode.getBoundingClientRect().top + window.scrollY + 10;
window.anime({
targets : document.scrollingElement,
duration : 500,
easing : 'linear',
scrollTop: offset
});
});
});

Expand Down