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

Normalize lines to pixels in wheel scroll event #15

Merged
merged 1 commit into from
Nov 27, 2015
Merged

Normalize lines to pixels in wheel scroll event #15

merged 1 commit into from
Nov 27, 2015

Conversation

evoyy
Copy link

@evoyy evoyy commented Nov 20, 2015

WheelEvent.deltaMode units can differ between browsers. For example, Firefox provides event.deltaX and event.deltaY in units of lines, whereas Chrome uses units of pixels. This causes inconsistent scroll behaviour between browsers. Firefox was extremely slow when scrolling in my testing. This commit normalizes lines to pixels based on the line-height of the content area so that Firefox scrolls as fast as Chrome.

Here are some references about this issue:

cubiq/iscroll#577 (comment)
https://github.com/noraesae/perfect-scrollbar/issues/281
http://stackoverflow.com/q/20110224/1890168
https://developer.mozilla.org/en-US/docs/Web/API/WheelEvent/deltaMode

@souhe
Copy link
Owner

souhe commented Nov 20, 2015

You're right. Scrollbar works horribly slow on firefox.

Is there any chance to easy convert lines to pixels without using external library?

@evoyy
Copy link
Author

evoyy commented Nov 21, 2015

It's not as simple as it sounds. This is the algorithm they use:

https://github.com/twolfson/line-height/blob/master/lib/line-height.js

Another option; assume 1 line == 20 pixels. This should be fine for most cases. I don't think it is so important that the scroll speed in Firefox vs another browser has to be absolutely identical.

@@ -148,6 +163,11 @@ export default class ScrollArea extends React.Component{
this.setState(newState);
}

convertLinesToPixels(lines) {
var pixels = lineHeight(findDOMNode(this.refs.content));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is that necessary to compute line height on every handleWheel event? Maybe you could move it to componentDidMount and compute line height only once

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is necessary, because each handleWheel event tells the exact number of lines that were scrolled, which depends how fast the wheel was moved. The value may be a number like 3.4365.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes but if I understand, function lineHeight() returns constant height of line(in some DOM element). It's not depend on wheel speed so it could be compute once in componentDidMount.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok now I understand. Yes, I agree. I think it's a good idea to compute it once instead of each time. I will have a look into this.

@evoyy
Copy link
Author

evoyy commented Nov 25, 2015

I have amended my pull request. Now the lineHeight is calculated just once, in componentDidMount.

souhe added a commit that referenced this pull request Nov 27, 2015
Normalize lines to pixels in wheel scroll event
@souhe souhe merged commit a1a935c into souhe:master Nov 27, 2015
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

Successfully merging this pull request may close these issues.

2 participants