From 205cf7ecc5e7bc082fd899faafd538bf08ab51a2 Mon Sep 17 00:00:00 2001 From: n7best Date: Mon, 30 Nov 2015 12:19:18 -0500 Subject: [PATCH 1/2] add go method --- README.md | 6 ++++++ dist/swiper.css | 2 +- dist/swiper.js | 26 +++++++++++++++++++++++++- dist/swiper.min.js | 2 +- package.json | 3 +++ src/swiper.js | 26 +++++++++++++++++++++++++- 6 files changed, 61 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 9ce2cfc..c2b7fe0 100644 --- a/README.md +++ b/README.md @@ -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). diff --git a/dist/swiper.css b/dist/swiper.css index 8019a56..30ee5d3 100644 --- a/dist/swiper.css +++ b/dist/swiper.css @@ -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%; diff --git a/dist/swiper.js b/dist/swiper.js index 91e2d80..44d9c7c 100644 --- a/dist/swiper.js +++ b/dist/swiper.js @@ -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); @@ -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); @@ -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 */ diff --git a/dist/swiper.min.js b/dist/swiper.min.js index 8dea173..ea3c48c 100644 --- a/dist/swiper.min.js +++ b/dist/swiper.min.js @@ -4,4 +4,4 @@ * @link https://github.com/weui/swiper.git * @license MIT */ -!function(t,e){"function"==typeof define?define(e):this[t]=e()}("Swiper",function(){function t(t){this.version="1.4.0",this._default={container:".swiper",item:".item",direction:"vertical",activeClass:"active",threshold:50,duration:300},this._options=e(this._default,t),this._start={},this._move={},this._end={},this._prev=0,this._current=0,this._offset=0,this._eventHandlers={},this.$container=document.querySelector(this._options.container),this.$items=this.$container.querySelectorAll(this._options.item),this.count=this.$items.length,this._width=this.$container.offsetWidth,this._height=this.$container.offsetHeight,this._init(),this._bind()}function e(t,e){for(var n in e)t[n]=e[n];return t}function n(){}return t.prototype._init=function(){var t=this,e=t._width,n=t._height,i=e,r=n*t.count;"horizontal"===t._options.direction&&(i=e*t.count,r=n),t.$container.style.width=i+"px",t.$container.style.height=r+"px",Array.prototype.forEach.call(t.$items,function(t,i){t.style.width=e+"px",t.style.height=n+"px"}),t._activate(0)},t.prototype._bind=function(){var t=this;this.$container.addEventListener("touchstart",function(e){t._start.x=e.changedTouches[0].pageX,t._start.y=e.changedTouches[0].pageY,t.$container.style["-webkit-transition"]="none",t.$container.style.transition="none"},!1),this.$container.addEventListener("touchmove",function(e){t._move.x=e.changedTouches[0].pageX,t._move.y=e.changedTouches[0].pageY;var n=t._move.y-t._start.y,i="translate3d(0, "+(n-t._offset)+"px, 0)";"horizontal"===t._options.direction&&(n=t._move.x-t._start.x,i="translate3d("+(n-t._offset)+"px, 0, 0)"),t.$container.style["-webkit-transform"]=i,t.$container.style.transform=i,e.preventDefault()},!1),this.$container.addEventListener("touchend",function(e){t._end.x=e.changedTouches[0].pageX,t._end.y=e.changedTouches[0].pageY;var n=t._end.y-t._start.y;"horizontal"===t._options.direction&&(n=t._end.x-t._start.x),t._prev=t._current,n>t._options.threshold?t._current=0===t._current?0:--t._current:n<-t._options.threshold&&(t._current=t._current=this.count-1?void 0:(this._prev=this._current,this._show(++this._current),this)},t.prototype.on=function(t,e){if(this._eventHandlers[t])throw new Error("event "+t+" is already register");if("function"!=typeof e)throw new Error("parameter callback must be a function");return this._eventHandlers[t]=e,this},t}); \ No newline at end of file +!function(t,e){"function"==typeof define?define(e):this[t]=e()}("Swiper",function(){function t(t){this.version="1.4.0",this._default={container:".swiper",item:".item",direction:"vertical",activeClass:"active",threshold:50,duration:300},this._options=e(this._default,t),this._start={},this._move={},this._end={},this._prev=0,this._current=0,this._offset=0,this._goto=-1,this._eventHandlers={},this.$container=document.querySelector(this._options.container),this.$items=this.$container.querySelectorAll(this._options.item),this.count=this.$items.length,this._width=this.$container.offsetWidth,this._height=this.$container.offsetHeight,this._init(),this._bind()}function e(t,e){for(var n in e)t[n]=e[n];return t}function n(){}return t.prototype._init=function(){var t=this,e=t._width,n=t._height,i=e,r=n*t.count;"horizontal"===t._options.direction&&(i=e*t.count,r=n),t.$container.style.width=i+"px",t.$container.style.height=r+"px",Array.prototype.forEach.call(t.$items,function(t,i){t.style.width=e+"px",t.style.height=n+"px"}),t._activate(0)},t.prototype._bind=function(){var t=this;this.$container.addEventListener("touchstart",function(e){t._start.x=e.changedTouches[0].pageX,t._start.y=e.changedTouches[0].pageY,t.$container.style["-webkit-transition"]="none",t.$container.style.transition="none"},!1),this.$container.addEventListener("touchmove",function(e){t._move.x=e.changedTouches[0].pageX,t._move.y=e.changedTouches[0].pageY;var n=t._move.y-t._start.y,i="translate3d(0, "+(n-t._offset)+"px, 0)";"horizontal"===t._options.direction&&(n=t._move.x-t._start.x,i="translate3d("+(n-t._offset)+"px, 0, 0)"),t.$container.style["-webkit-transform"]=i,t.$container.style.transform=i,e.preventDefault()},!1),this.$container.addEventListener("touchend",function(e){t._end.x=e.changedTouches[0].pageX,t._end.y=e.changedTouches[0].pageY;var n=t._end.y-t._start.y;"horizontal"===t._options.direction&&(n=t._end.x-t._start.x),t._prev=t._current,n>t._options.threshold?t._current=0===t._current?0:--t._current:n<-t._options.threshold&&(t._current=t._current-1){t._activate(t._current);var i=t._eventHandlers.swiped||n;i.apply(t,[t._prev,t._current]),t._goto=-1}e.preventDefault()},!1)},t.prototype._show=function(t){this._offset=t*this._height;var e="translate3d(0, -"+this._offset+"px, 0)";"horizontal"===this._options.direction&&(this._offset=t*this._width,e="translate3d(-"+this._offset+"px, 0, 0)");var n=this._options.duration+"ms";this.$container.style["-webkit-transition"]=n,this.$container.style.transition=n,this.$container.style["-webkit-transform"]=e,this.$container.style.transform=e},t.prototype._activate=function(t){var e=this._options.activeClass;Array.prototype.forEach.call(this.$items,function(n,i){n.classList.remove(e),t===i&&n.classList.add(e)})},t.prototype.go=function(t){return 0>t||t>this.count-1||t===this._current?void 0:(0===t?(this._current=0,this._prev=0):(this._current=t,this._prev=t-1),this._goto=t,this._show(this._current),this)},t.prototype.next=function(){return this._current>=this.count-1?void 0:(this._prev=this._current,this._show(++this._current),this)},t.prototype.on=function(t,e){if(this._eventHandlers[t])throw new Error("event "+t+" is already register");if("function"!=typeof e)throw new Error("parameter callback must be a function");return this._eventHandlers[t]=e,this},t}); \ No newline at end of file diff --git a/package.json b/package.json index 566f0b7..89c5d4d 100644 --- a/package.json +++ b/package.json @@ -23,5 +23,8 @@ "gulp-tap": "^0.1.3", "gulp-uglify": "^1.0.2", "gulp-util": "^3.0.7" + }, + "dependencies": { + "dateformat": "^1.0.12" } } diff --git a/src/swiper.js b/src/swiper.js index cf1dcd9..7792141 100644 --- a/src/swiper.js +++ b/src/swiper.js @@ -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); @@ -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); @@ -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 */ From c1c7d524bb91271d38e2012874fd55d10533c245 Mon Sep 17 00:00:00 2001 From: n7best Date: Mon, 30 Nov 2015 12:21:14 -0500 Subject: [PATCH 2/2] remove error dependency --- package.json | 3 --- 1 file changed, 3 deletions(-) diff --git a/package.json b/package.json index 89c5d4d..566f0b7 100644 --- a/package.json +++ b/package.json @@ -23,8 +23,5 @@ "gulp-tap": "^0.1.3", "gulp-uglify": "^1.0.2", "gulp-util": "^3.0.7" - }, - "dependencies": { - "dateformat": "^1.0.12" } }