Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

swipeTo for easier translate #50

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,42 @@ vue-swipe is a touch slider for vue.js.
$ npm install vue-swipe
```

# for JavaScript user :
install NodeJs first, then install npm plugins for conpile:
install vue package: https://cn.vuejs.org/v2/guide/installation.html#Bower
```bash
npm install -g vue
npm install --global vue-cli
```
install webpack package: https://webpack.github.io/docs/webpack-dev-server.html
```bash
npm install -g webpack-dev-server
```
compile :
```bash
npm install vue -save-dev
npm install vue-loader -save-dev
npm install vue-template-compiler -save-dev
npm install
npm run build // now file dist/vue-swipe.css and dist/vue-swipe.js is available
npm run dev // Open http://localhost:8080/example/ in browser
```

# Detail for code usage :
```JavaScript
swipeEle = new Vue({
el: '#container',
components: {
'swipe': vueSwipe,
'swipe-item': vueSwipeItem
}
}).$children[0];

swipeWidth = swipeEle.$el.clientWidth;
swipeEle.next();
swipeEle.prev();
swipeEle.doAnimate(undefined, opt);

# Install

## Import using module
Expand Down
2 changes: 1 addition & 1 deletion dist/vue-swipe.css

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

2 changes: 1 addition & 1 deletion dist/vue-swipe.js

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,13 @@
</head>
<body>
<div id="container">
<swipe class="my-swipe">
<swipe class="my-swipe" :auto="0" v-on:index-change="indexChange">
<swipe-item class="slide1">Slide1</swipe-item>
<swipe-item class="slide2">Slide2</swipe-item>
<swipe-item class="slide3">Slide3</swipe-item>
<swipe-item class="slide1">Slide4</swipe-item>
<swipe-item class="slide2">Slide5</swipe-item>
<swipe-item class="slide3">Slide6</swipe-item>
</swipe>

<br>
Expand All @@ -50,6 +53,6 @@

<script src="../node_modules/vue/dist/vue.js"></script>
<script src="/dist/vue-swipe.js"></script>
<script type="text/javascript" src="/example/dist/example.js"></script>
<script type="text/javascript" src="index.js"></script>
</body>
</html>
9 changes: 7 additions & 2 deletions example/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
const vueSwipe = VueSwipe.Swipe;
const vueSwipeItem = VueSwipe.SwipeItem;

new Vue({
swipeEle = new Vue({
el: '#container',
components: {
'swipe': vueSwipe,
'swipe-item': vueSwipeItem
},
methods: {
indexChange: function () {
alert("indexChange");
}
}
});
}).$children[0];
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@
"file-loader": "^0.8.5",
"style-loader": "^0.13.0",
"url-loader": "^0.5.7",
"vue": "2.1.8",
"vue": "^2.4.2",
"vue-hot-reload-api": "^1.2.2",
"vue-html-loader": "^1.0.0",
"vue-loader": "10.0.2",
"vue-template-compiler": "^2.1.8",
"vue-loader": "^10.0.2",
"vue-template-compiler": "^2.4.2",
"webpack": "^1.12.9",
"webpack-dev-server": "^1.14.0"
},
Expand Down
41 changes: 41 additions & 0 deletions src/swipe.vue
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@
addClass(newPage, 'is-active');

this.index = newIndex;
this.$emit('index-change');
}

if (prevPage) {
Expand Down Expand Up @@ -291,6 +292,46 @@
}
}, 10);
},

swipeTo(toIdx) {
if (this.$children.length === 0) return;
if (this.$children.length < 2) return;

var currentPage, toPage, pageWidth;
var speed = this.speed || 300;
var pages = this.pages;
var index = this.index;
var isNext = toIdx > index;
if(toIdx >= pages.length ) return;
if(toIdx < 0 ) return;

pageWidth = this.$el.clientWidth * (isNext ? 1 : -1);
currentPage = pages[index];
toPage = pages[toIdx];

var newIndex = toIdx;
var oldPage = this.$children[index].$el;

toPage.style.display = 'block';
this.translate(toPage, pageWidth);

var callback = () => {
if (newIndex !== undefined) {
var newPage = this.$children[newIndex].$el;
removeClass(oldPage, 'is-active');
addClass(newPage, 'is-active');

this.index = newIndex;
toPage.style.display = '';
this.$emit('index-change');
}
};

setTimeout(() => {
this.translate(currentPage, -pageWidth, speed, callback);
this.translate(toPage, 0, speed);
}, 10);
},

next() {
this.doAnimate('next');
Expand Down