DevExtreme jQuery/JS - Scroll the Content

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.

JavaScript
$(function() {
    $("#scrollViewContainer").dxScrollView({
        scrollByContent: true, // enables the swipe gesture on all platforms
        scrollByThumb: true // makes the scrollbar active on all platforms
    });
});

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.

JavaScript
$(function() {
    var scrollView = $("#scrollViewContainer").dxScrollView({
        height: 400
    }).dxScrollView("instance");

    $("#scrollUpButton").dxButton({
        text: "Scroll Up",
        onClick: function () {
            scrollView.scrollBy(-100);
        }
    });

    $("#scrollDownButton").dxButton({
        text: "Scroll Down",
        onClick: function () {
            scrollView.scrollBy(100);
        }
    });
});

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"

JavaScript
$(function() {
    var scrollView = $("#scrollViewContainer").dxScrollView({
        height: 400,
        width: 100,
        direction: "both"
    }).dxScrollView("instance");

    $("#scrollButton").dxButton({
        text: "Scroll",
        onClick: function () {
            scrollView.scrollBy({ left: 100, top: 100 });
        }
    });
});

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.

JavaScript
$(function() {
    var scrollView = $("#scrollViewContainer").dxScrollView({
        height: 400,
        width: 100,
        direction: "vertical"
    }).dxScrollView("instance");

    $("#scrollButton").dxButton({
        text: "Scroll",
        onClick: function () {
            scrollView.scrollTo(500);
        }
    });
});

To scroll the content to a specific element, call the scrollToElement(targetLocation) method.

HTML
JavaScript
<div id="scrollViewContainer">
    <!-- Here goes long content -->
    <div id="end"></div>
</div>
$(function() {
    var scrollView = $("#scrollViewContainer").dxScrollView({
        height: 400,
        width: 100,
        direction: 'vertical',
    }).dxScrollView("instance");

    $("#scrollButton").dxButton({
        text: "Scroll ",
        onClick: function () {
            // Scrolls the content to the element with the "end" id
            scrollView.scrollToElement($("#end"));
        }
    });
});

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