-
-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make scrolling more generic #201
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,90 @@ | ||
function scrollToRight() { | ||
var element = document.getElementById("showcase-scrolling-wrapper"); | ||
element.scrollBy({ | ||
top: 0, | ||
left: 320, | ||
behavior: 'smooth' | ||
}); | ||
function isScrollable(element) { | ||
return element.scrollWidth > element.clientWidth; | ||
} | ||
|
||
function updateScrollButtonStates(scrollable, leftButton, rightButton) { | ||
const isAtStart = scrollable.scrollLeft === 0; | ||
const isAtEnd = scrollable.scrollLeft + scrollable.clientWidth >= scrollable.scrollWidth - 1; // -1 to account for rounding errors | ||
|
||
leftButton.disabled = isAtStart; | ||
rightButton.disabled = isAtEnd; | ||
|
||
// Optional: Add/remove a class for styling | ||
leftButton.classList.toggle('disabled', isAtStart); | ||
rightButton.classList.toggle('disabled', isAtEnd); | ||
} | ||
|
||
function scrollToLeft() { | ||
var element = document.getElementById("showcase-scrolling-wrapper"); | ||
element.scrollBy({ | ||
top: 0, | ||
left: -320, | ||
behavior: 'smooth' | ||
function updateScrollButtonVisibility(scrollable, buttonGroup) { | ||
if (isScrollable(scrollable)) { | ||
buttonGroup.style.display = 'flex'; | ||
const leftButton = buttonGroup.querySelector(".left-scroll-button"); | ||
const rightButton = buttonGroup.querySelector(".right-scroll-button"); | ||
updateScrollButtonStates(scrollable, leftButton, rightButton); | ||
} else { | ||
buttonGroup.style.display = 'none'; | ||
} | ||
} | ||
|
||
function scroll(direction) { | ||
return function (event) { | ||
const buttonGroup = event.target.closest(".scroll-button-group"); | ||
const scrollableID = buttonGroup.getAttribute("data-scrollable"); | ||
const scrollable = document.getElementById(scrollableID); | ||
const cardWidth = scrollable.querySelector(".card").offsetWidth + 32; | ||
|
||
if (scrollable) { | ||
scrollable.scrollBy({ | ||
top: 0, | ||
left: direction * cardWidth, | ||
behavior: 'smooth' | ||
}); | ||
|
||
// Update button states after scrolling | ||
setTimeout(() => { | ||
const leftButton = buttonGroup.querySelector(".left-scroll-button"); | ||
const rightButton = buttonGroup.querySelector(".right-scroll-button"); | ||
updateScrollButtonStates(scrollable, leftButton, rightButton); | ||
}, 500); // Adjust timeout to match your scroll behavior duration | ||
} | ||
}; | ||
} | ||
|
||
const scrollToRight = scroll(1); | ||
const scrollToLeft = scroll(-1); | ||
|
||
function initializeScrollButtons() { | ||
document.querySelectorAll(".scroll-button-group").forEach(buttonGroup => { | ||
const scrollableID = buttonGroup.getAttribute("data-scrollable"); | ||
const scrollable = document.getElementById(scrollableID); | ||
|
||
if (scrollable) { | ||
const leftButton = buttonGroup.querySelector(".left-scroll-button"); | ||
const rightButton = buttonGroup.querySelector(".right-scroll-button"); | ||
|
||
updateScrollButtonVisibility(scrollable, buttonGroup); | ||
|
||
// Add event listeners to buttons | ||
leftButton.addEventListener("click", scrollToLeft); | ||
rightButton.addEventListener("click", scrollToRight); | ||
|
||
// Update visibility and button states on scroll | ||
scrollable.addEventListener('scroll', () => { | ||
updateScrollButtonStates(scrollable, leftButton, rightButton); | ||
}); | ||
|
||
// Update visibility and button states on window resize | ||
window.addEventListener('resize', () => { | ||
updateScrollButtonVisibility(scrollable, buttonGroup); | ||
}); | ||
|
||
// Optional: Update visibility and button states when content changes | ||
const observer = new MutationObserver(() => { | ||
updateScrollButtonVisibility(scrollable, buttonGroup); | ||
}); | ||
observer.observe(scrollable, { childList: true, subtree: true }); | ||
} | ||
}); | ||
} | ||
|
||
// Call this function when the DOM is ready | ||
document.addEventListener('DOMContentLoaded', initializeScrollButtons); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need dark mode versions of the disabled state