Skip to content
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

Fix carousel failing to lock to remainder slides #303

Merged
merged 4 commits into from
Oct 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ export default {
### Configuration
| Property | Type | Default | Description |
|:--------------------------|:--------|:--------|:--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| autoplay | Boolean | false | Flag to enable autoplay
| autoplayTimeout | Number | 2000 | Time elapsed before advancing slide
| autoplayHoverPause | Boolean | false | Flag to pause autoplay on hover
| easing | String | ease | Slide transition easing. Any valid CSS transition easing accepted. |
| minSwipeDistance | Number | 8 | Minimum distance for the swipe to trigger a slide advance. |
| navigationClickTargetSize | Number | 8 | Amount of padding to apply around the label in pixels. |
Expand Down
8 changes: 6 additions & 2 deletions play/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,13 @@ play("Carousel", module)
}
})
.add("Autoplay", h => createContainer(
h, containerWidth, [h(Carousel, { props: { autoplay: true, autoplayHoverPause: false } }, generateSlideImages(h))]
)
h, containerWidth, [h(Carousel, { props: { autoplay: true, autoplayHoverPause: false } }, generateSlideImages(h))]
)
)
.add("Autoplay, Looping", h => createContainer(
h, containerWidth, [h(Carousel, { props: { autoplay: true, autoplayHoverPause: false, loop: true } }, generateSlideImages(h))]
)
)
.add("Autoplay, pause on hover", h => createContainer(
h, containerWidth, [h(Carousel, { props: { autoplay: true, autoplayHoverPause: true } }, generateSlideImages(h))]
)
Expand Down
11 changes: 10 additions & 1 deletion src/Carousel.vue
Original file line number Diff line number Diff line change
Expand Up @@ -766,7 +766,16 @@ export default {
const width = this.scrollPerPage
? this.slideWidth * this.currentPerPage
: this.slideWidth;
this.offset = width * Math.round(this.offset / width);

// lock offset to either the nearest page, or to the last slide
const lastFullPageOffset = width * Math.floor(this.slideCount / this.currentPerPage - 1)
const remainderOffset = lastFullPageOffset + this.slideWidth * (this.slideCount % this.currentPerPage)
if (this.offset > (lastFullPageOffset + remainderOffset) / 2) {
this.offset = remainderOffset
}
else {
this.offset = width * Math.round(this.offset / width);
}

// clamp the offset between 0 -> maxOffset
this.offset = Math.max(0, Math.min(this.offset, this.maxOffset));
Expand Down