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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"license": "MIT",
"dependencies": {
"classnames": "^1.2.0",
"line-height": "^0.1.1",
"react": "^0.14.0"
},
"devDependencies": {
Expand Down
20 changes: 18 additions & 2 deletions src/js/scrollArea.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import '../less/scrollbar.less';
import React from 'react';
import Scrollbar from './scrollBar';
import {findDOMNode, warnAboutFunctionChild, warnAboutElementChild, positiveOrZero} from './utils';
import lineHeight from 'line-height';

export default class ScrollArea extends React.Component{
constructor(props){
Expand Down Expand Up @@ -32,6 +33,7 @@ export default class ScrollArea extends React.Component{

componentDidMount(){
window.addEventListener("resize", this.bindedHandleWindowResize);
this.lineHeightPx = lineHeight(findDOMNode(this.refs.content));
this.setSizesToState();
}

Expand Down Expand Up @@ -130,8 +132,22 @@ export default class ScrollArea extends React.Component{

handleWheel(e){
var newState = this.computeSizes();
var deltaY = e.deltaY * this.props.speed;
var deltaX = e.deltaX * this.props.speed;
var deltaY = e.deltaY;
var deltaX = e.deltaX;

/*
* WheelEvent.deltaMode can differ between browsers and must be normalized
* e.deltaMode === 0: The delta values are specified in pixels
* e.deltaMode === 1: The delta values are specified in lines
* https://developer.mozilla.org/en-US/docs/Web/API/WheelEvent/deltaMode
*/
if (e.deltaMode === 1) {
deltaY = deltaY * this.lineHeightPx;
deltaX = deltaX * this.lineHeightPx;
}

deltaY = deltaY * this.props.speed;
deltaX = deltaX * this.props.speed;

if(this.canScrollY(newState)){
newState.topPosition = this.computeTopPosition(-deltaY, newState);
Expand Down