I am trying to figure out how to scroll through a div using the mouse (left hold and drag), but I can't find anything useful.
If anyone has used trello, im trying to emulate the ability to drag horizontally using the mouse, instead of the scrollbar.
Most of the other plugins out there are for JQuery, and I would like either a native javascript solution or one with React.
I have looked at:
1. /
But it won't allow you to select elements inside of it, which is something that i also need.
Are there any other suitable plugins for React out there?
I am trying to figure out how to scroll through a div using the mouse (left hold and drag), but I can't find anything useful.
If anyone has used trello, im trying to emulate the ability to drag horizontally using the mouse, instead of the scrollbar.
Most of the other plugins out there are for JQuery, and I would like either a native javascript solution or one with React.
I have looked at:
1. https://github.com/asvd/dragscroll http://asvd.github.io/dragscroll/
But it won't allow you to select elements inside of it, which is something that i also need.
Are there any other suitable plugins for React out there?
Share Improve this question edited Dec 2, 2016 at 11:50 sudo bangbang 28.2k11 gold badges77 silver badges77 bronze badges asked Dec 2, 2016 at 6:32 pizzaepizzae 3,0258 gold badges29 silver badges47 bronze badges 1- Hey @pizzae did you find any solution ? i'm looking for the same issue to get solved ! – Nidhi Dadiya Commented Aug 19, 2020 at 10:31
1 Answer
Reset to default 11- Maintain MouseDown location and scroll info onmousedown of window
- Add mousemove listener on mousedown of scroller
- Calculate scrollLeft and scrollTop according to window clientX and clientY in mousemove
Remove mousemove on window's onmouseup listener.
class Application extends React.Component { state = {isScrolling: false}; componentWillUpdate = (nextProps, nextState) =>{ if(this.state.isScrolling !== nextState.isScrolling ) { this.toggleScrolling(nextState.isScrolling); } }; toggleScrolling = (isEnable) => { if (isEnable) { window.addEventListener('mousemove', this.onMouseMove); window.addEventListener('mouseup', this.onMouseUp); } else { window.removeEventListener('mousemove', this.onMouseMove); } }; onScroll = (event) => { }; onMouseMove = (event) => { const {clientX, scrollLeft, scrollTop, clientY} = this.state; this._scroller.scrollLeft = scrollLeft - clientX + event.clientX; this._scroller.scrollTop = scrollTop - clientY + event.clientY; }; onMouseUp = () => { this.setState({isScrolling: false, scrollLeft: 0, scrollTop: 0, clientX: 0, clientY:0}); }; onMouseDown = (event) => { const {scrollLeft, scrollTop} = this._scroller; this.setState({isScrolling:true, scrollLeft, scrollTop, clientX:event.clientX, clientY:event.clientY}); }; attachScroller = (scroller) => { this._scroller = React.findDOMNode(scroller); }; render() { return <div className='container'> <div className="scroller" ref={this.attachScroller} onMouseDown={this.onScroll} onScroll={this.onMouseMove} > <div className="child"/> </div> </div>; } } /* * Render the above component into the div#app */ React.render(<Application />, document.getElementById('app'));
helpful library
http://smikhalevski.github.io/react-scroll-box/
https://gist.github.com/gaearon/150fa1bed09c92abdb46
https://github.com/qiaolb/react-dragscroll