Skip to content

Instantly share code, notes, and snippets.

@zcyzcy88
Last active January 25, 2018 17:38
Show Gist options
  • Save zcyzcy88/909b77054b88fd69e8a0834b84e7a68b to your computer and use it in GitHub Desktop.
Save zcyzcy88/909b77054b88fd69e8a0834b84e7a68b to your computer and use it in GitHub Desktop.
Remove `position: fixed`
// ==UserScript==
// @name Remove topbar
// @namespace zcyzcy88
// @include *
// @exclude https://twitter.com/*
// @exclude https://www.youtube.com/*
// @exclude https://cloud.google.com/*
// @exclude https://developers.google.com/*
// @grant none
// ==/UserScript==
(function () {
/* 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)
})()
@Nightblade
Copy link

@zcyzcy88
Copy link
Author

@Nightblade dynamic added topbar won't be detected

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment