Getting Started with the ScrollView
This tutorial explains how to set up a basic Telerik UI for ASP.NET MVC 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.
Prerequisites
To successfully complete the tutorial, you need a project that is already configured to use the Telerik UI for ASP.NET MVC components:
To create a new pre-configured project for the Telerik UI for ASP.NET MVC components, you can use a project template.
To manually configure an existing project by using NuGet, see the Adding Telerik UI through NuGet.
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 MVC HtmlHelpers:
@using Kendo.Mvc.UI
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>
2. Initialize the ScrollView
Use the ScrollView HtmlHelper to add the component to a page:
- The
Name()
configuration method is mandatory as its value is used for theid
and thename
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>
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>
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>
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:
-
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>
- 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: