New to Telerik UI for ASP.NET Core? Download free 30-day trial

Getting Started with the ScrollView

This tutorial explains how to set up a basic Telerik UI for ASP.NET Core ScrollView and highlights the major steps in the configuration of the component.

You will initialize a ScrollView and learn how to scroll to a view. Then, you will see how to attach an event handler to the component.

Finally, you can run the sample code in Telerik REPL and continue exploring the component.

Sample Telerik UI for ASP.NET Core ScrollView

Prerequisites

To successfully complete the tutorial, you need a project that is already configured to use the Telerik UI for ASP.NET Core components:

  • You can use the Telerik REPL playground and skip installing the components on your system and configuring a project.

  • You can prepare a Visual Studio project by following either of these guides:

1. Prepare the CSHTML File

The first step is to add the required directives at the top of the .cshtml document:

  • To use the Telerik UI for ASP.NET Core HtmlHelpers:

    @using Kendo.Mvc.UI
    
  • To use the Telerik UI for ASP.NET Core TagHelpers:

    @addTagHelper *, Kendo.Mvc
    

Optionally, you can structure the document by adding the desired HTML elements like headings, divs, paragraphs, and others.

@using Kendo.Mvc.UI
<h4>ScrollView with event handler</h4>
<p>
</p>
@addTagHelper *, Kendo.Mvc
<h4>ScrollView with event handler</h4>
<p>
</p>

2. Initialize the ScrollView

Use the ScrollView HtmlHelper or TagHelper to add the component to a page:

  • The Name() configuration method is mandatory as its value is used for the id and the name attributes of the ScrollView element.
  • The ContentHeight() configuration method sets the height of the ScrollView content.
  • The Items() configuration method sets the items in the ScrollView.
@using Kendo.Mvc.UI
<h4>ScrollView with event handler</h4>
<p>
    @(Html.Kendo().ScrollView()
                .Name("scrollView")
                .EnablePager(false)
                .ContentHeight("100%")
                .TemplateId("scrollview-template")
                .DataSource(d =>
                        d.Custom()
                          .Type("odata")
                          .Transport(t => t.Read(r => r.Url("https://demos.telerik.com/kendo-ui/service/Northwind.svc/Products")))
                          .ServerPaging(true)
                          .PageSize(3))
                .HtmlAttributes(new { style = "height:500px; width:890px; max-width: 100%;" })
    )
</p>

<script id="scrollview-template" type="text/x-kendo-template">
    <div class="img-wrapper">
        # for (var i = 0; i < data.length; i++) { #
        <div>
            <div style="width: 140px; height: 140px;  background-image: #=setBackground(data[i].ProductID)#; background-repeat:no-repeat; background-size: cover;"></div>
            <p>#= data[i].ProductName #</p>
        </div>
        # } #
    </div>
</script>
<script>
    function setBackground(id) {
        return "url(https://demos.telerik.com/kendo-ui/content/web/foods/" + id + ".jpg)";
    }
</script>

<style>
    .k-scrollview-wrap .img-wrapper {
        display: flex;
        width: 100%;
        height: 100%;
        vertical-align: middle;
        align-items: center;
        justify-content: space-around;
        text-align: center;
    }

    .k-scrollview-wrap .img-wrapper>div {
        width: 30%;
        min-width: 150px;
        box-sizing: border-box;
        display: inline-block;
        vertical-align: top;
        margin-bottom: 1em;
    }

    .k-scrollview-wrap .img-wrapper>div>div {
        margin: auto;
        margin-bottom: 0.5em;
    }
</style>
@addTagHelper *, Kendo.Mvc
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

<p>

    <kendo-scrollview name="scrollView" enable-pager="false" content-height="100%" template-id="scrollview-template" style="height:500px; width:890px; max-width: 100%;">
        <datasource custom-type="odata" page-size="3" server-paging="true">
            <transport>
                <read url="https://demos.telerik.com/kendo-ui/service/Northwind.svc/Products" />
            </transport>
        </datasource>
    </kendo-scrollview>

</p>

<script id="scrollview-template" type="text/x-kendo-template">
    <div class="img-wrapper">
        # for (var i = 0; i < data.length; i++) { #
        <div>
            <div style="width: 140px; height: 140px; background-image: #=setBackground(data[i].ProductID)#; background-repeat:no-repeat; background-size: cover;"></div>
            <p>#= data[i].ProductName #</p>
        </div>
        # } #
    </div>
</script>
<script>
    function setBackground(id) {
        return "url(https://demos.telerik.com/kendo-ui/content/web/foods/" + id + ".jpg)";
    }
</script>

<style>

    div.k-scrollview ul.k-scrollview-wrap > li {
        text-align: center;
        display: table;
        box-sizing: border-box;
    }

    ul.k-scrollview-wrap > li > .img-wrapper {
        padding: 2em;
        display: table-cell;
        vertical-align: middle;
    }

        ul.k-scrollview-wrap > li > .img-wrapper > div {
            width: 30%;
            min-width: 150px;
            box-sizing: border-box;
            display: inline-block;
            vertical-align: top;
            margin-bottom: 1em;
        }

            ul.k-scrollview-wrap > li > .img-wrapper > div > div {
                margin: auto;
                margin-bottom: 0.5em;
            }

</style>

3. Use the Duration Configuration of the ScrollView

The next step is to configure Duration configuration. The Duration (in milliseconds) for the ScrollView is used to snap to the current page after the user releases it.

@using Kendo.Mvc.UI
<h4>ScrollView with event handler</h4>
<p>
    @(Html.Kendo().ScrollView()
                .Name("scrollView")
                .EnablePager(false)
                .ContentHeight("100%")
                .Duration(1500)
                .TemplateId("scrollview-template")
                .DataSource(d =>
                        d.Custom()
                          .Type("odata")
                          .Transport(t => t.Read(r => r.Url("https://demos.telerik.com/kendo-ui/service/Northwind.svc/Products")))
                          .ServerPaging(true)
                          .PageSize(3))
                .HtmlAttributes(new { style = "height:500px; width:890px; max-width: 100%;" })
    )
</p>

<script id="scrollview-template" type="text/x-kendo-template">
    <div class="img-wrapper">
        # for (var i = 0; i < data.length; i++) { #
        <div>
            <div style="width: 140px; height: 140px;  background-image: #=setBackground(data[i].ProductID)#; background-repeat:no-repeat; background-size: cover;"></div>
            <p>#= data[i].ProductName #</p>
        </div>
        # } #
    </div>
</script>
<script>
    function setBackground(id) {
        return "url(https://demos.telerik.com/kendo-ui/content/web/foods/" + id + ".jpg)";
    }
</script>

<style>
    .k-scrollview-wrap .img-wrapper {
        display: flex;
        width: 100%;
        height: 100%;
        vertical-align: middle;
        align-items: center;
        justify-content: space-around;
        text-align: center;
    }

    .k-scrollview-wrap .img-wrapper>div {
        width: 30%;
        min-width: 150px;
        box-sizing: border-box;
        display: inline-block;
        vertical-align: top;
        margin-bottom: 1em;
    }

    .k-scrollview-wrap .img-wrapper>div>div {
        margin: auto;
        margin-bottom: 0.5em;
    }
</style>
@addTagHelper *, Kendo.Mvc
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

<p>

    <kendo-scrollview name="scrollView" enable-pager="false" content-height="100%" duration=1500 template-id="scrollview-template" style="height:500px; width:890px; max-width: 100%;">
        <datasource custom-type="odata" page-size="3" server-paging="true">
            <transport>
                <read url="https://demos.telerik.com/kendo-ui/service/Northwind.svc/Products" />
            </transport>
        </datasource>
    </kendo-scrollview>

</p>

<script id="scrollview-template" type="text/x-kendo-template">
    <div class="img-wrapper">
        # for (var i = 0; i < data.length; i++) { #
        <div>
            <div style="width: 140px; height: 140px; background-image: #=setBackground(data[i].ProductID)#; background-repeat:no-repeat; background-size: cover;"></div>
            <p>#= data[i].ProductName #</p>
        </div>
        # } #
    </div>
</script>
<script>
    function setBackground(id) {
        return "url(https://demos.telerik.com/kendo-ui/content/web/foods/" + id + ".jpg)";
    }
</script>

<style>

    div.k-scrollview ul.k-scrollview-wrap > li {
        text-align: center;
        display: table;
        box-sizing: border-box;
    }

    ul.k-scrollview-wrap > li > .img-wrapper {
        padding: 2em;
        display: table-cell;
        vertical-align: middle;
    }

        ul.k-scrollview-wrap > li > .img-wrapper > div {
            width: 30%;
            min-width: 150px;
            box-sizing: border-box;
            display: inline-block;
            vertical-align: top;
            margin-bottom: 1em;
        }

            ul.k-scrollview-wrap > li > .img-wrapper > div > div {
                margin: auto;
                margin-bottom: 0.5em;
            }

</style>

4. Handle a ScrollView Event

The ScrollView exposes a Change() event that you can handle and assign specific functions to the component. In this tutorial, you will use the Change() event to display a message when the page is changed.

@using Kendo.Mvc.UI
<h4>ScrollView with event handler</h4>
<p>
    @(Html.Kendo().ScrollView()
                .Name("scrollView")
                .EnablePager(false)
                .ContentHeight("100%")
                .Duration(1500)
                .TemplateId("scrollview-template")
                .DataSource(d =>
                        d.Custom()
                          .Type("odata")
                          .Transport(t => t.Read(r => r.Url("https://demos.telerik.com/kendo-ui/service/Northwind.svc/Products")))
                          .ServerPaging(true)
                          .PageSize(3))
                .HtmlAttributes(new { style = "height:500px; width:890px; max-width: 100%;" })
                .Events(e => e.Change("onChange"))
    )
</p>

<script id="scrollview-template" type="text/x-kendo-template">
    <div class="img-wrapper">
        # for (var i = 0; i < data.length; i++) { #
        <div>
            <div style="width: 140px; height: 140px;  background-image: #=setBackground(data[i].ProductID)#; background-repeat:no-repeat; background-size: cover;"></div>
            <p>#= data[i].ProductName #</p>
        </div>
        # } #
    </div>
</script>
<script>
    function setBackground(id) {
        return "url(https://demos.telerik.com/kendo-ui/content/web/foods/" + id + ".jpg)";
    }

    function onChange(){
        console.log("change");
    }
</script>

<style>
    .k-scrollview-wrap .img-wrapper {
        display: flex;
        width: 100%;
        height: 100%;
        vertical-align: middle;
        align-items: center;
        justify-content: space-around;
        text-align: center;
    }

    .k-scrollview-wrap .img-wrapper>div {
        width: 30%;
        min-width: 150px;
        box-sizing: border-box;
        display: inline-block;
        vertical-align: top;
        margin-bottom: 1em;
    }

    .k-scrollview-wrap .img-wrapper>div>div {
        margin: auto;
        margin-bottom: 0.5em;
    }
</style>
@addTagHelper *, Kendo.Mvc
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

<p>

    <kendo-scrollview name="scrollView" enable-pager="false" content-height="100%" duration=1500 template-id="scrollview-template" style="height:500px; width:890px; max-width: 100%;">
        <datasource custom-type="odata" page-size="3" server-paging="true" on-change="onChange">
            <transport>
                <read url="https://demos.telerik.com/kendo-ui/service/Northwind.svc/Products" />
            </transport>
        </datasource>
    </kendo-scrollview>

</p>

<script id="scrollview-template" type="text/x-kendo-template">
    <div class="img-wrapper">
        # for (var i = 0; i < data.length; i++) { #
        <div>
            <div style="width: 140px; height: 140px; background-image: #=setBackground(data[i].ProductID)#; background-repeat:no-repeat; background-size: cover;"></div>
            <p>#= data[i].ProductName #</p>
        </div>
        # } #
    </div>
</script>
<script>
    function setBackground(id) {
        return "url(https://demos.telerik.com/kendo-ui/content/web/foods/" + id + ".jpg)";
    }

    function onChange(){
        console.log("change");
    }
</script>

<style>

    div.k-scrollview ul.k-scrollview-wrap > li {
        text-align: center;
        display: table;
        box-sizing: border-box;
    }

    ul.k-scrollview-wrap > li > .img-wrapper {
        padding: 2em;
        display: table-cell;
        vertical-align: middle;
    }

        ul.k-scrollview-wrap > li > .img-wrapper > div {
            width: 30%;
            min-width: 150px;
            box-sizing: border-box;
            display: inline-block;
            vertical-align: top;
            margin-bottom: 1em;
        }

            ul.k-scrollview-wrap > li > .img-wrapper > div > div {
                margin: auto;
                margin-bottom: 0.5em;
            }

</style>

For more examples, refer to the demo on using the events of the ScrollView.

5. (Optional) Reference Existing ScrollView Instances

You can reference the ScrollView instances that you have created and build on top of their existing configuration:

  1. Use the id attribute of the component instance to establish a reference.

    <script>
        var scrollView = $("#scrollView").kendoScrollView().data().kendoScrollView; // scrollView is a reference to the existing scrollView instance of the helper.
    </script>
    
  2. Use the ScrollView client-side API to control the behavior of the widget. In this example, you will use the next method to switch to the next page with an animation. script <script> var scrollView = $("#scrollView").kendoScrollView().data().kendoScrollView; // scrollView is a reference to the existing scrollView instance of the helper. scrollView.next(); </script> For more information on referencing specific helper instances, see the Methods and Events article.

Explore this Tutorial in REPL

You can continue experimenting with the code sample above by running it in the Telerik REPL server playground:

Next Steps

In this article