Created
March 25, 2016 19:23
-
-
Save mimiflynn/0b85f0ef88cce3072054 to your computer and use it in GitHub Desktop.
various view utilities in ES6
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
const updateActive = (node) => { | |
// remove active class from all items | |
Array.prototype.forEach.call(node.parentNode.children, (e) => { | |
e.className = e.className.split(' active').join(''); | |
}); | |
// add active class to correct item | |
node.className = node.className + ' active'; | |
} | |
const locationFromTop = (element) => { | |
let rect = element.getBoundingClientRect(); | |
let positionTop = rect.top + document.body.scrollTop; | |
return positionTop + 1; | |
} | |
// returns object | |
const getElementLocation = (className) => { | |
let elements = document.getElementsByClassName(className); | |
let elementDetails = {}; | |
Array.prototype.forEach.call(elements, (e) => { | |
let positionTop = locationFromTop(e); | |
let newClassName = e.className.split(className).join(''); | |
elementDetails[e.title] = { positionTop, newClassName }; | |
}); | |
return elementDetails; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment