Skip to content
Closed
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
3 changes: 2 additions & 1 deletion config/i18n/locales/english/translations.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"buttons": {
"forum": "Forum",
"donate": "Donate",
"load-more-articles": "Load More Articles"
"load-more-articles": "Load More Articles",
"scroll-to-top": "Scroll to Top"
},
"search": {
"label": "Search",
Expand Down
12 changes: 11 additions & 1 deletion cypress/e2e/english/landing/landing.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ const selectors = {
avatar: "[data-test-label='avatar']",
siteNavLogo: "[data-test-label='site-nav-logo']",
postPublishedTime: "[data-test-label='post-published-time']",
banner: "[data-test-label='banner']"
banner: "[data-test-label='banner']",
scrollButton: "[data-test-label='scroll-to-top']"
};

describe('Landing (Hashnode sourced)', () => {
Expand Down Expand Up @@ -55,6 +56,15 @@ describe('Landing (Hashnode sourced)', () => {
.should('equal', 'https://www.freecodecamp.org/');
});

it('the scroll button should be initially hidden', function () {
cy.get(selectors.scrollButton).should('be.hidden');
});

it('the scroll button should scroll into view', function () {
cy.scrollTo('bottom');
cy.get(selectors.scrollButton).should('be.visible');
});

it('the authenticated banner text should tell people to donate', function () {
cy.setCookie('jwt_access_token', 'dadadsdadsadasd');
cy.visit('/');
Expand Down
20 changes: 20 additions & 0 deletions src/_includes/assets/css/screen.css
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,26 @@ a.nav-forum {
}
}

#scrollToTop {
display: none;
position: fixed;
bottom: 20px;
right: 30px;
z-index: 99;
border: none;
outline: none;
color: var(--gray00);
background-color: var(--dark-blue);
cursor: pointer;
padding: 15px;
border-radius: 10px;
font-size: 18px;
}

#scrollToTop:hover {
background-color: var(--gray75);
}

/* 6. Post Feed
/* ---------------------------------------------------------- */

Expand Down
22 changes: 22 additions & 0 deletions src/_includes/assets/js/scroll-button.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Get the button:
let goTopButton = document.getElementById('scrollToTop');

window.onscroll = function () {
scrollFunction();
};

function scrollFunction() {
if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) {
// @ts-ignore
goTopButton.style.display = 'block';
} else {
// @ts-ignore
goTopButton.style.display = 'none';
}
}

// eslint-disable-next-line no-unused-vars
function goToTop() {
document.body.scrollTop = 0;
document.documentElement.scrollTop = 0;
}
4 changes: 3 additions & 1 deletion src/_includes/layouts/default.njk
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
{% if secrets.eleventyEnv === 'prod' %}
{% include "partials/gtm-head.njk" %}
{% endif %}

{# Dynamically load MathJax script #}
{% block mathjax %}
{% endblock %}
Expand Down Expand Up @@ -138,6 +138,8 @@
<body class="home-template">
{% endif %}

{% include "partials/scroll-button.njk" %}

{% if secrets.eleventyEnv === 'prod' %}
{% include "partials/gtm-body.njk" %}
{% endif %}
Expand Down
8 changes: 8 additions & 0 deletions src/_includes/partials/scroll-button.njk
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<button onclick="goToTop()" id="scrollToTop" title="Go to top" data-test-label="scroll-to-top">{% t 'buttons.scroll-to-top' %}</button>

{% block scripts %}
{% set js %}
{% include "assets/js/scroll-button.js" %}
{% endset %}
<script>{{ js | jsMin | safe }}</script>
{% endblock %}
Loading