From 650741702936c2ff1902ed011fd33e9b4b10068d Mon Sep 17 00:00:00 2001 From: janschoenherr Date: Wed, 26 Sep 2018 11:10:36 +0200 Subject: [PATCH] perf: Improve Sticky performance --- CHANGELOG.md | 1 + src/js/core/sticky.js | 33 ++++++++++++++++----------------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bf699f2b05..c352867e76 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ - Viewport Height component no longer forces `box-sizing: border-box` - Improve Tooltip performance - Improve Parallax performance +- Improve Sticky performance ### Removed diff --git a/src/js/core/sticky.js b/src/js/core/sticky.js index 87d010b106..c0a0b7afc1 100644 --- a/src/js/core/sticky.js +++ b/src/js/core/sticky.js @@ -1,5 +1,5 @@ import Class from '../mixin/class'; -import {$, addClass, after, Animation, assign, attr, css, fastdom, hasClass, height, isNumeric, isString, isVisible, noop, offset, query, remove, removeClass, replaceClass, scrollTop, toFloat, toggleClass, trigger, within} from 'uikit-util'; +import {$, addClass, after, Animation, assign, attr, css, fastdom, hasClass, height, isNumeric, isString, isUndefined, isVisible, noop, offset, query, remove, removeClass, replaceClass, scrollTop, toFloat, toggleClass, trigger, within} from 'uikit-util'; export default { @@ -50,18 +50,13 @@ export default { }, connected() { - this.placeholder = $('+ .uk-sticky-placeholder', this.$el) || $('
'); - - if (!this.isActive) { - this.hide(); - } }, disconnected() { if (this.isActive) { - this.isActive = false; + this.isActive = undefined; this.hide(); removeClass(this.selTarget, this.clsInactive); } @@ -134,14 +129,23 @@ export default { { read({height}) { + + this.topOffset = offset(this.isActive ? this.placeholder : this.$el).top; + this.bottomOffset = this.topOffset + height; + + const bottom = parseProp('bottom', this); + + this.top = Math.max(toFloat(parseProp('top', this)), this.topOffset) - this.offset; + this.bottom = bottom && bottom - height; + this.inactive = this.media && !window.matchMedia(this.media).matches; + return { - top: offset(this.isActive ? this.placeholder : this.$el).top, height: !this.isActive ? this.$el.offsetHeight : height, margins: css(this.$el, ['marginTop', 'marginBottom', 'marginLeft', 'marginRight']) }; }, - write({height, top, margins}) { + write({height, margins}) { const {placeholder} = this; @@ -152,14 +156,9 @@ export default { attr(placeholder, 'hidden', ''); } - this.topOffset = top; - this.bottomOffset = this.topOffset + height; - - const bottom = parseProp('bottom', this); - - this.top = Math.max(toFloat(parseProp('top', this)), this.topOffset) - this.offset; - this.bottom = bottom && bottom - height; - this.inactive = this.media && !window.matchMedia(this.media).matches; + if (isUndefined(this.isActive)) { + this.hide(); + } },