-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(create): add
create
method for post-initialisation
- Loading branch information
Simon Finney
committed
Mar 30, 2018
1 parent
82a4a46
commit 457fab6
Showing
5 changed files
with
74 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,9 @@ | |
* @author Simon Finney <[email protected]> | ||
*/ | ||
|
||
import parallax from './parallax'; | ||
import { create, init } from './parallax'; | ||
|
||
// Runs the `parallax` method when the DOM content has loaded. | ||
document.addEventListener('DOMContentLoaded', parallax); | ||
document.addEventListener('DOMContentLoaded', init); | ||
|
||
export { create }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,17 +3,17 @@ | |
* @author Simon Finney <[email protected]> | ||
*/ | ||
|
||
const DEFAULT_VALUES = { | ||
MODIFIER: '.0625rem', | ||
SPEED: 0.5, | ||
}; | ||
|
||
const NAMESPACE = 'cpp'; | ||
|
||
export default { | ||
CUSTOM_PROPERTIES: { | ||
MODIFIER: `--${NAMESPACE}-modifier`, | ||
PARALLAX: `--${NAMESPACE}-parallax`, | ||
SPEED: `--${NAMESPACE}-speed`, | ||
}, | ||
DEFAULT_VALUES: { | ||
MODIFIER: '.0625rem', | ||
SPEED: 0.5, | ||
}, | ||
NAMESPACE, | ||
const CUSTOM_PROPERTIES = { | ||
MODIFIER: `--${NAMESPACE}-modifier`, | ||
PARALLAX: `--${NAMESPACE}-parallax`, | ||
SPEED: `--${NAMESPACE}-speed`, | ||
}; | ||
|
||
export { CUSTOM_PROPERTIES, DEFAULT_VALUES, NAMESPACE }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,59 +3,61 @@ | |
* @author Simon Finney <[email protected]> | ||
*/ | ||
|
||
import constants from './constants'; | ||
import { CUSTOM_PROPERTIES, DEFAULT_VALUES, NAMESPACE } from './constants'; | ||
|
||
/** | ||
* Checks for and otherwise sets default custom properties to the root element. | ||
* @param {string} property The property to check and set. | ||
*/ | ||
function setDefaultProperty(property) { | ||
const { documentElement } = document; | ||
const customProperty = constants.CUSTOM_PROPERTIES[property]; | ||
const customProperty = CUSTOM_PROPERTIES[property]; | ||
|
||
if ( | ||
window | ||
.getComputedStyle(documentElement) | ||
.getPropertyValue(customProperty) === '' | ||
) { | ||
documentElement.style.setProperty( | ||
customProperty, | ||
constants.DEFAULT_VALUES[property] | ||
); | ||
documentElement.style.setProperty(customProperty, DEFAULT_VALUES[property]); | ||
} | ||
} | ||
|
||
/** | ||
* Finds all of the elements declared as parallax elements and attaches the parallax handler to them. | ||
* Attaches the parallax handler to a specific element. | ||
* @param {HTMLElement} parallaxElement The element to attach the parallax handler to. | ||
*/ | ||
export default function parallax() { | ||
function create(parallaxElement) { | ||
const w = window; | ||
const d = document; | ||
|
||
Object.keys(constants.DEFAULT_VALUES).forEach(defaultProperty => | ||
setDefaultProperty(defaultProperty) | ||
parallaxElement.style.setProperty( | ||
'transform', | ||
`translate3d(0, calc(var(${CUSTOM_PROPERTIES.PARALLAX}) * var(${ | ||
CUSTOM_PROPERTIES.MODIFIER | ||
})), 0)` | ||
); | ||
|
||
d.querySelectorAll(`[data-${constants.NAMESPACE}]`).forEach(element => { | ||
const parallaxElement = element; | ||
parallaxElement.style.setProperty('will-change', 'transform'); | ||
|
||
w.addEventListener('scroll', () => { | ||
parallaxElement.style.setProperty( | ||
'transform', | ||
`translate3d(0, calc(var(${constants.CUSTOM_PROPERTIES.PARALLAX}) * var(${ | ||
constants.CUSTOM_PROPERTIES.MODIFIER | ||
})), 0)` | ||
CUSTOM_PROPERTIES.PARALLAX, | ||
document.documentElement.scrollTop * | ||
w | ||
.getComputedStyle(parallaxElement) | ||
.getPropertyValue(CUSTOM_PROPERTIES.SPEED) | ||
); | ||
|
||
parallaxElement.style.setProperty('will-change', 'transform'); | ||
|
||
w.addEventListener('scroll', () => { | ||
parallaxElement.style.setProperty( | ||
constants.CUSTOM_PROPERTIES.PARALLAX, | ||
d.documentElement.scrollTop * | ||
w | ||
.getComputedStyle(parallaxElement) | ||
.getPropertyValue(constants.CUSTOM_PROPERTIES.SPEED) | ||
); | ||
}); | ||
}); | ||
} | ||
|
||
/** | ||
* Finds all of the elements declared as parallax elements and creates a parallax handler to each of them. | ||
*/ | ||
function init() { | ||
Object.keys(DEFAULT_VALUES).forEach(defaultProperty => | ||
setDefaultProperty(defaultProperty) | ||
); | ||
|
||
document.querySelectorAll(`[data-${NAMESPACE}]`).forEach(create); | ||
} | ||
|
||
export { create, init }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters