Guest User

Untitled

a guest
Nov 21st, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. class Nav extends React.Component {
  2. constructor(props) {
  3. super(props)
  4.  
  5. this.state = {
  6. isDown: false,
  7. startX: undefined,
  8. scrollLeft: undefined
  9. }
  10.  
  11. this.handleMouseDown = this.handleMouseDown.bind(this)
  12. this.handleMouseLeave = this.handleMouseLeave.bind(this)
  13. this.handleMouseUp = this.handleMouseUp.bind(this)
  14. this.handleMouseMove = this.handleMouseMove.bind(this)
  15. }
  16.  
  17. handleMouseDown(e) {
  18. this.setState({
  19. isDown: true,
  20. startX: e.pageX - e.currentTarget.offsetLeft,
  21. scrollLeft: e.currentTarget.scrollLeft
  22. })
  23. }
  24.  
  25. handleMouseLeave() {
  26. this.setState({
  27. isDown: false
  28. })
  29. }
  30.  
  31. handleMouseUp() {
  32. this.setState({
  33. isDown: false
  34. })
  35. }
  36.  
  37. handleMouseMove(e) {
  38. if (!this.state.isDown) return;
  39.  
  40. e.preventDefault()
  41.  
  42. const x = e.pageX - e.currentTarget.offsetLeft
  43. const walk = (x - this.state.startX) * 2
  44.  
  45. e.currentTarget.scrollLeft = this.state.scrollLeft - walk
  46. }
  47.  
  48. render() {
  49. return (
  50. <nav
  51. onMouseDown={this.handleMouseDown}
  52. onMouseUp={this.handleMouseUp}
  53. onMouseLeave={this.handleMouseLeave}
  54. onMouseMove={this.handleMouseMove}
  55. >
  56. {/* render a NodeList */}
  57. </nav>
  58. )
  59. }
  60. }
Add Comment
Please, Sign In to add comment