Skip to content

Commit

Permalink
Refactored DomHelpers getViewportData method. Refactored windowDimens…
Browse files Browse the repository at this point in the history
…ions for viewportSize.
  • Loading branch information
Mikel Tuesta committed Oct 18, 2016
1 parent d55212a commit d4d9ab8
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 42 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "foes-scrollproxy",
"version": "0.1.1",
"version": "0.2.0",
"license": "MIT",
"description": "",
"keywords": [
Expand Down
13 changes: 5 additions & 8 deletions src/Core/ScrollProxy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* @author Mikel Tuesta <mikel@lin3s.com>
* @author Mikel Tuesta <mikeltuesta@gmail.com>
*/
'use strict';

Expand All @@ -16,10 +16,7 @@
function ScrollProxy() {
var latestKnownScrollY = window.pageYOffset,
animating = false,
windowDimensions = {
height: -1,
width: -1
},
viewportSize = new DomHelpers.ViewportSize(-1, -1),
listeners = [];

var notifyListeners = function () {
Expand All @@ -45,8 +42,8 @@
requestFrame();
};
var onResize = function () {
windowDimensions = DomHelpers.getWindowDimensions();
notifyListeners('onResize', windowDimensions);
viewportSize = DomHelpers.getViewportSize();
notifyListeners('onResize', viewportSize);
};
var bindListeners = function () {
window.addEventListener('scroll', onScroll);
Expand All @@ -65,7 +62,7 @@
}
};
var init = function () {
windowDimensions = DomHelpers.getWindowDimensions();
viewportSize = DomHelpers.getViewportSize();
bindListeners();
};
init();
Expand Down
8 changes: 4 additions & 4 deletions src/Core/ScrollProxyListener.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* @author Mikel Tuesta <mikel@lin3s.com>
* @author Mikel Tuesta <mikeltuesta@gmail.com>
*/
'use strict';

Expand All @@ -19,16 +19,16 @@
(function(window, DomHelpers, undefined) {

function ScrollProxyListener() {
this.windowDimensions = DomHelpers.getWindowDimensions();
this.viewportSize = DomHelpers.getViewportSize();
this.latestKnownScrollY = undefined;
this.doFrame = function () {

};
this.onScroll = function (latestKnownScrollY) {
this.latestKnownScrollY = latestKnownScrollY;
};
this.onResize = function (windowDimensions) {
this.windowDimensions = windowDimensions;
this.onResize = function (viewportSize) {
this.viewportSize = viewportSize;
};
}

Expand Down
46 changes: 38 additions & 8 deletions src/Helpers/DomHelpers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* @author Mikel Tuesta <mikel@lin3s.com>
* @author Mikel Tuesta <mikeltuesta@gmail.com>
*/
'use strict';

Expand All @@ -14,26 +14,56 @@
return Math.max(body.scrollHeight, body.offsetHeight, html.clientHeight, html.scrollHeight, html.offsetHeight);
};

DomHelpers.getWindowDimensions = function () {
/**
*
* @returns {ViewportSize}
*/
DomHelpers.getViewportSize = function () {
var w = window,
d = document,
e = d.documentElement,
g = d.getElementsByTagName('body')[0];

return {
height: w.innerHeight || e.clientHeight || g.clientHeight,
width: w.innerWidth || e.clientWidth || g.clientWidth
}
return new DomHelpers.ViewportSize(
w.innerHeight || e.clientHeight || g.clientHeight,
w.innerWidth || e.clientWidth || g.clientWidth
);
};

DomHelpers.getViewportData = function (element, windowHeight) {
/**
*
* @param element
* @param viewportSize | Size object
* @returns {{isInViewport: boolean, rect: ClientRect}}
*/

DomHelpers.getViewportData = function (element, viewportSize) {
var rect = element.getBoundingClientRect();
return {
isInViewport: ((rect.top >= 0 && rect.top <= windowHeight) || (rect.bottom >= 0 && rect.bottom <= windowHeight)),
isInViewport: (
// Vertically
(rect.top >= 0 && rect.top <= viewportSize.height) ||
(rect.bottom >= 0 && rect.bottom <= viewportSize.height)
) && (
// Horizontally
(rect.left >= 0 && rect.left <= viewportSize.width) ||
(rect.right >= 0 && rect.right <= viewportSize.width)
),
rect: rect
};
};

/**
*
* @param height
* @param width
* @constructor
*/
DomHelpers.ViewportSize = function(height, width) {
this.height = height;
this.width = width;
};

// Expose DomHelpers
window.DomHelpers = DomHelpers;

Expand Down
12 changes: 6 additions & 6 deletions src/Plugins/ScrollImageParallaxListener.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* @author Mikel Tuesta <mikel@lin3s.com>
* @author Mikel Tuesta <mikeltuesta@gmail.com>
*/
'use strict';

Expand Down Expand Up @@ -38,19 +38,19 @@
this.doFrame = function() {
this.$images.each((function (index, el) {
var $el = $(el);
var inViewportData = DomHelpers.getViewportData($el[0], this.windowDimensions.height);
var inViewportData = DomHelpers.getViewportData($el[0], this.viewportSize);
if (inViewportData.isInViewport) {
var maxTranslate = (this.scale - 1) * this.imagesHeights[index],
translate = this.direction * inViewportData.rect.top / this.windowDimensions.height * maxTranslate;
translate = this.direction * inViewportData.rect.top / this.viewportSize.height * maxTranslate;
TweenLite.to($el, .5, { y: Math.floor(translate) });
}
}).bind(this));
};

this.onResize = function(windowDimensions) {
this.windowDimensions = windowDimensions;
this.onResize = function(viewportSize) {
this.viewportSize = viewportSize;
// Check breakpoint changed
var breakpoint = this.getNearestBreakpoint(this.windowDimensions.width);
var breakpoint = this.getNearestBreakpoint(this.viewportSize.width);
if (this.currentBreakpoint !== breakpoint) {
this.currentBreakpoint = breakpoint;
this.init();
Expand Down
9 changes: 5 additions & 4 deletions src/Plugins/ScrollStickyListener.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* @author Mikel Tuesta <mikel@lin3s.com>
* @author Mikel Tuesta <mikeltuesta@gmail.com>
*/
'use strict';

Expand Down Expand Up @@ -35,7 +35,7 @@
this.stickyHeight = this.$sticky.height();

this.maxStickyTranslate = Math.max(this.containerHeight - this.stickyHeight - this.stickyTopOffset - this.stickyBottomOffset, 0);
this.stickyOffsetLeft = Math.floor(DomHelpers.getViewportData(this.$sticky.get(0)).rect.left);
this.stickyOffsetLeft = Math.floor(DomHelpers.getViewportData(this.$sticky.get(0), this.viewportSize).rect.left);

this.onScroll();
this.doFrame();
Expand All @@ -57,10 +57,11 @@
};

this.onScroll = function (latestKnownScrollY) {
this.containerOffsetTop = Math.floor(DomHelpers.getViewportData(this.$container.get(0)).rect.top);
this.containerOffsetTop = Math.floor(DomHelpers.getViewportData(this.$container.get(0), this.viewportSize).rect.top);
};

this.onResize = function(windowDimensions) {
this.onResize = function(viewportSize) {
this.viewportSize = viewportSize;
this.init();
};

Expand Down
16 changes: 8 additions & 8 deletions src/Plugins/ScrollStickyWithContentsListener.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* @author Mikel Tuesta <mikel@lin3s.com>
* @author Mikel Tuesta <mikeltuesta@gmail.com>
*/
'use strict';

Expand Down Expand Up @@ -51,19 +51,19 @@
this.stickyWidth = this.$sticky.parent().outerWidth();

this.maxStickyTranslate = Math.max(this.containerHeight - this.stickyHeight - this.stickyTopOffset - this.stickyBottomOffset, 0);
this.stickyOffsetLeft = Math.floor(DomHelpers.getViewportData(this.$sticky.get(0)).rect.left);
this.stickyOffsetLeft = Math.floor(DomHelpers.getViewportData(this.$sticky.get(0), this.viewportSize).rect.left);

this.onScroll();
this.doFrame();
};

this.doFrame = function () {
var stickyTranslate = 0,
stickyExceedsViewport = this.stickyHeight > this.windowDimensions.height;
stickyExceedsViewport = this.stickyHeight > this.viewportSize.height;

if (this.containerOffsetTop - this.triggerOffset < 0) {
var absContainerOffsetTop = Math.abs(this.containerOffsetTop),
maxStickyInnerTranslateY = this.stickyHeight - this.windowDimensions.height + this.stickyTopOffset + this.stickyBottomOffset,
maxStickyInnerTranslateY = this.stickyHeight - this.viewportSize.height + this.stickyTopOffset + this.stickyBottomOffset,
currentStickyTop = this.stickyRect.top;
stickyTranslate = absContainerOffsetTop >= this.maxStickyTranslate ? this.maxStickyTranslate : absContainerOffsetTop;

Expand Down Expand Up @@ -95,12 +95,12 @@
this.latestKnownScrollYDelta = this.latestKnownScrollY - latestKnownScrollY;
this.latestKnownScrollY = latestKnownScrollY;

this.stickyRect = DomHelpers.getViewportData(this.$sticky.get(0)).rect;
this.containerOffsetTop = Math.floor(DomHelpers.getViewportData(this.$container.get(0)).rect.top);
this.stickyRect = DomHelpers.getViewportData(this.$sticky.get(0), this.viewportSize).rect;
this.containerOffsetTop = Math.floor(DomHelpers.getViewportData(this.$container.get(0), this.viewportSize).rect.top);
};

this.onResize = function(windowDimensions) {
this.windowDimensions = windowDimensions;
this.onResize = function(viewportSize) {
this.viewportSize = viewportSize;
this.init();
};

Expand Down
6 changes: 3 additions & 3 deletions src/Plugins/ScrollTextParallaxListener.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* @author Mikel Tuesta <mikel@lin3s.com>
* @author Mikel Tuesta <mikeltuesta@gmail.com>
*/
'use strict';

Expand All @@ -21,9 +21,9 @@
this.doFrame = function() {
this.$texts.each((function (index, el) {
var $el = $(el);
var inViewportData = DomHelpers.getViewportData($el[0], this.windowDimensions.height);
var inViewportData = DomHelpers.getViewportData($el[0], this.viewportSize);
if (inViewportData.isInViewport) {
var translate = this.direction * inViewportData.rect.top / this.windowDimensions.height * this.maxTranslate;
var translate = this.direction * inViewportData.rect.top / this.viewportSize.height * this.maxTranslate;
TweenLite.to($el, .5, { y: translate });
}
}).bind(this))
Expand Down

0 comments on commit d4d9ab8

Please sign in to comment.