Skip to content

Commit 08a839c

Browse files
Add a progress bar to show the scroll position (alshedivat#934)
This feature adds a horizontal bar under the top menu which tracks the vertical scroll position. Such a feature can be useful to represent how much is left to read on the current page more aesthetically. As this is an optional feature, `enable_progressbar` must be set to `true` in `_config.yml` to activate the functionality. I am not the original author of this code. I just made it compatible with the current version of the template at the time of this commit. The original code was most likely authored by Pankaj Parashar in this [post](https://css-tricks.com/reading-position-indicator/) made a few years before the first inclusion in an `al-folio` site. Then, the code was adapted for compatibility with the template at Anthony Plantanios' site. Finally, I did the last updates to have the code fit the new conventions used in the project. This was discussed in alshedivat#557 Co-authored-by: rohandebsarkar <rohandebsarkar@gmail.com>
1 parent 215fb34 commit 08a839c

File tree

6 files changed

+147
-2
lines changed

6 files changed

+147
-2
lines changed

_config.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ enable_navbar_social: false # enables displaying social links in the
308308
enable_project_categories: true # enables categorization of projects into
309309
# multiple categories
310310
enable_medium_zoom: true # enables image zoom feature (as on medium.com)
311-
311+
enable_progressbar: false # enables a horizontal progress bar linked to the vertical scroll position
312312

313313
# -----------------------------------------------------------------------------
314314
# Library versions

_includes/header.html

+9-1
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,12 @@
108108
</div>
109109
</div>
110110
</nav>
111-
</header>
111+
{% if site.enable_progressbar %}
112+
<!-- Scrolling Progress Bar -->
113+
<progress id="progress" value="0">
114+
<div class="progress-container">
115+
<span class="progress-bar"></span>
116+
</div>
117+
</progress>
118+
{%- endif %}
119+
</header>

_includes/scripts/progressBar.html

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
{% if site.enable_progressbar %}
2+
3+
<!-- Scrolling Progress Bar -->
4+
<script type="text/javascript">
5+
/*
6+
* This JavaScript code has been adapted from the article
7+
* https://css-tricks.com/reading-position-indicator/ authored by Pankaj Parashar,
8+
* published on the website https://css-tricks.com on the 7th of May, 2014.
9+
* Couple of changes were made to the original code to make it compatible
10+
* with the `al-foio` theme.
11+
*/
12+
const progressBar = $("#progress");
13+
/*
14+
* We set up the bar after all elements are done loading.
15+
* In some cases, if the images in the page are larger than the intended
16+
* size they'll have on the page, they'll be resized via CSS to accomodate
17+
* the desired size. This mistake, however, breaks the computations as the
18+
* scroll size is computed as soon as the elements finish loading.
19+
* To account for this, a minimal delay was introduced before computing the
20+
* values.
21+
*/
22+
window.onload = function () {
23+
setTimeout(progressBarSetup, 50);
24+
};
25+
/*
26+
* We set up the bar according to the browser.
27+
* If the browser supports the progress element we use that.
28+
* Otherwise, we resize the bar thru CSS styling
29+
*/
30+
function progressBarSetup() {
31+
if ("max" in document.createElement("progress")) {
32+
initializeProgressElement();
33+
$(document).on("scroll", function() {
34+
progressBar.attr({ value: getCurrentScrollPosition() });
35+
});
36+
$(window).on("resize", initializeProgressElement);
37+
} else {
38+
resizeProgressBar();
39+
$(document).on("scroll", resizeProgressBar);
40+
$(window).on("resize", resizeProgressBar);
41+
}
42+
}
43+
/*
44+
* The vertical scroll position is the same as the number of pixels that
45+
* are hidden from view above the scrollable area. Thus, a value > 0 is
46+
* how much the user has scrolled from the top
47+
*/
48+
function getCurrentScrollPosition() {
49+
return $(window).scrollTop();
50+
}
51+
52+
function initializeProgressElement() {
53+
let navbarHeight = $("#navbar").outerHeight(true);
54+
$("body").css({ "padding-top": navbarHeight });
55+
$("progress-container").css({ "padding-top": navbarHeight });
56+
progressBar.css({ top: navbarHeight });
57+
progressBar.attr({
58+
max: getDistanceToScroll(),
59+
value: getCurrentScrollPosition(),
60+
});
61+
}
62+
/*
63+
* The offset between the html document height and the browser viewport
64+
* height will be greater than zero if vertical scroll is possible.
65+
* This is the distance the user can scroll
66+
*/
67+
function getDistanceToScroll() {
68+
return $(document).height() - $(window).height();
69+
}
70+
71+
function resizeProgressBar() {
72+
progressBar.css({ width: getWidthPercentage() + "%" });
73+
}
74+
// The scroll ratio equals the percentage to resize the bar
75+
function getWidthPercentage() {
76+
return (getCurrentScrollPosition() / getDistanceToScroll()) * 100;
77+
}
78+
</script>
79+
80+
{%- endif %}

_layouts/default.html

+1
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,6 @@
3030
{% include scripts/misc.html %}
3131
{% include scripts/mathjax.html %}
3232
{% include scripts/analytics.html %}
33+
{% include scripts/progressBar.html %}
3334
</body>
3435
</html>

_layouts/distill.html

+1
Original file line numberDiff line numberDiff line change
@@ -105,5 +105,6 @@ <h3>Contents</h3>
105105

106106
{% include scripts/bootstrap.html %}
107107
{% include scripts/analytics.html %}
108+
{% include scripts/progressBar.html %}
108109
</body>
109110
</html>

_sass/_base.scss

+55
Original file line numberDiff line numberDiff line change
@@ -656,3 +656,58 @@ html.transition *:after {
656656
}
657657
}
658658
}
659+
660+
progress {
661+
/* Positioning */
662+
position: fixed;
663+
left: 0;
664+
top: 56px;
665+
z-index: 10;
666+
667+
/* Dimensions */
668+
width: 100%;
669+
height: 5px;
670+
671+
/* Reset the appearance */
672+
-webkit-appearance: none;
673+
-moz-appearance: none;
674+
appearance: none;
675+
676+
/* Get rid of the default border in Firefox/Opera. */
677+
border: none;
678+
679+
/* Progress bar container for Firefox/IE10 */
680+
background-color: transparent;
681+
682+
/* Progress bar value for IE10 */
683+
color: var(--global-theme-color);
684+
}
685+
686+
progress::-webkit-progress-bar {
687+
background-color: transparent;
688+
}
689+
690+
progress::-webkit-progress-value {
691+
background-color: var(--global-theme-color);
692+
}
693+
694+
progress::-moz-progress-bar {
695+
background-color: var(--global-theme-color);
696+
}
697+
698+
.progress-container {
699+
width: 100%;
700+
background-color: transparent;
701+
position: fixed;
702+
top: 56px;
703+
left: 0;
704+
height: 5px;
705+
display: block;
706+
}
707+
708+
.progress-bar {
709+
background-color: var(--global-theme-color);
710+
width: 0%;
711+
display: block;
712+
height: inherit;
713+
}

0 commit comments

Comments
 (0)