MCPcopy
hub / github.com/electerious/basicScroll / relativeToAbsoluteValue

Function relativeToAbsoluteValue

src/scripts/main.js:134–153  ·  view source on GitHub ↗
(value, elem, scrollTop = getScrollTop(), viewportHeight = getViewportHeight())

Source from the content-addressed store, hash-verified

132 * @returns {String} value - Absolute value.
133 */
134const relativeToAbsoluteValue = function(value, elem, scrollTop = getScrollTop(), viewportHeight = getViewportHeight()) {
135
136 const elemSize = elem.getBoundingClientRect()
137
138 const elemAnchor = value.match(/^[a-z]+/)[0]
139 const viewportAnchor = value.match(/[a-z]+$/)[0]
140
141 let y = 0
142
143 if (viewportAnchor === 'top') y -= 0
144 if (viewportAnchor === 'middle') y -= viewportHeight / 2
145 if (viewportAnchor === 'bottom') y -= viewportHeight
146
147 if (elemAnchor === 'top') y += (elemSize.top + scrollTop)
148 if (elemAnchor === 'middle') y += (elemSize.top + scrollTop) + elemSize.height / 2
149 if (elemAnchor === 'bottom') y += (elemSize.top + scrollTop) + elemSize.height
150
151 return `${ y }px`
152
153}
154
155/**
156 * Validates data and sets defaults for undefined properties.

Callers 1

validateFunction · 0.85

Calls 2

getScrollTopFunction · 0.85
getViewportHeightFunction · 0.85

Tested by

no test coverage detected