Skip to content

Commit

Permalink
Scrolling and fixing current page
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon-Rey committed Mar 6, 2024
1 parent 07eff15 commit 748e0da
Show file tree
Hide file tree
Showing 20 changed files with 192 additions and 133 deletions.
117 changes: 0 additions & 117 deletions docs/_includes/gallery.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,6 @@
</div>

<script>
const modal = document.getElementById('modal');
const modalImg = document.getElementById('modal-image');
const modalControls = document.querySelectorAll('.modal-control');
const modalPrev = document.getElementById('modal-prev');
const modalNext = document.getElementById('modal-next');
const modalClose = document.getElementById('modal-close');
const modalDetails = document.getElementById('modal-details-box');
const galleryImages = document.querySelectorAll('.photo-thumbnail');
let currentSlideIndex = 0;
let photoSrcs = [
{% for photo in photos %} "{{ site.baseurl }}/assets/galleries/{{ photo.src-large }}",
{% endfor %}
Expand All @@ -64,112 +55,4 @@
{% for photo in photos %} "{% if photo.title %}{{ photo.title }}{% if photo.caption %} &ndash; {% endif %}{% endif %}{% if photo.caption %}{{ photo.caption }}{% endif %}",
{% endfor %}
];

function preloadImages() {
photoSrcs.forEach((src) => {
const img = new Image();
img.src = src;
});
}

document.addEventListener('DOMContentLoaded', function() {
preloadImages(); // Preload images when the DOM is ready
});

galleryImages.forEach((image) => {
image.addEventListener('click', () => {
showModal();
showPhoto(parseInt(image.dataset.orderindex));
});
});

modal.addEventListener('click', (e) => {
if (e.target === modal || e.target.classList.contains('close')) {
hideModal();
}
});

modalImg.addEventListener('load', function() {
modalControls.forEach( (c) => {
c.classList.add('hidden');
});
});

modalPrev.addEventListener('click', () => {
showPhoto(currentSlideIndex - 1);
});

modalNext.addEventListener('click', () => {
showPhoto(currentSlideIndex + 1);
});

function showModal() {
modal.classList.add('visible');
document.body.style.overflow = 'hidden'; // Hide the scrollbar
}

function hideModal() {
modal.classList.remove('visible');
document.body.style.overflow = 'auto'; // Restore the scrollbar
modalPrev.classList.remove('hidden');
modalNext.classList.remove('hidden');
modalImg.src = "";
}

function showPhoto(n) {
if (n >= photoSrcs.length) {
n = 0;
}
if (n < 0) {
n = photoSrcs.length - 1;
}
currentSlideIndex = n;
modalImg.src = photoSrcs[n];
const details = photoDetails[n];
if (details) {
modalDetails.innerHTML = "<p>" + details + "</p>"
modalDetails.classList.add('visible');
} else if (modalDetails.classList.contains('visible')) {
modalDetails.classList.remove('visible');
}
}

document.addEventListener('keydown', (e) => {
if (modal.classList.contains('visible')) {
if (e.key === 'ArrowLeft' || e.keyCode === 37) {
showPhoto(currentSlideIndex - 1);
} else if (e.key === 'ArrowRight' || e.keyCode === 39) {
showPhoto(currentSlideIndex + 1);
} else if (e.key === 'Escape' || e.keyCode === 27) {
hideModal();
}
}
});

let touchStartX = 0;
let touchEndX = 0;

document.addEventListener('touchstart', e => {
if (modal.classList.contains('visible')) {
touchStartX = e.changedTouches[0].screenX
}
})

document.addEventListener('touchend', e => {
if (modal.classList.contains('visible')) {
if (e.target === modalClose) {
hideModal();
}
touchEndX = e.changedTouches[0].screenX
updateOnSwipe()
}
})
function updateOnSwipe() {
const diff = touchEndX - touchStartX
if (diff < -50) {
showPhoto(currentSlideIndex - 1);
} else if (diff > 50) {
showPhoto(currentSlideIndex + 1);
}
}
</script>
54 changes: 39 additions & 15 deletions docs/_layouts/default.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,30 @@
<ul id="main-menu">
<li class="separator"></li>

<li><a href="{{ "scrape_the_surface" | relative_url }}">Scrape the Surface</a></li>
<li><a href="{{ "shipped_around" | relative_url }}">Shipped Around</a></li>
<li><a href="{{ "patterns" | relative_url }}">Patterns</a></li>
<li><a href="{{ "lines_that_divide_or_unite" | relative_url }}">Lines that Divide or Unite</a></li>
<li><a href="{{ "shaping_the_silhouette" | relative_url }}">Shaping the Silhouette</a></li>
<li><a href="{{ "the_other_side_of_the_mirror" | relative_url }}">The Other Side of the Mirror</a></li>
<li id="nav-scrape-surface"><a href="{{ "scrape_the_surface" | relative_url }}">Scrape the Surface</a></li>
<li id="nav-shipped-around"><a href="{{ "shipped_around" | relative_url }}">Shipped Around</a></li>
<li id="nav-patterns"><a href="{{ "patterns" | relative_url }}">Patterns</a></li>
<li id="nav-lines-divide-unite"><a href="{{ "lines_that_divide_or_unite" | relative_url }}">Lines that Divide or Unite</a></li>
<li id="nav-shaping-silhouette"><a href="{{ "shaping_the_silhouette" | relative_url }}">Shaping the Silhouette</a></li>
<li id="nav-other-side-mirror"><a href="{{ "the_other_side_of_the_mirror" | relative_url }}">The Other Side of the Mirror</a></li>

<li class="separator"></li>

<li><a href="{{ "insides" | relative_url }}">Insides</a></li>
<li><a href="{{ "strolling_the_streets" | relative_url }}">Strolling the Streets</a></li>
<li><a href="{{ "outsides" | relative_url }}">Outside</a></li>
<li id="nav-insides"><a href="{{ "insides" | relative_url }}">Insides</a></li>
<li id="nav-strolling-streets"><a href="{{ "strolling_the_streets" | relative_url }}">Strolling the Streets</a></li>
<li id="nav-outsides"><a href="{{ "outsides" | relative_url }}">Outside</a></li>

<li class="separator"></li>

<li><a href="{{ "somewhere_up_in_the_sky" | relative_url }}">Somewhere up in the Sky</a></li>
<li><a href="{{ "skies" | relative_url }}">Skies</a></li>
<li id="nav-somewhere-up-sky"><a href="{{ "somewhere_up_in_the_sky" | relative_url }}">Somewhere up in the Sky</a></li>
<li id="nav-skies"><a href="{{ "skies" | relative_url }}">Skies</a></li>

<li class="separator"></li>

<li><a href="{{ "solitude" | relative_url }}">Solitudes</a></li>
<li><a href="{{ "foggy" | relative_url }}">Foggy</a></li>
<li><a href="{{ "movement" | relative_url }}">Movement</a></li>
<li><a href="{{ "portraits" | relative_url }}">Portraits</a></li>
<li id="nav-solitude"><a href="{{ "solitude" | relative_url }}">Solitudes</a></li>
<li id="nav-foggy"><a href="{{ "foggy" | relative_url }}">Foggy</a></li>
<li id="nav-movement"><a href="{{ "movement" | relative_url }}">Movement</a></li>
<li id="nav-portraits"><a href="{{ "portraits" | relative_url }}">Portraits</a></li>
</ul>
</nav>

Expand All @@ -51,4 +51,28 @@

</footer>

<script>
{% if page.navID %}
const currentNav = document.getElementById("{{ page.navID }}");
currentNav.classList.add("active");

// Set the scroll position of the menu
const menuItems = document.querySelectorAll("#main-menu > li");
let scrollPosition = 0;
let currentIndex = 0;
let currentNavID = menuItems[currentIndex].id;
while (currentNavID !== "{{ page.navID }}") {
scrollPosition += menuItems[currentIndex].offsetHeight;
currentIndex += 1;
currentNavID = menuItems[currentIndex].id;
}
scrollPosition -= (document.getElementById("main-menu-wrap").offsetHeight / 2);
document.getElementById("main-menu").scrollTop = scrollPosition;
{% endif %}
</script>

{% if page.additional_script %}
<script src="{{ site.baseurl }}/assets/js/{{ page.additional_script }}"></script>
{% endif %}

</body>
6 changes: 5 additions & 1 deletion docs/assets/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,13 @@ Main Menu
transition: background-color 0.5s;
}

#main-menu > li.active {
background-color: var(--color-background-dark);
}

#main-menu .separator {
margin: 25px 20%;
border: 1px #939393 solid;
border: 1px var(--color-background-dark) solid;
}

/* ========
Expand Down
117 changes: 117 additions & 0 deletions docs/assets/js/gallery.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
const modal = document.getElementById('modal');
const modalImg = document.getElementById('modal-image');
const modalControls = document.querySelectorAll('.modal-control');
const modalPrev = document.getElementById('modal-prev');
const modalNext = document.getElementById('modal-next');
const modalClose = document.getElementById('modal-close');
const modalDetails = document.getElementById('modal-details-box');
const galleryImages = document.querySelectorAll('.photo-thumbnail');
let currentSlideIndex = 0;

function preloadImages() {
photoSrcs.forEach((src) => {
const img = new Image();
img.src = src;
});
}

document.addEventListener('DOMContentLoaded', function() {
preloadImages(); // Preload images when the DOM is ready
});

galleryImages.forEach((image) => {
image.addEventListener('click', () => {
showModal();
showPhoto(parseInt(image.dataset.orderindex));
});
});

modal.addEventListener('click', (e) => {
if (e.target === modal || e.target.classList.contains('close')) {
hideModal();
}
});

modalImg.addEventListener('load', function() {
modalControls.forEach( (c) => {
c.classList.add('hidden');
});
});

modalPrev.addEventListener('click', () => {
showPhoto(currentSlideIndex - 1);
});

modalNext.addEventListener('click', () => {
showPhoto(currentSlideIndex + 1);
});

function showModal() {
modal.classList.add('visible');
document.body.style.overflow = 'hidden'; // Hide the scrollbar
}

function hideModal() {
modal.classList.remove('visible');
document.body.style.overflow = 'auto'; // Restore the scrollbar
modalPrev.classList.remove('hidden');
modalNext.classList.remove('hidden');
modalImg.src = "";
}

function showPhoto(n) {
if (n >= photoSrcs.length) {
n = 0;
}
if (n < 0) {
n = photoSrcs.length - 1;
}
currentSlideIndex = n;
modalImg.src = photoSrcs[n];
const details = photoDetails[n];
if (details) {
modalDetails.innerHTML = "<p>" + details + "</p>"
modalDetails.classList.add('visible');
} else if (modalDetails.classList.contains('visible')) {
modalDetails.classList.remove('visible');
}
}

document.addEventListener('keydown', (e) => {
if (modal.classList.contains('visible')) {
if (e.key === 'ArrowLeft' || e.keyCode === 37) {
showPhoto(currentSlideIndex - 1);
} else if (e.key === 'ArrowRight' || e.keyCode === 39) {
showPhoto(currentSlideIndex + 1);
} else if (e.key === 'Escape' || e.keyCode === 27) {
hideModal();
}
}
});

let touchStartX = 0;
let touchEndX = 0;

document.addEventListener('touchstart', e => {
if (modal.classList.contains('visible')) {
touchStartX = e.changedTouches[0].screenX
}
})

document.addEventListener('touchend', e => {
if (modal.classList.contains('visible')) {
if (e.target === modalClose) {
hideModal();
}
touchEndX = e.changedTouches[0].screenX
updateOnSwipe()
}
})
function updateOnSwipe() {
const diff = touchEndX - touchStartX
if (diff < -50) {
showPhoto(currentSlideIndex - 1);
} else if (diff > 50) {
showPhoto(currentSlideIndex + 1);
}
}
2 changes: 2 additions & 0 deletions docs/foggy.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
---
layout: default
title: Foggy | Simon Rey
navID: nav-foggy
additional_script: gallery.js
---

# <span class="foggy-text">Foggy</span>
Expand Down
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
layout: default
title: Simon Rey Photo
additional_script: gallery.js
---

# Me, Myself, and My Photos
Expand Down
2 changes: 2 additions & 0 deletions docs/insides.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
---
layout: default
title: Insides | Simon Rey
navID: nav-insides
additional_script: gallery.js
---

# Insides
Expand Down
2 changes: 2 additions & 0 deletions docs/lines_that_divide_or_unite.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
---
layout: default
title: Lines that Divide or Unite | Simon Rey
navID: nav-lines-divide-unite
additional_script: gallery.js
---

# Lines that Divide or Unite
Expand Down
2 changes: 2 additions & 0 deletions docs/movement.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
---
layout: default
title: Movement | Simon Rey
navID: nav-movement
additional_script: gallery.js
---

# Movement
Expand Down
2 changes: 2 additions & 0 deletions docs/outsides.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
---
layout: default
title: Outsides | Simon Rey
navID: nav-outsides
additional_script: gallery.js
---

# Outsides
Expand Down
Loading

0 comments on commit 748e0da

Please sign in to comment.