vuetify
Version:
Vue.js 2 Semantic Component Framework
60 lines (48 loc) • 1.77 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = goTo;
var _console = require('../../../util/console');
/**
* Modified from https://github.com/alamcordeiro/vue-smooth-scroll
*/
function easeInOutCubic(t) {
return t < 0.5 ? 4 * t * t * t : (t - 1) * (2 * t - 2) * (2 * t - 2) + 1;
}
function goTo(target) {
var offset = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
var duration = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 500;
if (typeof window === 'undefined') return;
if (offset && isNaN(offset)) {
(0, _console.consoleError)('Offset must be a Number, received ' + offset.constructor.name + ' instead.');
return;
}
if (duration && isNaN(duration)) {
(0, _console.consoleError)('Duration must be a Number, received ' + duration.constructor.name + ' instead.');
return;
}
var end = void 0;
if (target instanceof Element) {
end = target.getBoundingClientRect().top + window.pageYOffset;
} else if (typeof target === 'string') {
end = document.querySelector(target).getBoundingClientRect().top + window.pageYOffset;
} else if (typeof target === 'number') {
end = target;
} else {
(0, _console.consoleError)('Target must be a String/Number/DOMElement, received ' + target.constructor.name + ' instead.');
return;
}
end += offset;
var start = performance.now();
function step(now) {
var elapsed = now - start;
var position = end;
if (elapsed < duration) {
position = window.pageYOffset + (end - window.pageYOffset) * easeInOutCubic(elapsed / duration);
window.requestAnimationFrame(step);
}
window.scroll(0, position);
}
window.requestAnimationFrame(step);
}