Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[scrollery] window.scrollTop does not exist #2

Open
ErikFontanel opened this issue Jun 23, 2017 · 3 comments
Open

[scrollery] window.scrollTop does not exist #2

ErikFontanel opened this issue Jun 23, 2017 · 3 comments

Comments

@ErikFontanel
Copy link

I found a small cross-browser issue in scrollery.js. If I add data-scrollery=body to the body element, the event listener is added to window, but the window.scrollTop attribute does not exist. However document.documentElement.scrollTop does work.

Example:
Open http://csspl.us/test/scrollery.html in Firefox 54.0

@tomhodgins
Copy link
Owner

tomhodgins commented Jun 23, 2017

I'm not sure this one is solvable - when the page scrolls some browsers consider it <body> scrolling and some consider it <html> scrolling.

The right way to do it in JavaScript is by watching both or sniffing the browser, but the best we could do is watch both. Here's how I've handled it in another plugin:

var isSafari =
  !!navigator.userAgent.match(/safari/i)
  && !navigator.userAgent.match(/chrome/i)
  && typeof document.body.style.webkitFilter !== "undefined"
  && !window.chrome;
var isAndroid = navigator.userAgent.match(/android/i)
var isEdge = navigator.userAgent.match(/Edge\/\d/i)
var isChrome = navigator.userAgent.match(/Chrome/i)

// Safari, Android, and Edge should watch BODY
if (isSafari || isChrome || isAndroid || isEdge) {

var scrollContainer = document.body

// Firefox should watch HTML
} else {

 var scrollContainer = document.documentElement

}

So applying the same idea to CSS, you'd want to watch both <html> and <body>, maybe like this:

<!DOCTYPE html>
<html data-scrollery=html>
<body data-scrollery=body>
<style>
  html, body {
    background: hsl(calc(var(--html-scrollTop) + var(--body-scrollTop)),50%, 50%);
    font-size: 10vw;
  }
</style>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<script src=http://tomhodgins.github.io/cssplus/scrollery.js></script>

This should work according to the spec, and does work in Chrome and Safari, but Firefox doesn't like calc(var(--html-scrollTop) + var(--body-scrollTop)) so that doesn't work in Firefox no matter which element it watches for the value.

@ErikFontanel
Copy link
Author

Thanks for the insights! Never knew about the html/body differences. I'll try some stuff and let you know what works

@Martin-Pitt
Copy link

Martin-Pitt commented Aug 15, 2018

If you are tracking html/body, you could shortcut to the document.scrollingElement, which has pretty solid support.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants