Skip to content

Commit

Permalink
Scroll down button now scrolls to a next paragraph
Browse files Browse the repository at this point in the history
  • Loading branch information
pehala committed Sep 28, 2023
1 parent 16eca78 commit c4c020e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 13 deletions.
49 changes: 42 additions & 7 deletions backend/static/js/chords.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,48 @@ function transpose(event, object, id) {
}

function collapsible(element, id) {
const collapsible = $('#' + id);
$(".song:visible").slideUp()
$('#' + id).slideToggle()
}

function isElementInViewPort(element) {
let rect = element.getBoundingClientRect();
// get the height of the window
let viewPortBottom = window.innerHeight || document.documentElement.clientHeight;
// get the width of the window
let viewPortRight = window.innerWidth || document.documentElement.clientWidth;

let isTopInViewPort = rect.top >= 0,
isLeftInViewPort = rect.left >= 0,
isBottomInViewPort = rect.bottom <= viewPortBottom,
isRightInViewPort = rect.right <= viewPortRight;

// check if element is completely visible inside the viewport
return (isTopInViewPort && isLeftInViewPort && isBottomInViewPort && isRightInViewPort);

}

if (collapsible.hasClass('unrolled') === true) {
collapsible.slideUp();
} else {
collapsible.slideDown();
function betterScrollDown() {
let children = $(".song:visible p ")
if (children.length === 0)
return

let scrollTo = null;
for (let i = 0; i < children.length - 1; i++) {
if (isElementInViewPort(children[i])) {
scrollTo = children[i+1]
console.log(i+1)
break
}
}
collapsible.toggleClass("unrolled");
element.classList.toggle("unrolled");

let last_visible = isElementInViewPort(children[children.length - 1])

if (scrollTo !== null && !last_visible) {
console.log(scrollTo.getBoundingClientRect().top)
$('body,html').animate({
scrollTop: `+=${0.8 * scrollTo.getBoundingClientRect().top}`,
}, 800);
}

}
8 changes: 2 additions & 6 deletions backend/templates/songs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ <h6 class="d-inline" ><%:author%></h6>
{% endif %}
</span>
</div>
<div id="<%:number%>" style="display: none;">
<div id="<%:number%>" class="song" style="display: none;">
<div class="input-group mb-3">
<%if link%>
<a href="<%:link%>" class="btn btn-primary" role="button" aria-pressed="true">
Expand Down Expand Up @@ -161,11 +161,7 @@ <h6 class="d-inline" ><%:author%></h6>
});

$( "#scroll-down" ).click(function() {
const scrollAmount = (typeof window.outerHeight != 'undefined') ? Math.max(window.outerHeight, $(window).height()):$(window).height()
console.log(scrollAmount)
$('html, body').animate({
scrollTop: `+=${0.8 * scrollAmount}`
}, 800);
betterScrollDown()
});

let hidden = false
Expand Down

0 comments on commit c4c020e

Please sign in to comment.