Skip to content

Commit

Permalink
feat(rtl): Add Support for right to left slides (SSENSE#394)
Browse files Browse the repository at this point in the history
* feat(rtl): support rtl direction

* chore(build): build library

* fix(rtl): add extra slides based on drag momentum
  • Loading branch information
farnabaz authored and quinnlangille committed Mar 21, 2019
1 parent c85df61 commit 5245812
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 12 deletions.
2 changes: 1 addition & 1 deletion dist/vue-carousel.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/themes/vue/source/js/vue-carousel.min.js

Large diffs are not rendered by default.

50 changes: 40 additions & 10 deletions src/Carousel.vue
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,13 @@ export default {
*/
value: {
type: Number
},
/**
* Support right to left
*/
rtl: {
type: Boolean,
default: false
}
},
watch: {
Expand Down Expand Up @@ -433,6 +440,8 @@ export default {
currentOffset() {
if (this.isCenterModeEnabled) {
return 0;
} else if (this.rtl) {
return (this.offset - this.dragOffset) * 1;
} else {
return (this.offset + this.dragOffset) * -1;
}
Expand Down Expand Up @@ -756,7 +765,11 @@ export default {
this.dragOffset = this.dragOffset + Math.sign(deltaX) * (width / 2);
}
this.offset += this.dragOffset;
if (this.rtl) {
this.offset -= this.dragOffset;
} else {
this.offset += this.dragOffset;
}
this.dragOffset = 0;
this.dragging = false;
Expand Down Expand Up @@ -794,10 +807,19 @@ export default {
this.dragOffset = newOffsetX;
const nextOffset = this.offset + this.dragOffset;
if (nextOffset < 0) {
this.dragOffset = -Math.sqrt(-this.resistanceCoef * this.dragOffset);
} else if (nextOffset > this.maxOffset) {
this.dragOffset = Math.sqrt(this.resistanceCoef * this.dragOffset);
if (this.rtl) {
if (this.offset == 0 && this.dragOffset > 0) {
this.dragOffset = Math.sqrt(this.resistanceCoef * this.dragOffset);
} else if (this.offset == this.maxOffset && this.dragOffset < 0) {
this.dragOffset = -Math.sqrt(-this.resistanceCoef * this.dragOffset);
}
} else {
if (nextOffset < 0) {
this.dragOffset = -Math.sqrt(-this.resistanceCoef * this.dragOffset);
} else if (nextOffset > this.maxOffset) {
this.dragOffset = Math.sqrt(this.resistanceCoef * this.dragOffset);
}
}
},
onResize() {
Expand All @@ -813,11 +835,19 @@ export default {
},
render() {
// add extra slides depending on the momemtum speed
this.offset +=
Math.max(
-this.currentPerPage + 1,
Math.min(Math.round(this.dragMomentum), this.currentPerPage - 1)
) * this.slideWidth;
if (this.rtl) {
this.offset -=
Math.max(
-this.currentPerPage + 1,
Math.min(Math.round(this.dragMomentum), this.currentPerPage - 1)
) * this.slideWidth;
} else {
this.offset +=
Math.max(
-this.currentPerPage + 1,
Math.min(Math.round(this.dragMomentum), this.currentPerPage - 1)
) * this.slideWidth;
}
// & snap the new offset on a slide or page if scrollPerPage
const width = this.scrollPerPage
Expand Down

0 comments on commit 5245812

Please sign in to comment.