Skip to content

Commit

Permalink
Refactor save_scroll (#1049)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenjoezhang committed Aug 3, 2019
1 parent cb5c752 commit 97ec251
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 199 deletions.
2 changes: 1 addition & 1 deletion _config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ chat:
# Automatically scroll page to section which is under <!-- more --> mark.
scroll_to_more: true

# Automatically saving scroll position on each post / page in cookies.
# Automatically saving scroll position of each page in the browser.
save_scroll: false

# Automatically excerpt description in homepage as preamble text.
Expand Down
1 change: 0 additions & 1 deletion layout/_layout.swig
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@
{% block script_extra %}{% endblock %}

{% include '_scripts/next-boot.swig' %}
{% include '_scripts/scroll-cookie.swig' %}
{% include '_scripts/exturl.swig' %}
{% include '_scripts/three.swig' %}

Expand Down
1 change: 1 addition & 0 deletions layout/_partials/head/head.swig
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
version: '{{ version }}',
sidebar: {{ theme.sidebar | json_encode }},
back2top: {{ theme.back2top | json_encode }},
save_scroll: {{ theme.save_scroll }},
copycode: {{ theme.codeblock.copy_button | json_encode }},
fancybox: {{ theme.fancybox }},
mediumzoom: {{ theme.mediumzoom }},
Expand Down
4 changes: 0 additions & 4 deletions layout/_scripts/scroll-cookie.swig

This file was deleted.

168 changes: 0 additions & 168 deletions source/js/js.cookie.js

This file was deleted.

1 change: 1 addition & 0 deletions source/js/next-boot.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ $(document).ready(function() {
CONFIG.lazyload && window.lozad('.post-body img').observe();
CONFIG.pangu && window.pangu.spacingPage();

CONFIG.save_scroll && NexT.utils.saveScroll();
CONFIG.copycode.enable && NexT.utils.registerCopyCode();
CONFIG.back2top.enable && NexT.utils.registerBackToTop();
CONFIG.tabs && NexT.utils.registerTabsTag();
Expand Down
25 changes: 0 additions & 25 deletions source/js/scroll-cookie.js

This file was deleted.

20 changes: 20 additions & 0 deletions source/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,26 @@ NexT.utils = {
});
},

saveScroll: function() {
// Set relative link path (without domain)
var rpath = window.location.pathname;

// Read position from localStorage
if (localStorage.getItem('scroll' + rpath) !== null) {
var cvalues = localStorage.getItem('scroll' + rpath);
$(window).scrollTop(cvalues);
}

// Write position in localStorage
var timeout;
$(window).on('scroll', function() {
clearTimeout(timeout);
timeout = setTimeout(function() {
localStorage.setItem('scroll' + rpath, $(window).scrollTop());
}, 250);
});
},

/**
* One-click copy code support.
*/
Expand Down

0 comments on commit 97ec251

Please sign in to comment.