User Interaction
An end user can scroll the ScrollView content with the swipe gesture or with the scrollbar. The swipe gesture is default for touch devices, the scrollbar is default for desktops. However, you can unify the behavior of the ScrollView on all platforms. To control the swipe gesture scrolling, use the scrollByContent option. To control the scrollbar scrolling, use the scrollByThumb option.
- import React from 'react';
- import 'devextreme/dist/css/dx.common.css';
- import 'devextreme/dist/css/dx.light.css';
- import { ScrollView } from 'devextreme-react/scroll-view';
- class App extends React.Component {
- render() {
- return (
- <ScrollView
- scrollByContent={true} {/* enables the swipe gesture on all platforms */}
- scrollByThumb={true} {/* makes the scrollbar active on all platforms */}
- />
- );
- }
- }
- export default App;
API
To scroll the ScrollView content by a specified distance, call the scrollBy(distance) method. If you need to scroll in the opposite direction, the distance
parameter should be a negative number.
- import React from 'react';
- import 'devextreme/dist/css/dx.common.css';
- import 'devextreme/dist/css/dx.light.css';
- import { Button } from 'devextreme-react';
- import { ScrollView } from 'devextreme-react/scroll-view';
- class App extends React.Component {
- constructor(props) {
- super(props);
- this.scrollViewRef = React.createRef();
- }
- get scrollView() {
- return this.scrollViewRef.current.instance;
- }
- render() {
- return (
- <Button
- text="Scroll Up"
- onClick={this.scrollUp}>
- </Button>
- <Button
- text="Scroll Down"
- onClick={this.scrollDown}>
- </Button>
- <ScrollView
- ref={this.scrollViewRef}
- height={200}>
- <div>Content</div>
- </ScrollView>
- );
- }
- scrollUp = () => {
- this.scrollView.scrollBy(-100);
- }
- scrollDown = () => {
- this.scrollView.scrollBy(100);
- }
- }
- export default App;
To scroll the content both vertically and horizontally, call the scrollBy(distanceObj) method with an object as an argument. The format of the object is the following: { left: value1, top: value2 }. Note that in this case, the direction option must be set to "both"
- import React from 'react';
- import 'devextreme/dist/css/dx.common.css';
- import 'devextreme/dist/css/dx.light.css';
- import { Button } from 'devextreme-react';
- import { ScrollView } from 'devextreme-react/scroll-view';
- class App extends React.Component {
- constructor(props) {
- super(props);
- this.scrollViewRef = React.createRef();
- }
- get scrollView() {
- return this.scrollViewRef.current.instance;
- }
- render() {
- return (
- <Button
- text="Scroll"
- onClick={this.scroll}>
- </Button>
- <ScrollView
- ref={this.scrollViewRef}
- height={200}
- width={100}
- direction="both">
- <div>Content</div>
- </ScrollView>
- );
- }
- scroll = () => {
- this.scrollView.scrollBy({ left: 100, top: 100 });
- }
- }
- export default App;
To scroll the content to a specific position, call the scrollTo(targetLocation) method. Just like the scrollBy() method from the previous examples, the scrollTo() method accepts either a numeric value (when directon is "left" or "right") or an object (when direction is "both"). The object should have the following format: { left: value1, top: value2 }. Note that the top left corner of the ScrollView has the { left: 0, top: 0 } coordinates.
- import React from 'react';
- import 'devextreme/dist/css/dx.common.css';
- import 'devextreme/dist/css/dx.light.css';
- import { Button } from 'devextreme-react';
- import { ScrollView } from 'devextreme-react/scroll-view';
- class App extends React.Component {
- constructor(props) {
- super(props);
- this.scrollViewRef = React.createRef();
- }
- get scrollView() {
- return this.scrollViewRef.current.instance;
- }
- render() {
- return (
- <Button
- text="Scroll"
- onClick={this.scroll}>
- </Button>
- <ScrollView
- ref={this.scrollViewRef}
- height={200}
- width={100}
- direction="vertical">
- <div>Content</div>
- </ScrollView>
- );
- }
- scroll = () => {
- this.scrollView.scrollTo(300);
- }
- }
- export default App;
To scroll the content to a specific element, call the scrollToElement(targetLocation) method.
- import React from 'react';
- import 'devextreme/dist/css/dx.common.css';
- import 'devextreme/dist/css/dx.light.css';
- import { Button } from 'devextreme-react';
- import { ScrollView } from 'devextreme-react/scroll-view';
- class App extends React.Component {
- constructor(props) {
- super(props);
- this.scrollViewRef = React.createRef();
- }
- get scrollView() {
- return this.scrollViewRef.current.instance;
- }
- render() {
- return (
- <Button
- text="Scroll"
- onClick={this.scroll}>
- </Button>
- <ScrollView
- ref={this.scrollViewRef}
- height={200}
- width={100}
- direction="vertical">
- {/* Here goes long content */}
- <div id="end"></div>
- </ScrollView>
- );
- }
- scroll = () => {
- // Scrolls the content to the element with the "end" id
- this.scrollView.scrollToElement(document.querySelector('#end'));
- }
- }
- export default App;
To get the current scroll position against the top left corner, call the scrollOffset() method. It returns an object of the following format: { top: topScrollOffset, left: leftScrollOffset }. If you need to get only the top or left scroll offset, use the scrollTop() and scrollLeft() methods, respectively.
See Also
If you have technical questions, please create a support ticket in the DevExpress Support Center.