Skip to content

Commit

Permalink
fixed back to top button
Browse files Browse the repository at this point in the history
  • Loading branch information
amr-bash committed Mar 7, 2024
1 parent 7d79c3f commit 6239313
Show file tree
Hide file tree
Showing 6 changed files with 273 additions and 49 deletions.
4 changes: 3 additions & 1 deletion _includes/sidebar-right.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@

<!-- Back to Top -->

<button onclick="backToTopBtn()" id="backToTopBtn" title="Go to top" class="btn btn-primary">{{ site.data.ui-text[site.locale].back_to_top | default: 'Back to Top' }} &uarr;
<button onclick="topFunction()" id="backToTopBtn" title="Go to top">
<span class="arrow">&uarr;</span>
<span class="text">Back to Top</span>
</button>

</div>
Expand Down
43 changes: 41 additions & 2 deletions _sass/it-journey/custom.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
html, body {
max-width: 100%;
overflow-x: hidden;
}

* {
box-sizing: border-box;
}

.make-me-sticky {
position: -webkit-sticky;
position: sticky;
Expand Down Expand Up @@ -39,14 +48,44 @@
border-radius:10px;/* Rounded corners */
font-size:18px;/* Increase font size */
}


#backToTopBtn {
.text {
display: inline-block;
}

.arrow {
display: none;
}

@media (max-width: 600px) {
.text {
display: none;
}

.arrow {
display: inline-block;
}
}
}

#backToTopBtn:hover {
background-color: #555; /* Add a dark-grey background on hover */
opacity: 100%;
}

.img {
img {
display: inline-block;
max-width: 100%;
height: auto;
}

#navbar {
transition: transform 0.3s;
}

.hide-navbar {
transform: translateY(-100%);
}

// #box {
Expand Down
5 changes: 0 additions & 5 deletions _sass/it-journey/it-journey.scss
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,4 @@
// @extend .blockquote-footer;
// }

// Extends the CSS for .img-fluid to <img> tags inside a <p> tag

// p, img
// {
// @extend .img-fluid;
// }
35 changes: 8 additions & 27 deletions assets/js/back-to-top.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,24 @@
//Event listener to see if scrolling has started

window.addEventListener('scroll', function() {
var element = document.getElementById('backToTopBtn');
var height = window.innerHeight;
var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
document.addEventListener('DOMContentLoaded', (event) => {
let mybutton = document.getElementById("backToTopBtn");

if (scrollTop > height * 0.1) { // Adjust the 0.1 to control when the opacity starts changing
element.style.opacity = 0.2; // 20% opacity
element.style.display = "block"
} else {
element.style.opacity = 0; // 0% opacity
element.style.display = "none"
function topFunction() {
document.body.scrollTop = 0; // For Safari
document.documentElement.scrollTop = 0; // For Chrome, Firefox, IE and Opera
mybutton.style.opacity = "75%";
}
});

function backToTopBtn() {



let mybutton = document.getElementById("backToTopBtn");

if (mybutton) {
function topFunction() {
document.body.scrollTop = 0; // For Safari
document.documentElement.scrollTop = 0; // For Chrome, Firefox, IE and Opera
mybutton.style.opacity = "75%";
}

mybutton.onclick = topFunction;
}

// When the user scrolls down 20px from the top of the document, show the button
window.onscroll = function() {scrollFunction()};

function scrollFunction() {
if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) {
mybutton.style.display = "block";
} else {
mybutton.style.display = "none";
}
}
}
});
49 changes: 35 additions & 14 deletions pages/_notes/add-floating-back-to-top-button.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ sub-title: ""
author: ""
excerpt: ""
snippet: ""
lastmod: 2023-03-11T18:03:19.840Z
lastmod: 2024-03-07T00:01:36.787Z
---

[w3 schools](https://www.w3schools.com/howto/howto_js_scroll_to_top.asp)
Expand Down Expand Up @@ -51,6 +51,26 @@ Style the button:
font-size:18px;/* Increase font size */
}

#backToTopBtn {
.text {
display: inline-block;
}

.arrow {
display: none;
}

@media (max-width: 600px) {
.text {
display: none;
}

.arrow {
display: inline-block;
}
}
}

#backToTopBtn:hover {
background-color: #555; /* Add a dark-grey background on hover */
opacity: 100%;
Expand All @@ -62,29 +82,30 @@ Style the button:
```js
// assets/js/back-to-top.js

function backToTopBtn () {
topFunction();

document.addEventListener('DOMContentLoaded', (event) => {
let mybutton = document.getElementById("backToTopBtn");

function topFunction() {
document.body.scrollTop = 0; // For Safari
document.documentElement.scrollTop = 0; // For Chrome, Firefox, IE and Opera
mybutton.style.opacity = "75%";
}

if (mybutton) {
mybutton.onclick = topFunction;
}

// When the user scrolls down 20px from the top of the document, show the button
window.onscroll = function() {scrollFunction()};

function scrollFunction() {
if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) {
mybutton.style.display = "block";
} else {
mybutton.style.display = "none";
}
}

// When the user clicks on the button, scroll to the top of the document
mybutton.onclick = function() {topFunction()};

function topFunction() {
document.body.scrollTop = 0; // For Safari
document.documentElement.scrollTop = 0; // For Chrome, Firefox, IE and Opera
}
}
});
```

### Add the JS source to head
Expand Down
Loading

0 comments on commit 6239313

Please sign in to comment.