admin管理员组文章数量:1287810
I am using the jQuery custom content scroller to create custom scrollers on div elements. When scrolling inside of a div and reaching the bottom end of the div, the page will start scrolling. Is there a way to prevent the scroll event from propagating?
I have created a / to illustrate the issue. Just resize the browser to create a vertical scrollbar and start scrolling inside the "Hello World" div. I am calling the plugin like this:
$('#scrollable').mCustomScrollbar({
scrollInertia: 0
});
I am using the jQuery custom content scroller to create custom scrollers on div elements. When scrolling inside of a div and reaching the bottom end of the div, the page will start scrolling. Is there a way to prevent the scroll event from propagating?
I have created a http://jsfiddle/7CPv5/ to illustrate the issue. Just resize the browser to create a vertical scrollbar and start scrolling inside the "Hello World" div. I am calling the plugin like this:
$('#scrollable').mCustomScrollbar({
scrollInertia: 0
});
Share
Improve this question
asked Feb 16, 2014 at 18:29
FynnFynn
4,8934 gold badges35 silver badges75 bronze badges
5 Answers
Reset to default 3Different way to solve this:
$("#scrollable").mouseenter(function(){
$("#content-md").mCustomScrollbar("disable");
}).mouseleave(function(){
$("#content-md").mCustomScrollbar("update");
});
I know this is an old question but if someone stumbles upon it, here is the plugin-native way to do it without extra scripting:
$('#scrollable').mCustomScrollbar({
scrollInertia: 0,
mouseWheel: {preventDefault: true}
});
Reference: https://github./malihu/malihu-custom-scrollbar-plugin/issues/263
You can use this solution:
$('#scrollable').mCustomScrollbar({
scrollInertia: 0
});
var customScrollerFocused = false,
UserScrollDisabler = function() {
// spacebar: 32, pageup: 33, pagedown: 34, end: 35, home: 36
// left: 37, up: 38, right: 39, down: 40
this.scrollEventKeys = [32, 33, 34, 35, 36, 37, 38, 39, 40];
this.$window = $(window);
this.$document = $(document);
};
UserScrollDisabler.prototype = {
disable_scrolling_on_custom_scroll_focused : function() {
var t = this;
t.$window.on("mousewheel.UserScrollDisabler DOMMouseScroll.UserScrollDisabler", this._handleWheel);
t.$document.on("mousewheel.UserScrollDisabler touchmove.UserScrollDisabler", this._handleWheel);
t.$document.on("keydown.UserScrollDisabler", function(event) {
t._handleKeydown.call(t, event);
});
},
enable_scrolling : function() {
var t = this;
t.$window.off(".UserScrollDisabler");
t.$document.off(".UserScrollDisabler");
},
_handleKeydown : function(event) {
if(customScrollerFocused) {
for (var i = 0; i < this.scrollEventKeys.length; i++) {
if (event.keyCode === this.scrollEventKeys[i]) {
event.preventDefault();
return;
}
}
}
},
_handleWheel : function(event) {
if(customScrollerFocused) {
event.preventDefault();
}
}
};
var disabler = new UserScrollDisabler();
disabler.disable_scrolling_on_custom_scroll_focused();
$('#scrollable').find(".mCustomScrollBox").hover(
function() {
customScrollerFocused = true;
},
function() {
customScrollerFocused = false;
}
);
The idea here is to prevent body scroll event handling when the focus is on the jQuery custom content scroller.
You can use jquery-scrollLock:
$('#scrollable').scrollLock();
You can handle the mousewheel
and DOMMouseScroll
events and call preventDefault()
if the event originated from inside a scrollable region.
$(document).on('mousewheel DOMMouseScroll', function(e) {
if ($(e.target).closest('.mCustomScrollbar').data('mouseWheel')) {
e.preventDefault();
}
});
本文标签: javascriptPrevent scroll event propagation in mCustomScrollbarStack Overflow
版权声明:本文标题:javascript - Prevent scroll event propagation in mCustomScrollbar - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741327140a2372553.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论