Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
micmalti committed Oct 8, 2024
1 parent 651f904 commit 6b9e78c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 37 deletions.
36 changes: 36 additions & 0 deletions public/scripts/ContactButton.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
document.addEventListener('DOMContentLoaded', function() {
let contactSection = document.getElementById('contact');
let contactButton = document.querySelector('a[href="#contact"]');

function getSectionPosition() {
let rect = contactSection.getBoundingClientRect();
let scrollTop = window.scrollY || document.documentElement.scrollTop;
return {
top: rect.top + scrollTop,
height: rect.height,
bottom: rect.top + scrollTop + rect.height
};
} // get position of #contact section

function toggleButtonVisibility() {
let sectionPos = getSectionPosition();
let scrollTop = window.scrollY || document.documentElement.scrollTop;
let viewportHeight = window.innerHeight;
let scrollBottom = scrollTop + viewportHeight;

if (window.innerWidth >= 768) {
let inView = (scrollBottom > sectionPos.top) && (scrollTop < sectionPos.bottom);
if (inView) {
contactButton.classList.add('md:hidden');
} else {
contactButton.classList.remove('md:hidden');
}
} else {
contactButton.classList.add('md:hidden');
}
} // update button visibility based on the scroll position

toggleButtonVisibility();
window.addEventListener('scroll', toggleButtonVisibility); // listener to update button visibility
window.addEventListener('resize', toggleButtonVisibility); // listener to recheck visibility if screen resized
});
36 changes: 0 additions & 36 deletions public/scripts/animations.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,39 +32,3 @@
// };
// })

document.addEventListener('DOMContentLoaded', function() {
let contactSection = document.getElementById('contact');
let contactButton = document.querySelector('a[href="#contact"]');

function getSectionPosition() {
let rect = contactSection.getBoundingClientRect();
let scrollTop = window.scrollY || document.documentElement.scrollTop;
return {
top: rect.top + scrollTop,
height: rect.height,
bottom: rect.top + scrollTop + rect.height
};
} // get position of #contact section

function toggleButtonVisibility() {
let sectionPos = getSectionPosition();
let scrollTop = window.scrollY || document.documentElement.scrollTop;
let viewportHeight = window.innerHeight;
let scrollBottom = scrollTop + viewportHeight;

if (window.innerWidth >= 768) {
let inView = (scrollBottom > sectionPos.top) && (scrollTop < sectionPos.bottom);
if (inView) {
contactButton.classList.add('md:hidden');
} else {
contactButton.classList.remove('md:hidden');
}
} else {
contactButton.classList.add('md:hidden');
}
} // update button visibility based on the scroll position

toggleButtonVisibility();
window.addEventListener('scroll', toggleButtonVisibility); // listener to update button visibility
window.addEventListener('resize', toggleButtonVisibility); // listener to recheck visibility if screen resized
});
2 changes: 1 addition & 1 deletion src/layouts/PageLayout.astro
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const { pageTitle } = Astro.props;
</div>
</div>
<script type="text/javascript" src="/scripts/SmoothScrolling.js"></script>
<!-- <script type="text/javascript" src="/scripts/animations.js"></script> -->
<script type="text/javascript" src="/scripts/ContactButton.js"></script>
<script type="text/javascript" src="/scripts/FormHandler.js"></script>
</body>
</html>

0 comments on commit 6b9e78c

Please sign in to comment.