Skip to content

Commit 4907349

Browse files
Medha SawhneyMedha Sawhney
authored andcommitted
adding scroll-to-bottom button
1 parent ce512fe commit 4907349

File tree

6 files changed

+97
-2
lines changed

6 files changed

+97
-2
lines changed

_config.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ baseurl: # the subpath of your site, e.g. /blog/. Leave blank for root
2020
last_updated: true # set to true if you want to display last updated in the footer
2121
impressum_path: # set to path to include impressum link in the footer, use the same path as permalink in a page, helps to conform with EU GDPR
2222
back_to_top: true # set to false to disable the back to top button
23+
scroll_to_bottom: true # set to false to disable scroll to bottom button
24+
2325

2426
# -----------------------------------------------------------------------------
2527
# Theme

_includes/header.liquid

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
<!-- Social Icons -->
2727
<div class="navbar-brand social">{% include social.liquid %}</div>
2828
{% elsif site.enable_navbar_title %}
29-
<a class="navbar-brand title font-weight-lighter" href="{{ site.baseurl }}/">
29+
<a class="navbar-brand title font-weight-lighter d-flex align-items-center justify-content-start" href="{{ site.baseurl }}/" style="margin-left: 0 !important; padding-left: 0 !important;">
3030
<img src="{{ site.baseurl }}/assets/img/logo.jpg" alt="Icon" style="height: 40px; margin-right: 2px;">
3131
<span class="font-weight-bold">
3232
{{- site.title -}}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{% if site.scroll_to_bottom %}
2+
<script src="{{ '/assets/js/vanilla-scroll-to-bottom.min.js' | relative_url | bust_file_cache }}"></script>
3+
<script>
4+
addScrollToBottom();
5+
</script>
6+
{% endif %}

_layouts/default.liquid

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
{% include scripts/imageLayouts.liquid %}
7676
{% include scripts/jekyll_tabs.liquid %}
7777
{% include scripts/back_to_top.liquid %}
78+
{% include scripts/scroll_to_bottom.liquid %}
7879
{% include scripts/search.liquid %}
7980
</body>
8081
</html>

_layouts/default_about.liquid

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171

7272
</head>
7373

74-
74+
{% include scripts/scroll_to_bottom.liquid %}
7575

7676
<!-- Body -->
7777
<body class="{% if site.navbar_fixed %}fixed-top-nav{% endif %} {% unless site.footer_fixed %}sticky-bottom-footer{% endunless %}">
@@ -130,6 +130,7 @@
130130
{% include scripts/imageLayouts.liquid %}
131131
{% include scripts/jekyll_tabs.liquid %}
132132
{% include scripts/back_to_top.liquid %}
133+
{% include scripts/scroll_to_bottom.liquid %}
133134
{% include scripts/search.liquid %}
134135
</body>
135136
</html>
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
"use strict";
2+
3+
function addScrollToBottom() {
4+
function showButton() {
5+
currentScroll() <= threshold ? hidden && (button.className = "", hidden = !1) : hidden || (button.className = "hidden", hidden = !0);
6+
}
7+
8+
function currentScroll() {
9+
return scrollContainer.scrollTop || document.documentElement.scrollTop || 0;
10+
}
11+
12+
function scrollToPosition(position) {
13+
scrollContainer.scrollTop = position;
14+
document.documentElement.scrollTop = position;
15+
}
16+
17+
function scrollToBottom() {
18+
var targetPosition = scrollContainer.scrollHeight - window.innerHeight;
19+
scrollToPosition(targetPosition);
20+
}
21+
22+
var options = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : {},
23+
backgroundColor = options.backgroundColor || "#e2e2e2", // Gray background similar to the back-to-top button
24+
cornerOffset = options.cornerOffset || 20,
25+
diameter = options.diameter || 40,
26+
ease = options.ease || function (t) { return .5 * (1 - Math.cos(Math.PI * t)); },
27+
id = options.id || "scroll-to-bottom",
28+
innerHTML = options.innerHTML || '<svg viewBox="0 0 24 24"><path d="M7.41 8.59L12 13.17l4.59-4.58L18 10l-6 6-6-6z"></path></svg>', // Down arrow SVG
29+
scrollContainer = options.scrollContainer || document.body,
30+
scrollDuration = options.scrollDuration || 100,
31+
showWhenScrollTopIs = options.showWhenScrollTopIs || 1,
32+
size = options.size || diameter,
33+
textColor = options.textColor || "#000", // Arrow color similar to back-to-top button
34+
zIndex = options.zIndex || 1;
35+
36+
var n = Math.round(.5 * size), // Adjusted for better centering
37+
i = Math.round(.25 * size), // Adjusted for better centering
38+
d = "#" + id + "{background:" + backgroundColor + ";-webkit-border-radius:50%;-moz-border-radius:50%;border-radius:50%;top:" + cornerOffset + "px;-webkit-box-shadow:0 2px 5px 0 rgba(0,0,0,.26);-moz-box-shadow:0 2px 5px 0 rgba(0,0,0,.26);box-shadow:0 2px 5px 0 rgba(0,0,0,.26);color:" + textColor + ";cursor:pointer;display:block;height:" + size + "px;opacity:1;outline:0;position:fixed;right:" + cornerOffset + "px;-webkit-tap-highlight-color:transparent;-webkit-touch-callout:none;-webkit-transition:top .2s,opacity .2s;-o-transition:top .2s,opacity .2s;-moz-transition:top .2s,opacity .2s;transition:top .2s,opacity .2s;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;width:" + size + "px;z-index:" + zIndex + "}#" + id + " svg{display:block;fill:currentColor;height:" + n + "px;margin:" + i + "px auto 0;width:" + n + "px}#" + id + ".hidden{top:-" + size + "px;opacity:0}";
39+
40+
var r = document.createElement("style");
41+
r.appendChild(document.createTextNode(d));
42+
document.head.insertAdjacentElement("afterbegin", r);
43+
44+
var button = function () {
45+
var o = document.createElement("div");
46+
return o.id = id, o.className = "hidden", o.innerHTML = innerHTML, o.addEventListener("click", function (o) {
47+
o.preventDefault();
48+
scrollToBottom();
49+
}), document.body.appendChild(o), o;
50+
}(),
51+
hidden = !0,
52+
threshold = showWhenScrollTopIs;
53+
54+
function adjustButtonPosition() {
55+
var navbarHeight = document.querySelector('.navbar').offsetHeight || 0;
56+
button.style.top = (cornerOffset + navbarHeight) + "px";
57+
}
58+
59+
adjustButtonPosition(); // Initial adjustment to ensure visibility
60+
61+
window.addEventListener("resize", adjustButtonPosition); // Adjust if window is resized
62+
(scrollContainer === document.body ? window : scrollContainer).addEventListener("scroll", showButton);
63+
showButton();
64+
}
65+
66+
window.addEventListener("load", function () {
67+
var button = document.getElementById("scroll-to-bottom"),
68+
stickyTop = document.querySelector(".sticky-top"),
69+
fixedTop = document.querySelector(".fixed-top");
70+
71+
button && window.addEventListener("scroll", function () {
72+
if (stickyTop) {
73+
var stickyTopRect = stickyTop.getBoundingClientRect();
74+
window.scrollY > stickyTopRect.height
75+
? (button.style.position = "fixed", button.style.top = stickyTopRect.height + 15 + "px")
76+
: (button.style.position = "fixed", button.style.top = "15px");
77+
}
78+
79+
if (fixedTop) {
80+
var fixedTopRect = fixedTop.getBoundingClientRect();
81+
button.style.position = "fixed";
82+
button.style.top = fixedTopRect.height + 15 + "px";
83+
}
84+
});
85+
});

0 commit comments

Comments
 (0)