Skip to content

Commit

Permalink
feat(widget): 允许侧边栏卡片的粘性定位
Browse files Browse the repository at this point in the history
添加 sticky: true 设定固定
  • Loading branch information
inkss committed Jun 10, 2022
1 parent 2e2e05d commit 5d5c9d5
Show file tree
Hide file tree
Showing 19 changed files with 44 additions and 27 deletions.
1 change: 1 addition & 0 deletions _config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,7 @@ sidebar:
toc:
class: toc
display: [desktop, mobile] # [desktop, mobile]
sticky: true
header:
icon: fa-solid fa-list
title: 本文目录
Expand Down
11 changes: 3 additions & 8 deletions layout/_widget/_pre.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,15 @@ if (item.display) {
} else {
pf = ['desktop', 'mobile'];
}
if (item.sticky == true) {
page.widget_style = 'sticky '
} else {
page.widget_style = ''
}
if (where == 'sidebar') {
page.widget_style += theme.custom_css.sidebar.effect.join(' ');
} else if (where == 'body') {
page.widget_style += theme.custom_css.body.effect.join(' ');
}
page.widget_platform = pf.join(' ');
if (item.pjaxReload || item.pjaxReload===undefined){
page.widget_pjax = "pjax"
if (item.sticky !== true && (item.pjaxReload || item.pjaxReload===undefined)){
page.widget_pjax = "pjax";
}else{
page.widget_pjax = ""
}
%>
%>
2 changes: 1 addition & 1 deletion layout/_widget/blogger.ejs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<%- partial('_pre') %>
<section class="widget <%- item.class %> <%- page.widget_style %> <%- page.widget_platform %> <%- page.widget_pjax %>">
<section class="widget <%- item.class %> <%- page.widget_platform %> <%- page.widget_pjax %>">
<div class='content'>
<% if (item.avatar) { %>
<% if (item.url) { %>
Expand Down
2 changes: 1 addition & 1 deletion layout/_widget/category.ejs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<% if (site.categories.length){ %>
<%- partial('_pre') %>
<section class="widget <%- item.class %> <%- page.widget_style %> <%- page.widget_platform %> <%- page.widget_pjax %>">
<section class="widget <%- item.class %> <%- page.widget_platform %> <%- page.widget_pjax %>">
<%- partial('header', {item: item}) %>
<div class='content'>
<ul class="entry navigation">
Expand Down
2 changes: 1 addition & 1 deletion layout/_widget/copyright.ejs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<%- partial('_pre') %>
<section class="widget <%- item.class %> <%- page.widget_style %> <%- page.widget_platform %> <%- page.widget_pjax %>">
<section class="widget <%- item.class %> <%- page.widget_platform %> <%- page.widget_pjax %>">
<div class='content'>
<% if (item.blockquote == true) { %>
<blockquote>
Expand Down
2 changes: 1 addition & 1 deletion layout/_widget/grid.ejs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<%- partial('_pre') %>
<section class="widget <%- item.class %> <%- page.widget_style %> <%- page.widget_platform %> <%- page.widget_pjax %>">
<section class="widget <%- item.class %> <%- page.widget_platform %> <%- page.widget_pjax %>">
<%- partial('header', {item: item}) %>
<div class='content'>
<div class="grid navigation<%- item.fixed ? ' fixed' : '' %>">
Expand Down
2 changes: 1 addition & 1 deletion layout/_widget/lastupdate.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function genList() {
}
%>
<%- partial('_pre') %>
<section class="widget <%- page.widget_style %> <%- page.widget_platform %> <%- page.widget_pjax %>">
<section class="widget <%- page.widget_platform %> <%- page.widget_pjax %>">
<%- partial('header', {item: item}) %>
<div class='content'>
<%- genList() %>
Expand Down
2 changes: 1 addition & 1 deletion layout/_widget/list.ejs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<%- partial('_pre') %>
<section class="widget <%- item.class == 'list' ? item.class : 'list ' + item.class %> <%- page.widget_style %> <%- page.widget_platform %> <%- page.widget_pjax %>">
<section class="widget <%- item.class == 'list' ? item.class : 'list ' + item.class %> <%- page.widget_platform %> <%- page.widget_pjax %>">
<%- partial('header', {item: item}) %>
<div class='content'>
<ul class="list entry navigation">
Expand Down
23 changes: 20 additions & 3 deletions layout/_widget/load.ejs
Original file line number Diff line number Diff line change
@@ -1,15 +1,32 @@
<%
let widget_library_temp = [];
%>
<% (widgets||[]).forEach(function(widget){ %>
<% if (theme.sidebar.widget_library && (widget in theme.sidebar.widget_library)){ %>
<% let w = theme.sidebar.widget_library[widget]; w.id = widget; %>
<%- partial(w.class, {item: w, where: where}) %>
<% if(w.sticky) { %>
<% widget_library_temp.push(w) %>
<% } else { %>
<%- partial(w.class, {item: w, where: where}) %>
<% } %>
<% } %>
<% }) %>

<div class="widget-sticky pjax">
<% if(widget_library_temp.length !== 0) { %>
<% widget_library_temp.forEach( w => { %>
<%- partial(w.class, {item: w, where: where}) %>
<% }) %>
<% } %>
</div>

<%
let widget_pjax_length = 0;
page.sidebar.forEach(function(e){
if (e&&theme.sidebar.widget_library&&theme.sidebar.widget_library[e]) {
let widget_pjax_flag = theme.sidebar.widget_library[e].pjaxReload
if (widget_pjax_flag || widget_pjax_flag===undefined) {
let widget_stickys = theme.sidebar.widget_library[e].sticky
if (widget_stickys !== true && (widget_pjax_flag || widget_pjax_flag===undefined)) {
widget_pjax_length ++;
}
}
Expand All @@ -21,4 +38,4 @@ let widget_pjax_place_length = Object.keys(theme.sidebar.widget_library).length
<div class="pjax">
<!-- pjax占位 -->
</div>
<% } %>
<% } %>
2 changes: 1 addition & 1 deletion layout/_widget/music.ejs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<%- partial('_pre') %>
<% if (theme.plugins.aplayer.enable == true) { %>
<section class='widget <%- page.widget_style %> <%- page.widget_platform %> <%- page.widget_pjax %> <%- item.class %>'>
<section class='widget <%- page.widget_platform %> <%- page.widget_pjax %> <%- item.class %>'>
<%- partial('header', {item: item}) %>
<div class='content'>
<%- partial('../_plugins/aplayer/layout', {post: null, where: 'widget'}) %>
Expand Down
2 changes: 1 addition & 1 deletion layout/_widget/page.ejs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<%- partial('_pre') %>
<section class="widget <%- item.class %> <%- page.widget_style %> <%- page.widget_platform %> <%- page.widget_pjax %>">
<section class="widget <%- item.class %> <%- page.widget_platform %> <%- page.widget_pjax %>">
<%- partial('header', {item: item}) %>
<div class='content'>
<% site.pages.forEach(function(post){ %>
Expand Down
2 changes: 1 addition & 1 deletion layout/_widget/qrcode.ejs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<%- partial('_pre') %>
<section class="widget <%- item.class %> <%- page.widget_style %> <%- page.widget_platform %> <%- page.widget_pjax %>">
<section class="widget <%- item.class %> <%- page.widget_platform %> <%- page.widget_pjax %>">
<%- partial('header', {item: item}) %>
<div class='content article-entry'>
<% (item.images||[]).forEach(function(url){ %>
Expand Down
2 changes: 1 addition & 1 deletion layout/_widget/related_posts.ejs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<% if(page.layout == 'post' && page.content && page.path != undefined){ %>
<%- partial('_pre') %>
<section class="widget <%- item.class %> <%- page.widget_style %> <%- page.widget_platform %> <%- page.widget_pjax %>">
<section class="widget <%- item.class %> <%- page.widget_platform %> <%- page.widget_pjax %>">
<%- partial('header', {item: item}) %>
<div class="content">
<%- popular_posts( { maxCount: item.max_count , ulClass: 'popular-posts' , PPMixingRate: 0.2 } , page ) %>
Expand Down
2 changes: 1 addition & 1 deletion layout/_widget/tagcloud.ejs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<% if (site.tags.length){ %>
<%- partial('_pre') %>
<section class="widget <%- item.class %> <%- page.widget_style %> <%- page.widget_platform %> <%- page.widget_pjax %>">
<section class="widget <%- item.class %> <%- page.widget_platform %> <%- page.widget_pjax %>">
<%- partial('header', {item: item}) %>
<div class='content'>
<%- tagcloud(site.tags, {
Expand Down
2 changes: 1 addition & 1 deletion layout/_widget/text.ejs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<%- partial('_pre') %>
<section class="widget <%- item.class %> <%- page.widget_style %> <%- page.widget_platform %> <%- page.widget_pjax %>">
<section class="widget <%- item.class %> <%- page.widget_platform %> <%- page.widget_pjax %>">
<%- partial('header', {item: item}) %>
<div class='content'>
<% (item.content||[]).forEach(function(row){ %>
Expand Down
2 changes: 1 addition & 1 deletion layout/_widget/toc.ejs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<%- partial('_pre') %>
<% var realcontent = (page.encrypt == true) ? page.origin : page.content; %>
<% if (realcontent && page.toc != false && toc(realcontent).length > 0) { %>
<section class="widget toc-wrapper <%- page.widget_style %> <%- page.widget_platform %> <%- page.widget_pjax %>" id="toc-div" <% if (page.encrypt == true) { %>style="display:none" <% } %>>
<section class="widget toc-wrapper <%- page.widget_platform %> <%- page.widget_pjax %>" id="toc-div" <% if (page.encrypt == true) { %>style="display:none" <% } %>>
<%- partial('header', {item: item}) %>
<div class='content'>
<%- toc(realcontent, {list_number: item.list_number, min_depth: item.min_depth, max_depth: item.max_depth}) %>
Expand Down
2 changes: 1 addition & 1 deletion layout/_widget/webinfo.ejs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<%- partial('_pre') %>
<section class="widget <%- page.widget_style %> <%- page.widget_platform %> <%- page.widget_pjax %>">
<section class="widget <%- page.widget_platform %> <%- page.widget_pjax %>">
<%- partial('header', {item: item}) %>
<div class='content'>
<div class="webinfo">
Expand Down
4 changes: 4 additions & 0 deletions source/css/_style/_layout/sidebar.styl
Original file line number Diff line number Diff line change
Expand Up @@ -358,3 +358,7 @@
white-space: nowrap // 不换行
overflow: hidden // 超出宽度部分隐藏
text-overflow: ellipsis // 超出部分以点号替代

.widget-sticky
position: sticky
top: $navbar-height + $gap
4 changes: 2 additions & 2 deletions source/css/_style/_layout/toc.styl
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
z-index: 1
overflow: hidden
border-radius: $border-card
position: sticky
top: $navbar-height + $gap
//position: sticky
//top: $navbar-height + $gap
trans()
line-height: 1.6
header
Expand Down

10 comments on commit 5d5c9d5

@MHuiG
Copy link
Member

@MHuiG MHuiG commented on 5d5c9d5 Jun 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@inkss
Copy link
Member Author

@inkss inkss commented on 5d5c9d5 Jun 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

设置 sticky true 的单独放在一块儿处理了,位置放在未设置的后面


跳转的时候,是按照 .pjax 这个填充的吧,固定的渲染被排除

@MHuiG
Copy link
Member

@MHuiG MHuiG commented on 5d5c9d5 Jun 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我好像刷到缓存了

@MHuiG
Copy link
Member

@MHuiG MHuiG commented on 5d5c9d5 Jun 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

@MHuiG
Copy link
Member

@MHuiG MHuiG commented on 5d5c9d5 Jun 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

是 pjax 的问题

@inkss
Copy link
Member Author

@inkss inkss commented on 5d5c9d5 Jun 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我是这样改的:把设置 sticky 的放在一个div里,对这个div加了.pjax的class,里面的侧边栏就没加。但是pjax下,填充的行为导致了顺序的差异吧

@inkss
Copy link
Member Author

@inkss inkss commented on 5d5c9d5 Jun 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

从文章页跳到首页,音乐就被放到了最上面

@MHuiG
Copy link
Member

@MHuiG MHuiG commented on 5d5c9d5 Jun 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

music 会把 toc 挡住
可以调一下 z-index

@inkss
Copy link
Member Author

@inkss inkss commented on 5d5c9d5 Jun 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这样?

.widget-sticky
  z-index 10

@MHuiG
Copy link
Member

@MHuiG MHuiG commented on 5d5c9d5 Jun 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

应该可以
image

image

Please sign in to comment.