diff --git a/README.md b/README.md index 6774a16..75fd2ec 100644 --- a/README.md +++ b/README.md @@ -272,6 +272,10 @@ The data object can include the following properties: */ from: null, to: null, + /* + * Position offset. + */ + offset: 0, /* * Direct mode. */ @@ -365,6 +369,12 @@ Examples: } ``` +## Position offset + +Type: `Integer` Default: `0` Optional: `true` + +offset position of `from` and `to` values by `n` pixels + ### Direct mode Type: `Boolean|Node` Default: `false` Optional: `true` @@ -551,4 +561,4 @@ Examples: - Keep the amount of instances low. More instances means more checks, calculations and style changes. - Don't animate everything at once and don't animate too many properties. Browsers don't like this. - Smooth animations by adding a short transition to the element: `transform: translateY(var(--ty)); transition: transform .1s`. -- basicScroll applies all [props](#props) globally by default. Try to reuse variables across elements instead of creating more instances. \ No newline at end of file +- basicScroll applies all [props](#props) globally by default. Try to reuse variables across elements instead of creating more instances. diff --git a/src/scripts/main.js b/src/scripts/main.js index 6d38de6..8714781 100755 --- a/src/scripts/main.js +++ b/src/scripts/main.js @@ -131,14 +131,14 @@ const mapDirectToProperty = function(direct, properties) { * @param {?Integer} viewportHeight - Height of the viewport. * @returns {String} value - Absolute value. */ -const relativeToAbsoluteValue = function(value, elem, scrollTop = getScrollTop(), viewportHeight = getViewportHeight()) { +const relativeToAbsoluteValue = function(value, elem, offset, scrollTop = getScrollTop(), viewportHeight = getViewportHeight()) { const elemSize = elem.getBoundingClientRect() const elemAnchor = value.match(/^[a-z]+/)[0] const viewportAnchor = value.match(/[a-z]+$/)[0] - let y = 0 + let y = 0 + offset if (viewportAnchor==='top') y -= 0 if (viewportAnchor==='middle') y -= viewportHeight / 2 @@ -172,6 +172,7 @@ const validate = function(data = {}) { if (data.direct!==true && data.direct instanceof HTMLElement===false) data.direct = false if (data.track!==false) data.track = true + if (data.offset==null) data.offset = 0 if (typeof data.inside!=='function') throw new Error('Property `inside` must be a function') if (typeof data.outside!=='function') throw new Error('Property `outside` must be a function') @@ -183,8 +184,8 @@ const validate = function(data = {}) { } else { - if (isRelativeValue(data.from)===true) data.from = relativeToAbsoluteValue(data.from, data.elem) - if (isRelativeValue(data.to)===true) data.to = relativeToAbsoluteValue(data.to, data.elem) + if (isRelativeValue(data.from)===true) data.from = relativeToAbsoluteValue(data.from, data.elem, data.offset) + if (isRelativeValue(data.to)===true) data.to = relativeToAbsoluteValue(data.to, data.elem, data.offset) } @@ -420,6 +421,14 @@ export const create = function(data) { } + // Offsets the instance + const _offset = () => { + + // Define offset in pixels + offset = 0 + + } + // Assign instance to a variable so the instance can be used // elsewhere in the current function. const instance = { @@ -429,7 +438,8 @@ export const create = function(data) { update: _update, start: _start, stop: _stop, - destroy: _destroy + destroy: _destroy, + offset: _offset } // Store instance in global array and save the index @@ -458,4 +468,4 @@ window.addEventListener('resize', debounce(() => { }) -}, 50)) \ No newline at end of file +}, 50))