Skip to content

Commit 2d8b153

Browse files
committed
Merge pull request #8 from n7best/master
add go method
2 parents 8788447 + c1c7d52 commit 2d8b153

File tree

5 files changed

+58
-4
lines changed

5 files changed

+58
-4
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,12 @@
116116
swiper.next();
117117
```
118118

119+
- `go`: 主动滑动到指定界面。
120+
121+
```
122+
var swiper = new Swiper();
123+
swiper.go(1);
124+
```
119125
##License
120126

121127
swiper is available under the terms of the [MIT License](http://www.opensource.org/licenses/mit-license.php).

dist/swiper.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
height: 100%;
99
overflow: hidden;
1010
-webkit-transition: all 0.3s ease;
11-
transition: all 0.3s ease;
11+
transition: all 0.3s ease;
1212
}
1313
.item {
1414
height: 100%;

dist/swiper.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
this._prev = 0;
2929
this._current = 0;
3030
this._offset = 0;
31+
this._goto = -1;
3132
this._eventHandlers = {};
3233

3334
this.$container = document.querySelector(this._options.container);
@@ -134,10 +135,11 @@
134135
return false;
135136
}
136137

137-
if (me._current != me._prev) {
138+
if (me._current != me._prev || me._goto > -1) {
138139
me._activate(me._current);
139140
var cb = me._eventHandlers.swiped || noop;
140141
cb.apply(me, [me._prev, me._current]);
142+
me._goto = -1;
141143
}
142144
e.preventDefault();
143145
}, false);
@@ -180,6 +182,28 @@
180182
});
181183
};
182184

185+
/**
186+
* goto x page
187+
*/
188+
Swiper.prototype.go = function (index) {
189+
if(index < 0 || index > this.count - 1 || index === this._current){
190+
return;
191+
}
192+
193+
if (index === 0) {
194+
this._current = 0;
195+
this._prev = 0;
196+
}else{
197+
this._current = index;
198+
this._prev = index - 1;
199+
}
200+
201+
this._goto = index;
202+
this._show(this._current);
203+
204+
return this;
205+
};
206+
183207
/**
184208
* show next page
185209
*/

dist/swiper.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/swiper.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
this._prev = 0;
2323
this._current = 0;
2424
this._offset = 0;
25+
this._goto = -1;
2526
this._eventHandlers = {};
2627

2728
this.$container = document.querySelector(this._options.container);
@@ -128,10 +129,11 @@
128129
return false;
129130
}
130131

131-
if (me._current != me._prev) {
132+
if (me._current != me._prev || me._goto > -1) {
132133
me._activate(me._current);
133134
var cb = me._eventHandlers.swiped || noop;
134135
cb.apply(me, [me._prev, me._current]);
136+
me._goto = -1;
135137
}
136138
e.preventDefault();
137139
}, false);
@@ -174,6 +176,28 @@
174176
});
175177
};
176178

179+
/**
180+
* goto x page
181+
*/
182+
Swiper.prototype.go = function (index) {
183+
if(index < 0 || index > this.count - 1 || index === this._current){
184+
return;
185+
}
186+
187+
if (index === 0) {
188+
this._current = 0;
189+
this._prev = 0;
190+
}else{
191+
this._current = index;
192+
this._prev = index - 1;
193+
}
194+
195+
this._goto = index;
196+
this._show(this._current);
197+
198+
return this;
199+
};
200+
177201
/**
178202
* show next page
179203
*/

0 commit comments

Comments
 (0)