I write datagrid with lazy loading, and for some reason I need stop scrolling, when user achieve area without data, to load it. I'm would like to know how I can stop scrolling on page from my code. I have tried e.preventDefault
, return false
from event handler and that solution also almost working solution.
But problem is all of that solutions actually don't block scrolling, just emulate it. e.g. I want to block scrolling when user
scroll up to 5000px
$(function() {
var container = $(".container");
var info = $(".info > .position");
container.scroll(function(e) {
var topPosition = container.scrollTop();
if (topPosition >= 5000) {
disableScroll();
}
info.text("scrollTop: " + topPosition);
});
$(".info > .disable").click(disableScroll);
$(".info > .enable").click(enableScroll);
});
/*
* Code from solution which was mentioned above
* https://stackoverflow.com/questions/4770025/how-to-disable-scrolling-temporarily
*/
const keys = {
37: 1,
38: 1,
39: 1,
40: 1
};
function preventDefault(e) {
e = e || window.event;
if (e.preventDefault)
e.preventDefault();
e.returnValue = false;
}
function preventDefaultForScrollKeys(e) {
if (keys[e.keyCode]) {
preventDefault(e);
return false;
}
}
function disableScroll() {
$(".info > .state").text("Disable");
if (window.addEventListener) // older FF
window.addEventListener('DOMMouseScroll', preventDefault, false);
window.onwheel = preventDefault; // modern standard
window.onmousewheel = document.onmousewheel = preventDefault; // older browsers, IE
window.ontouchmove = preventDefault; // mobile
document.onkeydown = preventDefaultForScrollKeys;
}
function enableScroll() {
$(".info > .state").text("Enable");
if (window.removeEventListener)
window.removeEventListener('DOMMouseScroll', preventDefault, false);
window.onmousewheel = document.onmousewheel = null;
window.onwheel = null;
window.ontouchmove = null;
document.onkeydown = null;
}
html,
body,
.page {
overflow: hidden;
height: 100%;
width: 100%;
padding: 0;
margin: 0;
}
.header {
box-sizing: border-box;
position: fixed;
padding: 0 15px;
display: table;
top: 0;
left: 0;
height: 50px;
width: 100%;
}
.info {
background-color: #cdcdcd;
border: 1px dashed blue;
vertical-align: middle;
display: table-cell;
text-align: center;
margin: 0 15px;
height: 50px;
}
.container {
height: calc(100% - 60px);
width: calc(100% - 30px);
margin: 60px 15px 0 15px;
box-sizing: border-box;
border: 1px solid red;
overflow: auto;
}
.content {
height: 100000px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="page">
<div class="header">
<div class="info">
|<label class="position">Initial</label> |
<label class="state">Enable</label>
<button class="enable" type="button">Enable</button>
<button class="disable" type="button">Disable</button>
</div>
</div>
<div class="container">
<div class="content">
</div>
</div>
</div>
it solution almost work, except cases when user use scrollbar
or wheel-click
scrolling. e.g. not working use case I want scroll will be stopped when achieve limit (in the example 5000px
) and allow scrolling after some event. Is it possible?
UPDATE
Actually question is how to stop scrolling during scrolling example. Scrolling was disabled when user achieve 5000px
, but current scrolling process
didn't stopped. And I would like to know is is possible to stop scrolling in progress i.e. in other words how to stop scrolling in the example when position become greater than 5000px
(change color in header to red
)