I wrote some javascript code that prevents scrolling on focus. How do I disable it after focus is lost?
Focus
e.preventDefault();
e.stopPropagation();
Focus out
e.preventDefault = false;
e.stopPropagatio = false;
I wrote some javascript code that prevents scrolling on focus. How do I disable it after focus is lost?
Focus
e.preventDefault();
e.stopPropagation();
Focus out
e.preventDefault = false;
e.stopPropagatio = false;
Share
Improve this question
asked Jan 20, 2017 at 13:42
user5804438user5804438
3
- 1 Would it be worth trying to unbind the event listener? Or put the methods behind a boolean flag? – evolutionxbox Commented Jan 20, 2017 at 13:43
- 1 You can't undo them. Just stop calling them. – Oriol Commented Jan 20, 2017 at 13:46
- @evolutionxbox that fixed it :). Thanks! – user5804438 Commented Jan 20, 2017 at 13:52
2 Answers
Reset to default 4Fixed by unbinding the event.
Focus
$('.form-control').focus(function() {
$('#element').on('scroll touchmove mousewheel', function(e){
e.preventDefault();
e.stopPropagation();
})
});
Focus out
$('.form-control').focusout(function() {
$('#element').unbind();
});
You can't undo preventDefault
nor stopPropagation
.
Just stop calling them, e.g.
var stopScroll = true;
element.addEventListener('scroll', function() {
if (stopScroll) {
e.preventDefault();
e.stopPropagation();
}
});
// ...
stopScroll = false;
function listener() {
e.preventDefault();
e.stopPropagation();
}
element.addEventListener('scroll', listener);
// ...
element.removeEventListener('scroll', listener);