Comparing version 1.7.4 to 1.8.0
/*! | ||
* MoveTo - A lightweight scroll animation javascript library without any dependency. | ||
* Version 1.7.4 (28-09-2018 16:02) | ||
* Version 1.8.0 (14-01-2019 13:15) | ||
* Licensed under MIT | ||
* Copyright 2018 Hasan Aydoğdu <hsnaydd@gmail.com> | ||
* Copyright 2019 Hasan Aydoğdu <hsnaydd@gmail.com> | ||
*/ | ||
@@ -18,2 +18,3 @@ | ||
easing: 'easeOutQuart', | ||
container: window, | ||
callback: function callback() {} }; | ||
@@ -67,2 +68,14 @@ | ||
/** | ||
* 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 | ||
@@ -123,5 +136,5 @@ * @param {object} options Options | ||
var distance = typeof target === 'number' ? target : target.getBoundingClientRect().top; | ||
var from = window.pageYOffset; | ||
var from = countScrollTop(options.container); | ||
var startTime = null; | ||
var lastPageYOffset = void 0; | ||
var lastYOffset = void 0; | ||
distance -= options.tolerance; | ||
@@ -131,3 +144,3 @@ | ||
var loop = function loop(currentTime) { | ||
var currentPageYOffset = window.pageYOffset; | ||
var currentYOffset = countScrollTop(_this2.options.container); | ||
@@ -143,6 +156,6 @@ if (!startTime) { | ||
if (lastPageYOffset) { | ||
if (lastYOffset) { | ||
if ( | ||
distance > 0 && lastPageYOffset > currentPageYOffset || | ||
distance < 0 && lastPageYOffset < currentPageYOffset) | ||
distance > 0 && lastYOffset > currentYOffset || | ||
distance < 0 && lastYOffset < currentYOffset) | ||
{ | ||
@@ -152,14 +165,12 @@ return options.callback(target); | ||
} | ||
lastPageYOffset = currentPageYOffset; | ||
lastYOffset = currentYOffset; | ||
var val = _this2.easeFunctions[options.easing]( | ||
timeElapsed, from, distance, options.duration); | ||
var val = _this2.easeFunctions[options.easing](timeElapsed, from, distance, options.duration); | ||
options.container.scroll(0, val); | ||
window.scroll(0, val); | ||
if (timeElapsed < options.duration) { | ||
window.requestAnimationFrame(loop); | ||
} else { | ||
window.scroll(0, distance + from); | ||
options.container.scroll(0, distance + from); | ||
options.callback(target); | ||
@@ -166,0 +177,0 @@ } |
/*! | ||
* MoveTo - A lightweight scroll animation javascript library without any dependency. | ||
* Version 1.7.4 (28-09-2018 16:02) | ||
* Version 1.8.0 (14-01-2019 13:15) | ||
* Licensed under MIT | ||
* Copyright 2018 Hasan Aydoğdu <hsnaydd@gmail.com> | ||
* Copyright 2019 Hasan Aydoğdu <hsnaydd@gmail.com> | ||
*/ | ||
"use strict";var MoveTo=function(){var n={tolerance:0,duration:800,easing:"easeOutQuart",callback:function(){}};function o(t,e,n,o){return t/=o,-n*(--t*t*t*t-1)+e}function l(e,n){var o={};return Object.keys(e).forEach(function(t){o[t]=e[t]}),Object.keys(n).forEach(function(t){o[t]=n[t]}),o}function t(){var t=0<arguments.length&&void 0!==arguments[0]?arguments[0]:{},e=1<arguments.length&&void 0!==arguments[1]?arguments[1]:{};this.options=l(n,t),this.easeFunctions=l({easeOutQuart:o},e)}return t.prototype.registerTrigger=function(t,e){var n=this;if(t){var o,i,r,a=t.getAttribute("href")||t.getAttribute("data-target"),u=a&&"#"!==a?document.getElementById(a.substring(1)):document.body,c=l(this.options,(o=t,i=this.options,r={},Object.keys(i).forEach(function(t){var e=o.getAttribute("data-mt-"+t.replace(/([A-Z])/g,function(t){return"-"+t.toLowerCase()}));e&&(r[t]=isNaN(e)?e:parseInt(e,10))}),r));"function"==typeof e&&(c.callback=e);var s=function(t){t.preventDefault(),n.move(u,c)};return t.addEventListener("click",s,!1),function(){return t.removeEventListener("click",s,!1)}}},t.prototype.move=function(r){var a=this,u=1<arguments.length&&void 0!==arguments[1]?arguments[1]:{};if(0===r||r){u=l(this.options,u);var c="number"==typeof r?r:r.getBoundingClientRect().top,s=window.pageYOffset,f=null,d=void 0;c-=u.tolerance;window.requestAnimationFrame(function t(e){var n=window.pageYOffset;f||(f=e-1);var o=e-f;if(d&&(0<c&&n<d||c<0&&d<n))return u.callback(r);d=n;var i=a.easeFunctions[u.easing](o,s,c,u.duration);window.scroll(0,i),o<u.duration?window.requestAnimationFrame(t):(window.scroll(0,c+s),u.callback(r))})}},t.prototype.addEaseFunction=function(t,e){this.easeFunctions[t]=e},t}();"undefined"!=typeof module?module.exports=MoveTo:window.MoveTo=MoveTo; | ||
"use strict";var MoveTo=function(){var n={tolerance:0,duration:800,easing:"easeOutQuart",container:window,callback:function(){}};function o(t,e,n,o){return t/=o,-n*(--t*t*t*t-1)+e}function v(e,n){var o={};return Object.keys(e).forEach(function(t){o[t]=e[t]}),Object.keys(n).forEach(function(t){o[t]=n[t]}),o}function d(t){return t instanceof HTMLElement?t.scrollTop:t.pageYOffset}function t(){var t=0<arguments.length&&void 0!==arguments[0]?arguments[0]:{},e=1<arguments.length&&void 0!==arguments[1]?arguments[1]:{};this.options=v(n,t),this.easeFunctions=v({easeOutQuart:o},e)}return t.prototype.registerTrigger=function(t,e){var n=this;if(t){var o,i,r,a=t.getAttribute("href")||t.getAttribute("data-target"),c=a&&"#"!==a?document.getElementById(a.substring(1)):document.body,u=v(this.options,(o=t,i=this.options,r={},Object.keys(i).forEach(function(t){var e=o.getAttribute("data-mt-"+t.replace(/([A-Z])/g,function(t){return"-"+t.toLowerCase()}));e&&(r[t]=isNaN(e)?e:parseInt(e,10))}),r));"function"==typeof e&&(u.callback=e);var s=function(t){t.preventDefault(),n.move(c,u)};return t.addEventListener("click",s,!1),function(){return t.removeEventListener("click",s,!1)}}},t.prototype.move=function(r){var a=this,c=1<arguments.length&&void 0!==arguments[1]?arguments[1]:{};if(0===r||r){c=v(this.options,c);var u="number"==typeof r?r:r.getBoundingClientRect().top,s=d(c.container),f=null,l=void 0;u-=c.tolerance;window.requestAnimationFrame(function t(e){var n=d(a.options.container);f||(f=e-1);var o=e-f;if(l&&(0<u&&n<l||u<0&&l<n))return c.callback(r);l=n;var i=a.easeFunctions[c.easing](o,s,u,c.duration);c.container.scroll(0,i),o<c.duration?window.requestAnimationFrame(t):(c.container.scroll(0,u+s),c.callback(r))})}},t.prototype.addEaseFunction=function(t,e){this.easeFunctions[t]=e},t}();"undefined"!=typeof module?module.exports=MoveTo:window.MoveTo=MoveTo; |
{ | ||
"name": "moveto", | ||
"version": "1.7.4", | ||
"version": "1.8.0", | ||
"description": "A lightweight scroll animation javascript library without any dependency.", | ||
@@ -5,0 +5,0 @@ "main": "dist/moveTo.js", |
@@ -64,3 +64,4 @@ # MoveTo [![npm version](https://badge.fury.io/js/moveto.svg)](https://badge.fury.io/js/moveto) [![Bower version](https://badge.fury.io/bo/moveTo.svg)](https://badge.fury.io/bo/moveTo) [![CDNJS version](https://img.shields.io/cdnjs/v/moveTo.svg)](https://cdnjs.com/libraries/moveTo) [![Build Status](https://travis-ci.org/hsnaydd/moveTo.svg?branch=master)](https://travis-ci.org/hsnaydd/moveTo.js) | ||
duration: 800, | ||
easing: 'easeOutQuart' | ||
easing: 'easeOutQuart', | ||
container: window | ||
}) | ||
@@ -74,2 +75,3 @@ ``` | ||
| easing | easeOutQuart | Ease function name | | ||
| container | window | The container been computed and scrolled | ||
| callback | noop | The function to be run after scrolling complete. Target passes as the first argument | | ||
@@ -76,0 +78,0 @@ |
@@ -11,2 +11,3 @@ | ||
easing: 'easeOutQuart', | ||
container: window, | ||
callback: function() {}, | ||
@@ -60,2 +61,14 @@ }; | ||
/** | ||
* 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 | ||
@@ -116,5 +129,5 @@ * @param {object} options Options | ||
let distance = typeof target === 'number' ? target : target.getBoundingClientRect().top; | ||
const from = window.pageYOffset; | ||
const from = countScrollTop(options.container); | ||
let startTime = null; | ||
let lastPageYOffset; | ||
let lastYOffset; | ||
distance -= options.tolerance; | ||
@@ -124,3 +137,3 @@ | ||
const loop = (currentTime) => { | ||
let currentPageYOffset = window.pageYOffset; | ||
let currentYOffset = countScrollTop(this.options.container); | ||
@@ -136,6 +149,6 @@ if (!startTime) { | ||
if (lastPageYOffset) { | ||
if (lastYOffset) { | ||
if ( | ||
(distance > 0 && lastPageYOffset > currentPageYOffset) || | ||
(distance < 0 && lastPageYOffset < currentPageYOffset) | ||
(distance > 0 && lastYOffset > currentYOffset) || | ||
(distance < 0 && lastYOffset < currentYOffset) | ||
) { | ||
@@ -145,9 +158,7 @@ return options.callback(target); | ||
} | ||
lastPageYOffset = currentPageYOffset; | ||
lastYOffset = currentYOffset; | ||
const val = this.easeFunctions[options.easing]( | ||
timeElapsed, from, distance, options.duration | ||
); | ||
const val = this.easeFunctions[options.easing](timeElapsed, from, distance, options.duration); | ||
window.scroll(0, val); | ||
options.container.scroll(0, val); | ||
@@ -157,3 +168,3 @@ if (timeElapsed < options.duration) { | ||
} else { | ||
window.scroll(0, distance + from); | ||
options.container.scroll(0, distance + from); | ||
options.callback(target); | ||
@@ -160,0 +171,0 @@ } |
@@ -49,2 +49,3 @@ /* eslint-disable require-jsdoc */ | ||
t.not(inst.options.callback, undefined); | ||
t.not(inst.options.container, undefined); | ||
}); | ||
@@ -152,1 +153,30 @@ | ||
test.serial.cb('It should scroll to target position inside an element', (t) => { | ||
const container = document.createElement('div'); | ||
document.body.appendChild(container); | ||
const inst = new MoveTo({ | ||
container: container | ||
}); | ||
const calls = []; | ||
// mock scroll. | ||
const originalScroll = container.scroll; | ||
container.scroll = function(_, y) { | ||
calls.push(y); | ||
}; | ||
inst.move(1500); | ||
setTimeout(() => { | ||
// revert scroll. | ||
container.scroll = originalScroll; | ||
t.is(calls[calls.length - 1], 1500); | ||
t.end(); | ||
}, 1000); | ||
}); | ||
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
230687
18
694
254