From 07103c8f73242d58759d75965b73831acc477823 Mon Sep 17 00:00:00 2001 From: Todd Beauchamp Date: Mon, 5 Dec 2016 15:20:18 -0500 Subject: [PATCH] chore(refactor): renaming and reorg --- dist/ScrollMagnetContainer.vue | 32 ++++++++++++++++++-------------- dist/ScrollMagnetItem.vue | 34 +++++++++++++++++++--------------- dist/vue-scroll-magnet.min.js | 4 ++-- package.json | 2 +- src/ScrollMagnetContainer.vue | 32 ++++++++++++++++++-------------- src/ScrollMagnetItem.vue | 34 +++++++++++++++++++--------------- 6 files changed, 77 insertions(+), 61 deletions(-) diff --git a/dist/ScrollMagnetContainer.vue b/dist/ScrollMagnetContainer.vue index acf6941..955addf 100644 --- a/dist/ScrollMagnetContainer.vue +++ b/dist/ScrollMagnetContainer.vue @@ -19,7 +19,7 @@ }; }, props: { - targetElement: { + boundsElementSelector: { type: String, default: '', required: false, @@ -32,16 +32,17 @@ mounted() { // Assign a target element to the container. This element will be used as a reference // for the height of the scrollable area. - if (!this.targetElement) { + if (!this.boundsElementSelector) { // If no prop is provided, use the parent element of this.target = this.$el.parentElement; } else { // Find the element by its selector and assign it as the target - const $el = document.querySelector(this.targetElement); + const $el = document.querySelector(this.boundsElementSelector); if ($el) { this.target = $el; } } + this.getElementPosition(); this.attachMutationObserver(); }, @@ -58,7 +59,8 @@ attachScroll() { if (typeof window !== 'undefined') { window.addEventListener('scroll', () => { - this.recalcAttributes(); + this.getScrollPosition(); + this.getElementPosition(); }); } }, @@ -76,7 +78,8 @@ attachResize() { if (typeof window !== 'undefined') { window.addEventListener('resize', () => { - this.recalcAttributes(); + this.getScrollPosition(); + this.getElementPosition({ recalcWidth: true, recalcHeight: true }); }); } }, @@ -103,7 +106,7 @@ this.mutationObserver = new MutationObserver(() => { // When the reference element changes, we must recompute the scroll attributes - this.getElementPosition({ recalcHeight: true }); + this.getElementPosition({ recalcWidth: true, recalcHeight: true }); // The child magnet item listens for changes to this.scrollTop in this component // to update itself. this.scrollTop is manually adjusted so that the child updates @@ -127,26 +130,27 @@ }, /** * Get the container's dimensions and offset and update data attributes + * @param {object} options Configuration object for options */ getElementPosition(options) { + const recalcWidth = (options && options.recalcWidth) || false; const recalcHeight = (options && options.recalcHeight) || false; - const viewportOffset = this.$el.getBoundingClientRect(); - const scrollY = this.getScrollY(); - this.offsetTop = viewportOffset.top + scrollY; - this.width = this.$el.clientWidth; + this.offsetTop = this.$el.getBoundingClientRect().top + this.getScrollY(); + + if (!this.width > 0 || recalcWidth) { + this.width = (this.$el && this.$el.clientWidth) || 0; + } if (!this.height > 0 || recalcHeight) { this.height = (this.target && this.target.clientHeight) || 0; } }, /** - * Recalculate the scroll container's attributes such as current - * scroll position, offsetTop, and height + * Recalculate the scroll container's position */ - recalcAttributes() { + getScrollPosition() { this.scrollTop = this.getScrollY(); - this.getElementPosition({ recalcHeight: true }); this.offsetTop = this.$el.getBoundingClientRect().top + this.getScrollY(); }, /** diff --git a/dist/ScrollMagnetItem.vue b/dist/ScrollMagnetItem.vue index 2a3da4e..2895743 100644 --- a/dist/ScrollMagnetItem.vue +++ b/dist/ScrollMagnetItem.vue @@ -1,7 +1,7 @@