Prevent PageSscroll When DropDownList is Opened
Environment
Product | Progress® Kendo UI® DropDownList for jQuery |
Description
How can I prevent the page scroll when the DropDownList is opened?
Solution
- Query the
ul
element of the DropDownList and attach awheel
event handler to its parent. - Prevent the default action if the scroll is at the beginning or at the end of the DropDownList ListView.
<div id="example">
<br/><br/><br/><br/><br/><br/><br/><br/>
<div id="example" style="min-height: 2000px; padding: 30px;">
<input id="products" style="width: 400px" />
</div>
</div>
<script>
$(function() {
$("#products").kendoDropDownList({
dataTextField: "ProductName",
dataValueField: "ProductID",
dataSource: {
transport: {
read: {
dataType: "jsonp",
url: "https://demos.telerik.com/kendo-ui/service/Products",
}
}
},
value: "74"
});
var widget = $("#products").data("kendoDropDownList");
widget.ul.parent().on("wheel", function(e) {
var container = this;
if ((container.scrollTop == 0 && e.originalEvent.deltaY < 0) ||
(container.scrollTop == container.scrollHeight - container.offsetHeight &&
e.originalEvent.deltaY > 0)) {
e.preventDefault();
e.stopPropagation();
}
});
});
</script>