For example I want to find all elements that have computed style position: fixed;. How to do it without making much load on the CPU ?
Is iterating every getElementsByTagName('*') and then doing for loop the only way ?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Instead of selecting all (
*) elements, and usegetComputedStyle+getPropertyValue, you can follow the following steps:Loop through all CSS rules (via
document.styleSheets[1]) and take the selectors which containsposition: fixed.Select all elements whose
styleattribute containsposition: fixed.Use
document.querySelectorAllto select all elements which match the selector.window.getComputedStyle(elem, null).getPropertyValue('position')equalsfixedto filter elements which are not at a fixed position (possibly overridden through a more specific selector, or!important).At this point, you have an array containing all
position: fixedelements.[1]The external stylesheets have to be located at the same origin, because of the Same origin policy.Code (small demo: http://jsfiddle.net/GtXpw/):