Hi is it possible to disable window scrolling without using overflow:hidden;
when i\'m hover an element?
i tryed :
$(\'.chat-content\').
You can use jquery-disablescroll to solve the problem:
$window.disablescroll();
$window.disablescroll("undo");
Supports handleWheel
, handleScrollbar
, handleKeys
and scrollEventKeys
.
CSS 'fixed' solution (like Facebook does):
body_temp = $("<div class='body_temp' />")
.append($('body').contents())
.css('position', 'fixed')
.css('top', "-" + scrolltop + 'px')
.width($(window).width())
.appendTo('body');
to toggle to normal state:
var scrolltop = Math.abs($('.body_temp').position().top);
$('body').append($('.body_temp').contents()).scrollTop(scrolltop);
Following Glens idea, here it goes another possibility. It would allow you to scroll inside the div, but would prevent the body to scroll with it, when the div scroll ends. However, it seems to accumulate too many preventDefault if you scroll too much, and then it creates a lag if you want to scroll up. Does anybody have a suggestion to fix that?
$(".scrollInsideThisDiv").bind("mouseover",function(){
var bodyTop = document.body.scrollTop;
$('body').on({
'mousewheel': function(e) {
if (document.body.scrollTop == bodyTop) return;
e.preventDefault();
e.stopPropagation();
}
});
});
$(".scrollInsideThisDiv").bind("mouseleave",function(){
$('body').unbind("mousewheel");
});
Without external variables:
$('.element').bind('mousewheel', function(e, d) {
if((this.scrollTop === (this.scrollHeight - this.offsetHeight) && d < 0)
|| (this.scrollTop === 0 && d > 0)) {
e.preventDefault();
}
});
Plenty of good ideas on this thread. I have a lot of popups in my page for handling user input. What I use, is a combination of disabling the mousewheel and hiding the scrollbar:
this.disableScrollFn= function(e) {
e.preventDefault(); e.stopPropagation()
};
document.body.style.overflow = 'hidden';
$('body').on('mousewheel', this.disableScrollFn);
Advantage of this is we stop the user from scrolling in any possible way, and without having to change css position and top properties. I'm not concerened about touch events, since touch outside would close the popup.
To disable this, upon closing the popup I do the following.
document.body.style.overflow = 'auto';
$('body').off('mousewheel', this.disableScrollFn);
Note, I store a reference to my disableScrollFn on the existing object (in my case a PopupViewModel), for that gets triggered upon closing the popup to have access to disableScrollFn.
tfe answered this question in another post on StackOverflow: Answered
Another method would be to use:
$(document).bind("touchmove",function(event){
event.preventDefault();
});
But it may prevent some of the jquery mobile functionality from working properly.