Created
July 9, 2019 05:57
-
-
Save RamakrishnaChilaka/77a4aa8aeccd7a122c31ce688fd936c5 to your computer and use it in GitHub Desktop.
Traverse through DOM tree and capture scrolling positions of each HTML element.
This file contains hidden or 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
// assuming jquery is present | |
import $ from 'jquery'; | |
export default function traverse(element, ser_obj) { | |
if (!element) { | |
return; | |
} | |
var that = makeid; | |
if (element.scrollTop() || element.scrollLeft()) { | |
if (!element.attr('id')) { | |
element.attr('id', that(5)); | |
} | |
ser_obj[element.attr('id')] = {}; | |
ser_obj[element.attr('id')] = { | |
top: element.scrollTop(), | |
left: element.scrollLeft(), | |
}; | |
} | |
var childrenElements = element.children(); | |
if (childrenElements && childrenElements.length) { | |
childrenElements.each(function () { | |
let childrenElement = $(this); | |
traverse(childrenElement, ser_obj); | |
}); | |
} | |
} | |
function makeid(length) { | |
let result = ''; | |
const characters = | |
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; | |
const charactersLength = characters.length; | |
for (let i = 0; i < length; i++) { | |
result += characters.charAt(Math.floor(Math.random() * charactersLength)); | |
} | |
return result; | |
} | |
// traverse($(':root')); | |
// console.log('ser_obj ', ser_obj); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment