Skip to content

Commit

Permalink
Merge pull request #25 from hsnaydd/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
hsnaydd authored May 7, 2019
2 parents dfcd31d + 36a624c commit a1192ca
Show file tree
Hide file tree
Showing 9 changed files with 3,151 additions and 1,799 deletions.
11 changes: 6 additions & 5 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"presets": ["es2015"],
// Remove the line below to enable ES2015 support.
// "only": "gulpfile.babel.js",
"retainLines": true
}
"presets": [
[
"@babel/preset-env"
]
]
}
3 changes: 3 additions & 0 deletions .browserslistrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
> 0.25%
not dead
ie >= 10
18 changes: 15 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,15 +225,27 @@ $ npm install

# To start the development server run:

$ gulp serve
$ yarn start

# Or so if using npm:

$ npm run start

# To lint your code run:

$ gulp scripts:lint
$ yarn lint

# Or so if using npm:

$ npm run lint

# To make a full new build run:

$ gulp build
$ yarn build

# Or so if using npm:

$ npm run build

# To run tests:

Expand Down
182 changes: 100 additions & 82 deletions dist/moveTo.js
Original file line number Diff line number Diff line change
@@ -1,106 +1,119 @@
/*!
* MoveTo - A lightweight scroll animation javascript library without any dependency.
* Version 1.8.0 (14-01-2019 13:15)
* Version 1.8.1 (07-05-2019 23:39)
* Licensed under MIT
* Copyright 2019 Hasan Aydoğdu <hsnaydd@gmail.com>
*/

'use strict';
"use strict";

var MoveTo = function () {
/**
* Defaults
* @type {object}
*/
* Defaults
* @type {object}
*/
var defaults = {
tolerance: 0,
duration: 800,
easing: 'easeOutQuart',
container: window,
callback: function callback() {} };


callback: function callback() {}
};
/**
* easeOutQuart Easing Function
* @param {number} t - current time
* @param {number} b - start value
* @param {number} c - change in value
* @param {number} d - duration
* @return {number} - calculated value
*/
* easeOutQuart Easing Function
* @param {number} t - current time
* @param {number} b - start value
* @param {number} c - change in value
* @param {number} d - duration
* @return {number} - calculated value
*/

function easeOutQuart(t, b, c, d) {
t /= d;
t--;
return -c * (t * t * t * t - 1) + b;
}

/**
* Merge two object
*
* @param {object} obj1
* @param {object} obj2
* @return {object} merged object
*/
* Merge two object
*
* @param {object} obj1
* @param {object} obj2
* @return {object} merged object
*/


function mergeObject(obj1, obj2) {
var obj3 = {};
Object.keys(obj1).forEach(function (propertyName) {
obj3[propertyName] = obj1[propertyName];
});

Object.keys(obj2).forEach(function (propertyName) {
obj3[propertyName] = obj2[propertyName];
});
return obj3;
};
}

;
/**
* Converts camel case to kebab case
* @param {string} val the value to be converted
* @return {string} the converted value
*/
* Converts camel case to kebab case
* @param {string} val the value to be converted
* @return {string} the converted value
*/

function kebabCase(val) {
return val.replace(/([A-Z])/g, function ($1) {
return '-' + $1.toLowerCase();
});
};
}

;
/**
* Count a number of item scrolled top
* @param {Window|HTMLElement} container
* @return {number}
*/
* Count a number of item scrolled top
* @param {Window|HTMLElement} container
* @return {number}
*/

function countScrollTop(container) {
if (container instanceof HTMLElement) {
return container.scrollTop;
}

return container.pageYOffset;
};
}

;
/**
* MoveTo Constructor
* @param {object} options Options
* @param {object} easeFunctions Custom ease functions
*/
function MoveTo() {var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};var easeFunctions = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
* MoveTo Constructor
* @param {object} options Options
* @param {object} easeFunctions Custom ease functions
*/

function MoveTo() {
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var easeFunctions = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
this.options = mergeObject(defaults, options);
this.easeFunctions = mergeObject({ easeOutQuart: easeOutQuart }, easeFunctions);
this.easeFunctions = mergeObject({
easeOutQuart: easeOutQuart
}, easeFunctions);
}

/**
* 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;
* 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) {
return;
}

var href = dom.getAttribute('href') || dom.getAttribute('data-target');
// The element to be scrolled
var target = href && href !== '#' ?
document.getElementById(href.substring(1)) :
document.body;
var href = dom.getAttribute('href') || dom.getAttribute('data-target'); // The element to be scrolled

var target = href && href !== '#' ? document.getElementById(href.substring(1)) : document.body;
var options = mergeObject(this.options, _getOptionsFromTriggerDom(dom, this.options));

if (typeof callback === 'function') {
Expand All @@ -109,34 +122,39 @@ var MoveTo = function () {

var listener = function listener(e) {
e.preventDefault();

_this.move(target, options);
};

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

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

/**
* Move
* Scrolls to given element by using easeOutQuart function
* @param {HTMLElement|number} target Target element to be scrolled or target position
* @param {object} options Custom options
*/
MoveTo.prototype.move = function (target) {var _this2 = this;var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
* Move
* Scrolls to given element by using easeOutQuart function
* @param {HTMLElement|number} target Target element to be scrolled or target position
* @param {object} options Custom options
*/


MoveTo.prototype.move = function (target) {
var _this2 = this;

var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};

if (target !== 0 && !target) {
return;
}

options = mergeObject(this.options, options);

var distance = typeof target === 'number' ? target : target.getBoundingClientRect().top;
var from = countScrollTop(options.container);
var startTime = null;
var lastYOffset = void 0;
distance -= options.tolerance;
var lastYOffset;
distance -= options.tolerance; // rAF loop

// rAF loop
var loop = function loop(currentTime) {
var currentYOffset = countScrollTop(_this2.options.container);

Expand All @@ -150,13 +168,11 @@ var MoveTo = function () {
var timeElapsed = currentTime - startTime;

if (lastYOffset) {
if (
distance > 0 && lastYOffset > currentYOffset ||
distance < 0 && lastYOffset < currentYOffset)
{
if (distance > 0 && lastYOffset > currentYOffset || distance < 0 && lastYOffset < currentYOffset) {
return options.callback(target);
}
}

lastYOffset = currentYOffset;

var val = _this2.easeFunctions[options.easing](timeElapsed, from, distance, options.duration);
Expand All @@ -173,27 +189,29 @@ var MoveTo = function () {

window.requestAnimationFrame(loop);
};

/**
* Adds custom ease function
* @param {string} name Ease function name
* @param {function} fn Ease function
*/
* Adds custom ease function
* @param {string} name Ease function name
* @param {function} fn Ease function
*/


MoveTo.prototype.addEaseFunction = function (name, fn) {
this.easeFunctions[name] = fn;
};

/**
* Returns options which created from trigger dom element
* @param {HTMLElement} dom Trigger dom element
* @param {object} options The instance's options
* @return {object} The options which created from trigger dom element
*/
* Returns options which created from trigger dom element
* @param {HTMLElement} dom Trigger dom element
* @param {object} options The instance's options
* @return {object} The options which created from trigger dom element
*/


function _getOptionsFromTriggerDom(dom, options) {
var domOptions = {};

Object.keys(options).forEach(function (key) {
var value = dom.getAttribute('data-mt-' + kebabCase(key));
var value = dom.getAttribute("data-mt-".concat(kebabCase(key)));

if (value) {
domOptions[key] = isNaN(value) ? value : parseInt(value, 10);
}
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.

4 changes: 2 additions & 2 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const banner = [


gulp.task('scripts:lint', (cb) => {
return gulp.src('src/scripts/**/*.js')
return gulp.src('src/**/*.js')
.pipe($.eslint())
.pipe($.eslint.format())
.pipe(browserSync.active ? $.util.noop() : $.eslint.failOnError());
Expand All @@ -45,7 +45,7 @@ gulp.task('scripts', ['scripts:lint'], () => {
.pipe(gulp.dest, 'dist/');

return gulp.src('src/**/*.js')
.pipe($.babel())
.pipe($.babel({presets: ['@babel/preset-env']}))
.pipe($.header(banner))
.pipe(gulp.dest('dist'))
.pipe(scriptsMinChannel());
Expand Down
Loading

0 comments on commit a1192ca

Please sign in to comment.