Created
August 15, 2012 08:34
-
-
Save aisensiy/3357647 to your computer and use it in GitHub Desktop.
Javascript: hide fix elements when add a big cover element on the page
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
var res = []; | |
/** | |
* Try to hide elements with style fixed | |
*/ | |
function hide_fix() { | |
var possibilities = ['[style*="position:fixed"],[style*="position: fixed"]'], | |
searchFor = /\bposition:\s*fixed;/, | |
cssProp = 'position', | |
cssValue = 'fixed', | |
styles = document.styleSheets, | |
i, j, l, rules, rule, elem; | |
for (i = 0; i < styles.length; i++) { | |
rules = styles[i].cssRules; | |
if(!rules) return; | |
l = rules.length; | |
for (j = 0; j < l; j++) { | |
rule = rules[j]; | |
if (searchFor.test(rule.cssText)) { | |
possibilities.push(rule.selectorText); | |
} | |
} | |
} | |
possibilities = possibilities.join(','); | |
possibilities = document.querySelectorAll(possibilities); | |
l = possibilities.length; | |
for (i = 0; i < l; i++) { | |
elem = possibilities[i]; | |
// Test whether the element is really position:fixed | |
if (window.getComputedStyle(elem, null).getPropertyValue(cssProp) === cssValue) { | |
res.push(elem); | |
} | |
} | |
for (i = 0, l = res.length; i < l; i++) { | |
res[i].setAttribute('data-style', res[i].style.visibility); | |
res[i].style.visibility = 'hidden'; | |
} | |
} | |
/** | |
* Recovery the elements style which are be marked with style fixed. | |
*/ | |
function show_fix() { | |
var i, l; | |
for (i = 0, l = res.length; i < l; i++) { | |
res[i].style.visibility = res[i].getAttribute('data-style'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment