Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
hsnaydd committed Mar 27, 2017
2 parents 8d94847 + e126bf4 commit 2bf5588
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 10 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,24 @@ Ease function. See [Easing Equations](http://gizma.com/easing/) for more ease fu
```
</details>

<details>
<summary>Unregister a trigger</summary>

```js
document.addEventListener('DOMContentLoaded', function () {
const moveTo = new MoveTo();

const trigger = document.getElementsByClassName('js-trigger')[0];

// Register a trigger
const unregister = moveTo.registerTrigger(trigger, { duration: 500 });

// Unregister a trigger
unregister();
});
```
</details>

## Development setup

```sh
Expand Down
11 changes: 8 additions & 3 deletions dist/moveTo.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*!
* MoveTo - A lightweight scroll animation javascript library without any dependency.
* Version 1.5.3 (24-03-2017 14:41)
* Version 1.6.0 (27-03-2017 10:55)
* Licensed under MIT
* Copyright 2017 Hasan Aydoğdu <hsnaydd@gmail.com>
*/
Expand Down Expand Up @@ -94,6 +94,7 @@ var MoveTo = function () {
* Register a dom element as trigger
* @param {HTMLElement} dom Dom trigger element
* @param {function} callback Callback function
* @return {function|void} unregister function
*/
MoveTo.prototype.registerTrigger = function (dom, callback) {var _this = this;
if (!dom) {
Expand All @@ -111,10 +112,14 @@ var MoveTo = function () {
options.callback = callback;
}

dom.addEventListener('click', function (e) {
var listener = function listener(e) {
e.preventDefault();
_this.move(target, options);
});
};

dom.addEventListener('click', listener, false);

return function () {return dom.removeEventListener('click', listener, false);};
};

/**
Expand Down
4 changes: 2 additions & 2 deletions dist/moveTo.min.js

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "moveto",
"version": "1.5.3",
"version": "1.6.0",
"description": "A lightweight scroll animation javascript library without any dependency.",
"main": "dist/moveTo.js",
"scripts": {
Expand Down
9 changes: 7 additions & 2 deletions src/moveTo.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ const MoveTo = (() => {
* Register a dom element as trigger
* @param {HTMLElement} dom Dom trigger element
* @param {function} callback Callback function
* @return {function|void} unregister function
*/
MoveTo.prototype.registerTrigger = function(dom, callback) {
if (!dom) {
Expand All @@ -104,10 +105,14 @@ const MoveTo = (() => {
options.callback = callback;
}

dom.addEventListener('click', (e) => {
const listener = (e) => {
e.preventDefault();
this.move(target, options);
});
};

dom.addEventListener('click', listener, false);

return () => dom.removeEventListener('click', listener, false);
};

/**
Expand Down
15 changes: 13 additions & 2 deletions tests/moveTo-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ function createMockDomElement(options) {
offsetParent: null,
addEventListener: function(event, cb) {
this._listeners[event] = cb;
},
removeEventListener: function(event, cb) {
delete this._listeners[event];
}
};
}
Expand Down Expand Up @@ -65,15 +68,23 @@ test('It should change options', (t) => {

test('It should register trigger', (t) => {
const instance = new MoveTo();

const elem = createMockDomElement({});
t.is(elem._listeners['click'], undefined);

instance.registerTrigger(elem);

t.true(typeof elem._listeners['click'] === 'function');
});

test('It should unregister trigger', (t) => {
const instance = new MoveTo();
const elem = createMockDomElement({});
const unregister = instance.registerTrigger(elem);

t.true(typeof elem._listeners['click'] === 'function');
unregister();
t.true(typeof elem._listeners['click'] === 'undefined');
});

test('It should add custom ease function', (t) => {
const inst = new MoveTo();
const elem = createMockDomElement({});
Expand Down

0 comments on commit 2bf5588

Please sign in to comment.