-
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
base: master
Are you sure you want to change the base?
Changes from 3 commits
748da97
47571c9
40f72ed
74fd31d
26dd07d
39ebd5a
a18226c
8bd864f
edbad95
0f4f2f6
cffc5d2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -151,7 +151,7 @@ export default { | |
| this.navigate(next) | ||
| }, | ||
| navigate(index, behavior = 'smooth') { | ||
| index = this.loop ? index + this.slides.length : index | ||
| index = Math.min(Math.max(this.loop ? index + this.slides.length : index, 0), this.container?.children.length - 1) | ||
|
|
||
| this.vertical | ||
| ? this.slider.scrollTo({ top: this.container?.children[index]?.offsetTop, behavior: behavior }) | ||
|
|
@@ -202,14 +202,38 @@ 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 | ||
| 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.container.children.length, | ||
| ) | ||
|
Comment on lines
+300
to
+303
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. The reason we're not calculating from the currentSlide to the last visible slide is because this could cause the visibleSlides to jump up and down depending on your position in the slider. While visibleSlides is also used in slidesTotal, causing the total to change
Collaborator
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. I think this will still cause incorrect calculations for the Is this value still relevant? Because on most shops we already hide the "progress bar" under sliders anyway.
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. Where do you see this calculation being incorrect? let averageSlideWidth = 2191 / 7; // 313
let visibleSlides = 1252 / 313; // 4The only change in this calculation is that instead of using the width of the first child, we're using the average width of all childs.
Collaborator
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. I'm imagining a hypothetical situation where you have 100 wide slides (say, 100px wide each) followed by a bunch extremely tiny slides (say, 1px) at the end, where only two of the wide slides are visible (so the slider span itself is 200px). slidesVisible would still be 2 even if you have up to 25 of the tiny slides at the end. With the current calculation of the slidesTotal, you'd have 24 extra useless "slides" in your progress bar (as all of the tiny slides would all be visible after slide number 100). Obviously this is an absurd example, but more realistically I could see there being 1 too few or 1 too many slides in real world situations. |
||
| }, | ||
| slidesTotal() { | ||
| if (!this.mounted) { | ||
|
|
||
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 bestGuess is correct on regularly sized sliders.


Depending on the size differences in other childs it may only have to check one additional element.