orxapi.tools.toscroll
Version:
The TypeScript toScroll tools library for orxapi
57 lines • 1.98 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var $ = require("jquery");
/**
* Scroll to target position
* @param target
* @param offset
* @param duration
*/
function toScroll(_a) {
var target = _a.target, _b = _a.offset, offset = _b === void 0 ? 0 : _b, _c = _a.duration, duration = _c === void 0 ? 500 : _c;
var top = getPosition(target, "offset").top;
$("html, body").animate({ scrollTop: top + offset }, duration);
}
exports.toScroll = toScroll;
/**
* Handler for scrolling to target position
* @see toScroll
* @param evt
*/
function handleToScroll(evt) {
evt.preventDefault();
var elem = $(evt.currentTarget);
var _a = elem.data(), offset = _a.scrollOffset, duration = _a.scrollDuration, anchor = _a.scrollTarget;
var href = $(evt.currentTarget).attr("href") || anchor;
var target = $(href);
toScroll({ target: target, offset: offset, duration: duration });
}
exports.handleToScroll = handleToScroll;
/**
* Scroll to the target form hash into url
* @see toScroll
* @param hash
* @param target
* @param offset
* @param duration
*/
function toScrollFromUrl(_a) {
var hash = _a.hash, target = _a.target, _b = _a.offset, offset = _b === void 0 ? 0 : _b, _c = _a.duration, duration = _c === void 0 ? 500 : _c;
if (hash === window.location.hash) {
toScroll({ target: target, offset: offset, duration: duration });
}
}
exports.toScrollFromUrl = toScrollFromUrl;
/**
* Always return a coordinate object
* @param obj The JQuery object
* @param functionName Position by default or offset type
* @returns {{top: number, left: number}}
*/
function getPosition(obj, functionName) {
if (functionName === void 0) { functionName = "position"; }
// TODO fix TS7015 noImplicitAny
return obj.length ? obj[functionName]() : { top: 0, left: 0 };
}
exports.getPosition = getPosition;
//# sourceMappingURL=toScroll.js.map