Skip to content

Commit

Permalink
add go method
Browse files Browse the repository at this point in the history
  • Loading branch information
n7best authored and n7best committed Nov 30, 2015
1 parent 8788447 commit 205cf7e
Show file tree
Hide file tree
Showing 6 changed files with 61 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.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,8 @@
"gulp-tap": "^0.1.3",
"gulp-uglify": "^1.0.2",
"gulp-util": "^3.0.7"
},
"dependencies": {
"dateformat": "^1.0.12"
}
}
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 205cf7e

Please sign in to comment.