问题
I find elements on a web page that have been positioned fixed to be in my way frequently. I'd like to find a way to disable position: fixed CSS rules in any web site that I visit.
I wrote a userscript (Firefox, Greasemonkey) that scans every node in the document and figures out if it has a computed style position fixed, then overrides that to be static.
Is there a better way to accomplish my goal?
This is the script I wrote, I've narrowed it to just divs for now:
Array.forEach(
document.querySelectorAll("div")
,function(el) {
if (window.getComputedStyle(el).position === 'fixed') {
el.style.position = 'static';
}
}
);
回答1:
If your Greasemonkey script works, it is probably the most cost effective way to eliminate fixed-positioned styling.
Some alternatives that require much more effort but will use less CPU/memory per page:
Write an Add-on that:
- Deletes CSS style rules as they are loaded. (Greasemonkey cannot always do this because of cross-domain issues.)
- Uses Mutation Observers to intercept javascript attempts to set
position: fixed.
Fork and compile your own version of Firefox that ignores
position: fixed. You'd probably want this controlled by both a "blacklist" and a "whitelist".
来源:https://stackoverflow.com/questions/13696100/greasemonkey-script-to-make-fixed-positioned-elements-static