Skip to content

Commit

Permalink
Merge pull request #8 from n7best/master
Browse files Browse the repository at this point in the history
add go method
  • Loading branch information
progrape committed Dec 2, 2015
2 parents 8788447 + c1c7d52 commit 2d8b153
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 4 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@
swiper.next();
```

- `go`: 主动滑动到指定界面。

```
var swiper = new Swiper();
swiper.go(1);
```
##License

swiper is available under the terms of the [MIT License](http://www.opensource.org/licenses/mit-license.php).
2 changes: 1 addition & 1 deletion dist/swiper.css
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
height: 100%;
overflow: hidden;
-webkit-transition: all 0.3s ease;
transition: all 0.3s ease;
transition: all 0.3s ease;
}
.item {
height: 100%;
Expand Down
26 changes: 25 additions & 1 deletion dist/swiper.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
this._prev = 0;
this._current = 0;
this._offset = 0;
this._goto = -1;
this._eventHandlers = {};

this.$container = document.querySelector(this._options.container);
Expand Down Expand Up @@ -134,10 +135,11 @@
return false;
}

if (me._current != me._prev) {
if (me._current != me._prev || me._goto > -1) {
me._activate(me._current);
var cb = me._eventHandlers.swiped || noop;
cb.apply(me, [me._prev, me._current]);
me._goto = -1;
}
e.preventDefault();
}, false);
Expand Down Expand Up @@ -180,6 +182,28 @@
});
};

/**
* goto x page
*/
Swiper.prototype.go = function (index) {
if(index < 0 || index > this.count - 1 || index === this._current){
return;
}

if (index === 0) {
this._current = 0;
this._prev = 0;
}else{
this._current = index;
this._prev = index - 1;
}

this._goto = index;
this._show(this._current);

return this;
};

/**
* show next page
*/
Expand Down
2 changes: 1 addition & 1 deletion dist/swiper.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 25 additions & 1 deletion src/swiper.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
this._prev = 0;
this._current = 0;
this._offset = 0;
this._goto = -1;
this._eventHandlers = {};

this.$container = document.querySelector(this._options.container);
Expand Down Expand Up @@ -128,10 +129,11 @@
return false;
}

if (me._current != me._prev) {
if (me._current != me._prev || me._goto > -1) {
me._activate(me._current);
var cb = me._eventHandlers.swiped || noop;
cb.apply(me, [me._prev, me._current]);
me._goto = -1;
}
e.preventDefault();
}, false);
Expand Down Expand Up @@ -174,6 +176,28 @@
});
};

/**
* goto x page
*/
Swiper.prototype.go = function (index) {
if(index < 0 || index > this.count - 1 || index === this._current){
return;
}

if (index === 0) {
this._current = 0;
this._prev = 0;
}else{
this._current = index;
this._prev = index - 1;
}

this._goto = index;
this._show(this._current);

return this;
};

/**
* show next page
*/
Expand Down

0 comments on commit 2d8b153

Please sign in to comment.