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 18, 2017
2 parents 32da396 + 49cb3c9 commit 48dda89
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 200 deletions.
2 changes: 1 addition & 1 deletion demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">
<link rel="stylesheet" type="text/css" href="./demo.css">

<script src="../dist/moveTo.js"></script>
<script src="../dist/moveTo.min.js"></script>
<script>
document.addEventListener('DOMContentLoaded', function(){
const easeFunctions = {
Expand Down
202 changes: 98 additions & 104 deletions dist/moveTo.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*!
* MoveTo - A lightweight, smooth scrolling javascript library without any dependency.
* Version 1.5.0 (18-03-2017 17:24)
* Version 1.5.1 (18-03-2017 19:23)
* Licensed under MIT
* Copyright 2017 Hasan Aydoğdu <hsnaydd@gmail.com>
*/

'use strict';var _createClass = function () {function defineProperties(target, props) {for (var i = 0; i < props.length; i++) {var descriptor = props[i];descriptor.enumerable = descriptor.enumerable || false;descriptor.configurable = true;if ("value" in descriptor) descriptor.writable = true;Object.defineProperty(target, descriptor.key, descriptor);}}return function (Constructor, protoProps, staticProps) {if (protoProps) defineProperties(Constructor.prototype, protoProps);if (staticProps) defineProperties(Constructor, staticProps);return Constructor;};}();function _classCallCheck(instance, Constructor) {if (!(instance instanceof Constructor)) {throw new TypeError("Cannot call a class as a function");}}
'use strict';
var MoveTo = function () {
/**
* Defaults
Expand Down Expand Up @@ -85,124 +85,118 @@ var MoveTo = function () {
};

/**
* Scrolls to an element
*/var
MoveTo = function () {

/**
* Constructer
* @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] : {};_classCallCheck(this, MoveTo);
this.options = mergeObject(defaults, options);
this.easeFunctions = mergeObject({ easeOutQuart: easeOutQuart }, easeFunctions);
* 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);
}

/**
* Register a dom element as trigger
* @param {HTMLElement} dom Dom trigger element
* @param {function} callback Callback function
*/
MoveTo.prototype.registerTrigger = function (dom, callback) {var _this = this;
if (!dom) {
return;
}

/**
* Register a dom element as trigger
* @param {HTMLElement} dom Dom trigger element
* @param {function} callback Callback function
*/_createClass(MoveTo, [{ key: 'registerTrigger', value: function registerTrigger(
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)) :
0;
var options = mergeObject(this.options, _getOptionsFromTriggerDom(dom, this.options));

var href = dom.getAttribute('href') || dom.getAttribute('data-target');
// The element to be scrolled
var target = href && href !== '#' ?
document.getElementById(href.substring(1)) :
0;
var options = mergeObject(this.options, _getOptionsFromTriggerDom(dom, this.options));
if (typeof callback === 'function') {
options.callback = callback;
}

if (typeof callback === 'function') {
options.callback = callback;
}
dom.addEventListener('click', function (e) {
e.preventDefault();
_this.move(target, options);
});
};

/**
* 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 to = typeof target === 'number' ? target : getOffsetSum(target).top;
var from = window.pageYOffset;
to -= options.tolerance;
var change = to - from;
var startTime = null;
var lastPageYOffset = 0;

// rAF loop
var loop = function loop(currentTime) {
var currentPageYOffset = window.pageYOffset;

dom.addEventListener('click', function (e) {
e.preventDefault();
_this.move(target, options);
});
if (!startTime) {
// To starts time from 1, we subtracted -1 from current time
// If time starts from 1 The first loop will not do anything,
// because easing value will be zero
startTime = currentTime - 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
*/ }, { key: 'move', value: function move(
target) {var _this2 = this;var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
if (target !== 0 && !target) {
return;
var timeElapsed = currentTime - startTime;

if (lastPageYOffset !== 0) {
if (
lastPageYOffset === currentPageYOffset ||
change > 0 && lastPageYOffset > currentPageYOffset ||
change < 0 && lastPageYOffset < currentPageYOffset)
{
return options.callback(target);
}
}
lastPageYOffset = currentPageYOffset;

var val = _this2.easeFunctions[options.easing](
timeElapsed, from, change, options.duration);

options = mergeObject(this.options, options);

var to = typeof target === 'number' ? target : getOffsetSum(target).top;
var from = window.pageYOffset;
to -= options.tolerance;
var change = to - from;
var startTime = null;
var lastPageYOffset = 0;

// rAF loop
var loop = function loop(currentTime) {
var currentPageYOffset = window.pageYOffset;

if (!startTime) {
// To starts time from 1, we subtracted -1 from current time
// If time starts from 1 The first loop will not do anything,
// because easing value will be zero
startTime = currentTime - 1;
}

var timeElapsed = currentTime - startTime;

if (lastPageYOffset !== 0) {
if (
lastPageYOffset === currentPageYOffset ||
change > 0 && lastPageYOffset > currentPageYOffset ||
change < 0 && lastPageYOffset < currentPageYOffset)
{
return options.callback(target);
}
}
lastPageYOffset = currentPageYOffset;

var val = _this2.easeFunctions[options.easing](
timeElapsed, from, change, options.duration);


window.scroll(0, val);

if (timeElapsed < options.duration) {
window.requestAnimationFrame(loop);
} else {
window.scroll(0, to);
options.callback(target);
}
};

window.scroll(0, val);

if (timeElapsed < options.duration) {
window.requestAnimationFrame(loop);
} else {
window.scroll(0, to);
options.callback(target);
}
};

/**
* Adds custom ease function
* @param {string} name Ease function name
* @param {function} fn Ease function
*/ }, { key: 'addEaseFunction', value: function addEaseFunction(
name, fn) {
this.easeFunctions[name] = fn;
} }]);return MoveTo;}();
window.requestAnimationFrame(loop);
};

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

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.0",
"version": "1.5.1",
"description": "A lightweight, smooth scrolling javascript library without any dependency.",
"main": "src/moveTo.js",
"scripts": {
Expand Down
Loading

0 comments on commit 48dda89

Please sign in to comment.