-
Notifications
You must be signed in to change notification settings - Fork 13
Perform current slide calculation based on child elements #1334
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
Merged
+121
−27
Merged
Changes from 2 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
748da97
Perform current slide calculation based on child elements
indykoning 47571c9
Apply fixes from Prettier
indykoning 40f72ed
Use total children since these also affect scroll width/height
indykoning 74fd31d
Apply fixes from Prettier
indykoning 26dd07d
Fix loop getting no slides
indykoning 39ebd5a
Merge branch 'bugfix/current-calculation' of github.com:rapidez/core …
indykoning a18226c
Apply fixes from Prettier
indykoning 8bd864f
Fix bestguess for loop sliders
indykoning edbad95
Apply fixes from Prettier
indykoning 0f4f2f6
Implement useScroll to determine scroll positions
indykoning cffc5d2
Apply fixes from Prettier
indykoning ffd1f53
Added todo
indykoning 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -64,11 +64,11 @@ export default { | |
| } | ||
| this.$nextTick(() => { | ||
| useResizeObserver(this.slider, useThrottleFn(this.updateSpan, 150, true, true)) | ||
| setTimeout(() => this.slider.dispatchEvent(new CustomEvent('scroll'))) | ||
| this.mounted = true | ||
| if (this.loop) { | ||
| this.initLoop() | ||
| } | ||
| setTimeout(() => this.slider.dispatchEvent(new CustomEvent('scroll'))) | ||
| this.mounted = true | ||
|
|
||
| this.initAutoPlay() | ||
| }) | ||
|
|
@@ -151,7 +151,10 @@ export default { | |
| this.navigate(next) | ||
| }, | ||
| navigate(index, behavior = 'smooth') { | ||
| index = this.loop ? index + this.slides.length : index | ||
| index = Math.min( | ||
| Math.max(index, 0), | ||
| this.container?.children.length - 1 | ||
| ) | ||
|
|
||
| this.vertical | ||
| ? this.slider.scrollTo({ top: this.container?.children[index]?.offsetTop, behavior: behavior }) | ||
|
|
@@ -202,14 +205,40 @@ export default { | |
| if (!this.mounted) { | ||
| return 0 | ||
| } | ||
| return Math.round(this.position / this.childSpan) % this.slides.length | ||
| // First make a calculated guess, improving performance by not having to loop through all slides | ||
| const bestGuess = Math.round(this.position / this.childSpan) % this.slides.length | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| const getSlideByGuess = (slide) => { | ||
| const bestGuessChild = this.container.children[slide] | ||
| if (!bestGuessChild) { | ||
| return slide; | ||
| } | ||
| const bestGuessSpan = this.vertical | ||
| ? bestGuessChild.offsetHeight | ||
| : bestGuessChild.offsetWidth | ||
| const bestGuessOffset = this.vertical | ||
| ? bestGuessChild.offsetTop | ||
| : bestGuessChild.offsetLeft | ||
|
|
||
| // Past halfway to the slide it will snap | ||
| if (this.position + (bestGuessSpan / 2) < bestGuessOffset) { | ||
| return getSlideByGuess(slide - 1); | ||
| } | ||
|
|
||
| if (this.position >= bestGuessOffset + bestGuessSpan) { | ||
| return getSlideByGuess(slide + 1); | ||
| } | ||
|
Jade-GG marked this conversation as resolved.
|
||
|
|
||
| return slide; | ||
| } | ||
|
|
||
| return getSlideByGuess(bestGuess) | ||
| }, | ||
| slidesVisible() { | ||
| if (!this.mounted) { | ||
| return 0 | ||
| } | ||
|
|
||
| return Math.round(this.sliderSpan / this.childSpan) | ||
| return Math.round(this.sliderSpan / (this.vertical ? this.container.scrollHeight : this.container.scrollWidth) * this.slides.length) | ||
| }, | ||
| slidesTotal() { | ||
| if (!this.mounted) { | ||
|
|
||
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.
This new order is necessary due to 176b2cd#diff-2d53f035dc6e0201915f81219d324c2479c3acd61fde0b67ffbc091205965494R222