@hilemangroup/bp-frontend
Version:
Common shared frontend utilities for boilerplate projects
65 lines (52 loc) • 1.5 kB
JavaScript
+(function ($) {
var historySupported = !!window.history.pushState;
var settings = {
context: 'html, body',
duration: 800,
offset: 0
};
function getOffset($ctx, $elem, options) {
return $elem.offset().top - options.offset;
}
function toHash(hash, options) {
var $elem = $(hash);
var $ctx;
if (!$elem.length) { return; }
options = $.extend({}, settings, options);
$ctx = $(options.context);
$ctx.animate(
{
scrollTop: getOffset($ctx, $elem, options)
},
options.duration,
function () {
if (historySupported) {
window.history.replaceState(null, '', hash);
}
if (typeof options.callback === 'function') {
options.callback();
}
}
);
}
function toElem(elem, options) {
var $elem = $(elem);
var $ctx;
if (!$elem.length) { return; }
options = $.extend({}, settings, options);
$ctx = $(options.context);
$ctx.animate(
{
scrollTop: getOffset($ctx, $elem, options)
},
options.duration,
(typeof options.callback === 'function')
? options.callback
: null
);
}
$.scrollto = {
elem: toElem,
hash: toHash
};
})(jQuery);