//roll up a little to show the titles
if (this.options.deepLinkSmudge) {
var _this = this;
- onLoad(function() {
+ onLoad($(window), function() {
var offset = _this.$element.offset();
$('html, body').animate({ scrollTop: offset.top }, _this.options.deepLinkSmudgeDelay);
});
_this._updateActive();
});
- onLoad(function () {
- _this.$element.on({
- 'resizeme.zf.trigger': _this.reflow.bind(_this),
- 'scrollme.zf.trigger': _this._updateActive.bind(_this)
- }).on('click.zf.magellan', 'a[href^="#"]', function (e) {
- e.preventDefault();
- var arrival = _this.getAttribute('href');
- _this.scrollToLoc(arrival);
- });
+ _this.onLoadListener = onLoad($(window), function () {
+ _this.$element
+ .on({
+ 'resizeme.zf.trigger': _this.reflow.bind(_this),
+ 'scrollme.zf.trigger': _this._updateActive.bind(_this)
+ })
+ .on('click.zf.magellan', 'a[href^="#"]', function (e) {
+ e.preventDefault();
+ var arrival = _this.getAttribute('href');
+ _this.scrollToLoc(arrival);
+ });
});
this._deepLinkScroll = function(e) {
var hash = this.$active[0].getAttribute('href');
window.location.hash.replace(hash, '');
}
- $(window).off('hashchange', this._deepLinkScroll);
+
+ $(window)
+ .off('hashchange', this._deepLinkScroll)
+ .off(this.onLoadListener);
}
}
_setMQChecker() {
var _this = this;
- onLoad(function () {
+ this.onLoadListener = onLoad($(window), function () {
if (MediaQuery.atLeast(_this.options.revealOn)) {
_this.reveal(true);
}
this.close();
this.$element.off('.zf.trigger .zf.offCanvas');
this.$overlay.off('.zf.offCanvas');
+ $(window).off(this.onLoadListener);
}
}
}
this._events();
if (this.options.deepLink && window.location.hash === ( `#${this.id}`)) {
- onLoad(() => this.open());
+ this.onLoadListener = onLoad($(window), () => this.open());
}
}
}
this.$element.hide().off();
this.$anchor.off('.zf');
- $(window).off(`.zf.reveal:${this.id}`);
+ $(window)
+ .off(`.zf.reveal:${this.id}`)
+ .off(this.onLoadListener);
if ($('.reveal:visible').length === 0) {
this._removeRevealOpenClasses(); // also remove .is-reveal-open from the html element when there is no opened reveal
this.scrollCount = this.options.checkEvery;
this.isStuck = false;
- onLoad(function () {
+ this.onLoadListener = onLoad($(window), function () {
//We calculate the container height to have correct values for anchor points offset calculation.
_this.containerHeight = _this.$element.css("display") == "none" ? 0 : _this.$element[0].getBoundingClientRect().height;
_this.$container.css('height', _this.containerHeight);
if (this.$anchor && this.$anchor.length) {
this.$anchor.off('change.zf.sticky');
}
- $(window).off(this.scrollListener);
+ $(window)
+ .off(this.scrollListener)
+ .off(this.onLoadListener);
if (this.wasWrapped) {
this.$element.unwrap();
}
if(isActive && _this.options.autoFocus){
- onLoad(function() {
+ _this.onLoadListener = onLoad($(window), function() {
$('html, body').animate({ scrollTop: $elem.offset().top }, _this.options.deepLinkSmudgeDelay, () => {
$link.focus();
});
$(window).off('hashchange', this._checkDeepLink);
}
+ $(window).off(this.onLoadListener);
}
}
}
/**
- * Call the given function once the window is loaded,
- * or immediately if the window is already loaded.
+ * Return an event type to listen for window load.
+ *
+ * If `$elem` is passed, an event will be triggered on `$elem`. If window is already loaded, the event will still be triggered.
+ * If `handler` is passed, attach it to the event on `$elem`.
+ * Calling `onLoad` without handler allows you to get the event type that will be triggered before attaching the handler by yourself.
* @function
*
- * @param {Function} fn - function to call on window load.
+ * @param {Object} [] $elem - jQuery element on which the event will be triggered if passed.
+ * @param {Function} [] handler - function to attach to the event.
+ * @returns {String} - event type that should or will be triggered.
*/
-function onLoad(fn) {
- if (document.readyState === 'complete')
- setTimeout(() => fn(), 0);
- else
- $(window).one('load', () => fn());
+function onLoad($elem, handler) {
+ const didLoad = document.readyState === 'complete';
+ const eventType = (didLoad ? '_didLoad' : 'load') + '.zf.util.onLoad';
+ const cb = () => $elem.triggerHandler(eventType);
+
+ if ($elem) {
+ if (handler) $elem.one(eventType, handler);
+
+ if (didLoad)
+ setTimeout(cb);
+ else
+ $(window).one('load', cb);
+ }
+
+ return eventType;
}
export {rtl, GetYoDigits, RegExpEscape, transitionend, onLoad};
if (typeof($.triggersInitialized) === 'undefined') {
let $document = $(document);
- onLoad(function () {
+ onLoad($(window), function () {
Triggers.Initializers.addSimpleListeners();
Triggers.Initializers.addGlobalListeners();
});