How do I disable position:fixed in web pages?

丶灬走出姿态 提交于 2019-12-05 02:08:31

These two questions on stackoverflow and superuser are very similar.

Arguably, the easiest solution (suggested in the answers on superuser) is to install this greasemonkey script. It has the advantage that it tries to be clever by looping only over relevent html elements and attempting to recognise pseudo-pop-up windows, and not unfixing those. It will automatically run on all loaded webpages unless you edit the @include header line. (I have not actually tested it.)

If you want a bookmarklet, then this should do the job (tested on nautil.us):

javascript:(function(){x=document.querySelectorAll('*');for(i=0;i<x.length;i++){if(getComputedStyle(x[i]).position=="fixed"){x[i].style.position="absolute";}}}())

As a bonus, considering that css position: sticky headers are now also popping up their ugly heads, you can get rid of both them and "fixed" elements:

javascript:(function(){x=document.querySelectorAll('*');for(i=0;i<x.length;i++){elementStyle=getComputedStyle(x[i]);if(elementStyle.position=="fixed"||elementStyle.position=="sticky"){x[i].style.position="absolute";}}}())

Not compressed:

var x = document.querySelectorAll('*');
for(var i=0; i<x.length; i++) {
    elementStyle = getComputedStyle(x[i]);
    if(elementStyle.position=="fixed" || elementStyle.position=="sticky") {
        x[i].style.position="absolute";
    }
}
Scimonster

I found this answer which tells how to find elements with a specific CSS property (using jQuery):

var x = $('.myselector').filter(function () { 
    return this.style.position == 'fixed' 
});

If we then use that returned set, we can reset the positions to static:

var x = $('*').filter(function () { 
    return this.style.position == 'fixed' 
}).css('position', 'static');

Just place this in a userscript (with jQuery included) and you're all set!


And a non-jQuery solution...

var x = document.querySelectorAll('*'); // get all elements
for (var i = 0; i< x.length; i++) {
    if (x[i].style.position == 'fixed') {
        x[i].style.position = 'static';
    }
}

Thank you, Scimonster. I used your jQuery solution, but altered it to query for the header tag (and imported jQuery):

var jq = document.createElement('script');
jq.src = "https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js";
document.getElementsByTagName('head')[0].appendChild(jq);

$("header").css('position', 'absolute'); 

This is great:

https://alisdair.mcdiarmid.org/kill-sticky-headers/

A sticky header is a sure sign of stuff you don't want on your screen, and this just destroys them!

Could probably modify it to change them from fixed to static, but I haven't found a case where I actually want to.

Update again again

A Vanilla JS solution:

/* find the topbar by coordinate */
var topbar
var el = document.elementFromPoint(document.documentElement.clientWidth - 200, 20)
while (el) {
    if (getComputedStyle(el).position == 'fixed') topbar = el
    el = el.parentNode;
    if (el == document.body) break
}
if (topbar == undefined) return

/* disable position:fixed */
// topbar.style.position = 'absolute'
// ↑ this line doesn't work well, because sometime offsetParent is not <body>
// ↓ workaround
var paint = function (enforce) {
    var scrollTop = document.documentElement.scrollTop || document.body.scrollTop
    var threshold = 200
    if (!enforce && scrollTop > threshold * 3) return
    var offset = scrollTop / threshold
    if (offset > 1.2) offset = 1.2
    topbar.style.transform = 'translateY(-' + offset * 100 + '%)'
}
paint(true) // initialize
document.addEventListener('scroll', () => paint())

/* when use JS to frequently change CSS value, disable CSS transition to avoid weird delay */
// topbar.style.transition = 'transform 0s'
// ↑ this line doesn't work because of compatibility
// ↓ workaround
topbar.classList.add('remove-topbar')
var style = document.createElement('style')
style.innerHTML = '.remove-topbar{transition:transform 0s !important}'
document.head.appendChild(style)

Userscript: https://gist.github.com/zcyzcy88/909b77054b88fd69e8a0834b84e7a68b

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!