Skip to content

Commit

Permalink
goTo function done
Browse files Browse the repository at this point in the history
  • Loading branch information
ganlanyuan committed Dec 21, 2016
1 parent c991e87 commit cdbef1c
Show file tree
Hide file tree
Showing 9 changed files with 150 additions and 91 deletions.
39 changes: 28 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# tiny-slider
![version](https://img.shields.io/badge/Version-1.0.4-green.svg)
![version](https://img.shields.io/badge/Version-1.1.4-green.svg)
Tiny slider for all purposes, inspired by [Owl Carousel](http://owlcarousel.owlgraphic.com/).
<!-- [demo](http://creatiointl.org/william/tiny-slider/v1-new/demo/) -->
The previous version is still available in branch [v0](https://github.com/ganlanyuan/tiny-slider/tree/v0), you may want to know how to [transfer from v0](transfer.md).
Expand Down Expand Up @@ -138,14 +138,13 @@ info = {
```

## Methods
#####getInfo
```javascript
// get info object
var slider = tns(...);

// getInfo:
// return info object
slider.getInfo();

document.querySelector('.my-next-button').addEventListener('click', function () {
document.querySelector('.next-button').onclick = function () {
// get slider info
var info = slider.getInfo(),
indexPrev = info.indexCached;
Expand All @@ -154,14 +153,32 @@ document.querySelector('.my-next-button').addEventListener('click', function ()
// update style based on index
info.slideItems[indexPrev].classList.remove('active');
info.slideItems[indexCurrent].classList.add('active');
}, false);
};
```

##### goTo
```javascript
// go to slides by number or keywords
var slider = tns(...);
slider.goTo(3);
slider.goTo('prev');
slider.goTo('previous');
slider.goTo('next');
slider.goTo('first');
slider.goTo('last');

document.querySelector('.goto-button').onclick = function () {
slider.goTo(3);
};
```

// destory
##### destory
```javascript
var slider = tns(...);
slider.destory();
```
## Custom Events
Available events:
`indexChanged`, `initialized`, `transitionStart`, `transitionEnd`, `touchStart`, `touchMove` and `touchEnd`.
Available events include: `initialized`, `indexChanged`, `transitionStart`, `transitionEnd`, `touchStart`, `touchMove` and `touchEnd`.
```javascript
var slider = tns(...);

Expand All @@ -170,10 +187,10 @@ var customizedFunction = function (info) {
console.log(info.event.type, info.container.id);
}

// do something on transitionend
// bind function to event
slider.events.on('transitionEnd', customizedFunction);

// remove event binding
// remove function binding
slider.events.off('transitionEnd', customizedFunction);
```
#### Fallback
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tiny-slider",
"version": "1.0.4",
"version": "1.1.4",
"homepage": "https://github.com/ganlanyuan/tiny-slider",
"authors": [
"WilliamLin <[email protected]>"
Expand Down
2 changes: 1 addition & 1 deletion dist/min/tiny-slider.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/min/tiny-slider.native.js

Large diffs are not rendered by default.

64 changes: 39 additions & 25 deletions dist/tiny-slider.js
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ gn.unwrap = function (els) {
};
/**
* tiny-slider
* @version 1.0.4
* @version 1.1.4
* @author William Lin
* @license The MIT License (MIT)
* @github https://github.com/ganlanyuan/tiny-slider/
Expand Down Expand Up @@ -1838,36 +1838,50 @@ var tns = (function () {
getInfo: info,
events: events,
goTo: function (targetIndex) {
var indexBase = (mode === 'gallery') ? 0 : cloneCount;
switch (targetIndex) {
case 'first':
targetIndex = indexBase;
break;
case 'last':
targetIndex = indexBase + slideCount - 1;
break;
case 'prev':
case 'previous':
targetIndex = index - 1;
break;
case 'next':
targetIndex = index + 1;
break;
}

// define: absolute index
// define: absolute target index
// define: task completed status, initilized to false
var absIndex = index%slideCount,
absTargetIndex = targetIndex%slideCount;

absTargetIndex,
completed = false;
// redefine: absolute index when it's minus
if (absIndex < 0) { absIndex += slideCount; }
if (absTargetIndex < 0) { absTargetIndex += slideCount; }

console.log(index, absTargetIndex, absIndex);
if (absIndex !== absTargetIndex) {
if (typeof targetIndex === 'string') {
switch (targetIndex) {
// run click functions when target is 'prev' or 'next'
// set completed status to true
case 'prev':
case 'previous':
onClickPrev();
completed = true;
break;
case 'next':
onClickNext();
completed = true;
break;
// set absolute target index
case 'first':
absTargetIndex = 0;
break;
case 'last':
absTargetIndex = slideCount - 1;
break;
}
} else {
// set absolute target index
absTargetIndex = targetIndex%slideCount;
if (absTargetIndex < 0) { absTargetIndex += slideCount; }
}

// when it's not completed and
// absolute index not equal to absolute target index
// => reset index and render
if (!completed && absIndex !== absTargetIndex) {
index += absTargetIndex - absIndex;
console.log(index);
checkIndex();

// render();
render();
}

},
Expand Down
64 changes: 39 additions & 25 deletions dist/tiny-slider.native.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* tiny-slider
* @version 1.0.4
* @version 1.1.4
* @author William Lin
* @license The MIT License (MIT)
* @github https://github.com/ganlanyuan/tiny-slider/
Expand Down Expand Up @@ -1280,36 +1280,50 @@ var tns = (function () {
getInfo: info,
events: events,
goTo: function (targetIndex) {
var indexBase = (mode === 'gallery') ? 0 : cloneCount;
switch (targetIndex) {
case 'first':
targetIndex = indexBase;
break;
case 'last':
targetIndex = indexBase + slideCount - 1;
break;
case 'prev':
case 'previous':
targetIndex = index - 1;
break;
case 'next':
targetIndex = index + 1;
break;
}

// define: absolute index
// define: absolute target index
// define: task completed status, initilized to false
var absIndex = index%slideCount,
absTargetIndex = targetIndex%slideCount;

absTargetIndex,
completed = false;
// redefine: absolute index when it's minus
if (absIndex < 0) { absIndex += slideCount; }
if (absTargetIndex < 0) { absTargetIndex += slideCount; }

console.log(index, absTargetIndex, absIndex);
if (absIndex !== absTargetIndex) {
if (typeof targetIndex === 'string') {
switch (targetIndex) {
// run click functions when target is 'prev' or 'next'
// set completed status to true
case 'prev':
case 'previous':
onClickPrev();
completed = true;
break;
case 'next':
onClickNext();
completed = true;
break;
// set absolute target index
case 'first':
absTargetIndex = 0;
break;
case 'last':
absTargetIndex = slideCount - 1;
break;
}
} else {
// set absolute target index
absTargetIndex = targetIndex%slideCount;
if (absTargetIndex < 0) { absTargetIndex += slideCount; }
}

// when it's not completed and
// absolute index not equal to absolute target index
// => reset index and render
if (!completed && absIndex !== absTargetIndex) {
index += absTargetIndex - absIndex;
console.log(index);
checkIndex();

// render();
render();
}

},
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tiny-slider",
"version": "1.0.4",
"version": "1.1.4",
"description": "Pure tiny javascript slider for all purposes.",
"main": "dist/tiny-slider.js",
"style": "dist/tiny-slider.css",
Expand Down
64 changes: 39 additions & 25 deletions src/tiny-slider.native.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* tiny-slider
* @version 1.0.4
* @version 1.1.4
* @author William Lin
* @license The MIT License (MIT)
* @github https://github.com/ganlanyuan/tiny-slider/
Expand Down Expand Up @@ -1280,36 +1280,50 @@ var tns = (function () {
getInfo: info,
events: events,
goTo: function (targetIndex) {
var indexBase = (mode === 'gallery') ? 0 : cloneCount;
switch (targetIndex) {
case 'first':
targetIndex = indexBase;
break;
case 'last':
targetIndex = indexBase + slideCount - 1;
break;
case 'prev':
case 'previous':
targetIndex = index - 1;
break;
case 'next':
targetIndex = index + 1;
break;
}

// define: absolute index
// define: absolute target index
// define: task completed status, initilized to false
var absIndex = index%slideCount,
absTargetIndex = targetIndex%slideCount;

absTargetIndex,
completed = false;
// redefine: absolute index when it's minus
if (absIndex < 0) { absIndex += slideCount; }
if (absTargetIndex < 0) { absTargetIndex += slideCount; }

console.log(index, absTargetIndex, absIndex);
if (absIndex !== absTargetIndex) {
if (typeof targetIndex === 'string') {
switch (targetIndex) {
// run click functions when target is 'prev' or 'next'
// set completed status to true
case 'prev':
case 'previous':
onClickPrev();
completed = true;
break;
case 'next':
onClickNext();
completed = true;
break;
// set absolute target index
case 'first':
absTargetIndex = 0;
break;
case 'last':
absTargetIndex = slideCount - 1;
break;
}
} else {
// set absolute target index
absTargetIndex = targetIndex%slideCount;
if (absTargetIndex < 0) { absTargetIndex += slideCount; }
}

// when it's not completed and
// absolute index not equal to absolute target index
// => reset index and render
if (!completed && absIndex !== absTargetIndex) {
index += absTargetIndex - absIndex;
console.log(index);
checkIndex();

// render();
render();
}

},
Expand Down
2 changes: 1 addition & 1 deletion tests/index2.html
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@

var btn = document.querySelector('#goto');
btn.onclick = function () {
inner.goTo('prev');
inner.goTo(33);
}
});
</script>
Expand Down

0 comments on commit cdbef1c

Please sign in to comment.