transcroll
Advanced tools
Comparing version 1.0.2 to 1.0.4
76
index.js
@@ -1,15 +0,15 @@ | ||
export const easings = { | ||
linear: t => t, | ||
easeInQuad: t => t * t, | ||
easeOutQuad: t => t * (2 - t), | ||
easeInOutQuad: t => t < 0.5 ? 2 * t * t : -1 + (4 - 2 * t) * t, | ||
easeInCubic: t => t * t * t, | ||
easeOutCubic: t => (--t) * t * t + 1, | ||
easeInOutCubic: t => t < 0.5 ? 4 * t * t * t : (t - 1) * (2 * t - 2) * (2 * t - 2) + 1, | ||
easeInQuart: t => t * t * t * t, | ||
easeOutQuart: t => 1 - (--t) * t * t * t, | ||
easeInOutQuart: t => t < 0.5 ? 8 * t * t * t * t : 1 - 8 * (--t) * t * t * t, | ||
easeInQuint: t => t * t * t * t * t, | ||
easeOutQuint: t => 1 + (--t) * t * t * t * t, | ||
easeInOutQuint: t => t < 0.5 ? 16 * t * t * t * t * t : 1 + 16 * (--t) * t * t * t * t | ||
var easings = { | ||
linear: function (t) { return t; }, | ||
easeInQuad: function (t) { return t * t; }, | ||
easeOutQuad: function (t) { return t * (2 - t); }, | ||
easeInOutQuad: function (t) { return t < 0.5 ? 2 * t * t : -1 + (4 - 2 * t) * t; }, | ||
easeInCubic: function (t) { return t * t * t; }, | ||
easeOutCubic: function (t) { return (--t) * t * t + 1; }, | ||
easeInOutCubic: function (t) { return t < 0.5 ? 4 * t * t * t : (t - 1) * (2 * t - 2) * (2 * t - 2) + 1; }, | ||
easeInQuart: function (t) { return t * t * t * t; }, | ||
easeOutQuart: function (t) { return 1 - (--t) * t * t * t; }, | ||
easeInOutQuart: function (t) { return t < 0.5 ? 8 * t * t * t * t : 1 - 8 * (--t) * t * t * t; }, | ||
easeInQuint: function (t) { return t * t * t * t * t; }, | ||
easeOutQuint: function (t) { return 1 + (--t) * t * t * t * t; }, | ||
easeInOutQuint: function (t) { return t < 0.5 ? 16 * t * t * t * t * t : 1 + 16 * (--t) * t * t * t * t; } | ||
}; | ||
@@ -30,3 +30,10 @@ | ||
*/ | ||
export default (target, { el = window, duration = 200, easing = easings.easeInQuad, jump = 0.5, interrupt = true } = {}) => { | ||
function transcroll(target, ref) { | ||
if ( ref === void 0 ) ref = {}; | ||
var el = ref.el; if ( el === void 0 ) el = window; | ||
var duration = ref.duration; if ( duration === void 0 ) duration = 200; | ||
var easing = ref.easing; if ( easing === void 0 ) easing = easings.easeInQuad; | ||
var jump = ref.jump; if ( jump === void 0 ) jump = 0.5; | ||
var interrupt = ref.interrupt; if ( interrupt === void 0 ) interrupt = true; | ||
function getPosition() { | ||
@@ -36,7 +43,7 @@ return el === window ? window.pageYOffset : el.scrollTop; | ||
function setPosition(v) { | ||
if (el === window) window.scroll(0, v); | ||
else el.scrollTop = v; | ||
if (el === window) { window.scroll(0, v); } | ||
else { el.scrollTop = v; } | ||
} | ||
function maxPosition() { | ||
const element = el === window ? window.document.documentElement : el; | ||
var element = el === window ? window.document.documentElement : el; | ||
return element.scrollHeight - element.clientHeight; | ||
@@ -48,5 +55,5 @@ } | ||
function getPageOffset(element) { | ||
if (element === undefined) element = el; | ||
if (element === window) return 0; | ||
let top = 0, | ||
if (element === undefined) { element = el; } | ||
if (element === window) { return 0; } | ||
var top = 0, | ||
_el = element; | ||
@@ -59,3 +66,3 @@ do { | ||
} | ||
const targetPosition = Math.min( | ||
var targetPosition = Math.min( | ||
typeof target === 'number' && target || | ||
@@ -67,3 +74,3 @@ typeof target === 'string' && (getPageOffset(select(target)) - getPageOffset()) || | ||
const data = { | ||
var data = { | ||
interrupted: false, | ||
@@ -78,3 +85,3 @@ jumped: false | ||
let startPosition = getPosition(), | ||
var startPosition = getPosition(), | ||
positionDiff = startPosition - targetPosition, | ||
@@ -89,21 +96,24 @@ startTime = 'now' in window.performance ? performance.now() : new Date().getTime(); | ||
if (typeof easing === 'string') easing = easings[easing]; | ||
if (typeof easing === 'string') { easing = easings[easing]; } | ||
return new Promise(resolve => { | ||
return new Promise(function (resolve) { | ||
function stop(e) { | ||
if (e) data.interrupted = true; | ||
if (e) { data.interrupted = true; } | ||
el.removeEventListener('mousewheel', stop); | ||
resolve(data); | ||
} | ||
if (interrupt) el.addEventListener('mousewheel', stop); | ||
if (interrupt) { el.addEventListener('mousewheel', stop); } | ||
(function scroll() { | ||
if (data.interrupted) return; | ||
const now = 'now' in window.performance ? performance.now() : new Date().getTime(), | ||
if (data.interrupted) { return; } | ||
var now = 'now' in window.performance ? performance.now() : new Date().getTime(), | ||
time = Math.min(1, ((now - startTime) / duration)), | ||
fn = easing(time); | ||
setPosition(fn * (targetPosition - startPosition) + startPosition); | ||
if (time === 1) stop(); | ||
else requestAnimationFrame(scroll); | ||
if (time === 1) { stop(); } | ||
else { requestAnimationFrame(scroll); } | ||
})(); | ||
}); | ||
}; | ||
} | ||
module.exports = transcroll; | ||
module.exports.easings = easings; |
{ | ||
"name": "transcroll", | ||
"version": "1.0.2", | ||
"version": "1.0.4", | ||
"description": "Animate scroll in a modern way", | ||
@@ -9,3 +9,9 @@ "homepage": "https://github.com/mrTimofey/transcroll", | ||
"author": "Timofey Tiunov <timoxa17@gmail.com>", | ||
"license": "MIT" | ||
"license": "MIT", | ||
"scripts": { | ||
"build": "buble src.js > index.js" | ||
}, | ||
"devDependencies": { | ||
"buble": "^0.19.3" | ||
} | ||
} |
6115
103
1