[new feature] Swipe: support disable loop (#670)

This commit is contained in:
neverland 2018-03-06 16:56:01 +08:00 committed by GitHub
parent 9027c3e8b4
commit 8e7930033b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 3 deletions

View File

@ -72,6 +72,7 @@ export default {
|-----------|-----------|-----------|-------------|-------------|
| autoplay | Autoplay interval (ms) | `Number` | - | - |
| duration | Animation duration (ms) | `Number` | `500` | - |
| loop | Whether to enable loop | `Boolean` | `true` | - |
| show-indicators | Whether to show indocators | `Boolean` | `true` | - |
| initial-swipe | Index of initial swipe, start from 0 | `Number` | `0` | - |

View File

@ -72,6 +72,7 @@ export default {
|-----------|-----------|-----------|-------------|-------------|
| autoplay | 自动轮播间隔,单位为 ms | `Number` | - | - |
| duration | 动画时长,单位为 ms | `Number` | `500` | - |
| loop | 是否开启循环播放 | `Boolean` | `true` | - |
| show-indicators | 是否显示指示器 | `Boolean` | `true` | - |
| initial-swipe | 初始位置,从 0 开始算 | `Number` | `0` | - |

View File

@ -29,6 +29,10 @@ export default create({
props: {
autoplay: Number,
loop: {
type: Boolean,
default: true
},
initialSwipe: {
type: Number,
default: 0
@ -152,6 +156,14 @@ export default create({
move(move = 0, offset = 0) {
const { active, count, swipes, deltaX, width } = this;
if (
!this.loop &&
((active === 0 && (offset > 0 || move < 0)) ||
(active === count - 1 && (offset < 0 || move > 0)))
) {
return;
}
if (move) {
if (active === -1) {
swipes[count - 1].offset = 0;
@ -190,9 +202,9 @@ export default create({
},
getDirection(touch) {
const distanceX = Math.abs(touch.clientX - this.startX);
const distanceY = Math.abs(touch.clientY - this.startY);
return distanceX > distanceY ? 'horizontal' : distanceX < distanceY ? 'vertical' : '';
const offsetX = Math.abs(touch.clientX - this.startX);
const offsetY = Math.abs(touch.clientY - this.startY);
return offsetX > offsetY ? 'horizontal' : offsetX < offsetY ? 'vertical' : '';
},
range(num, arr) {