I have a little "floating tool box" - a div with position:fixed; overflow:auto. Works just fine.

But when scrolling inside that box (with the mouse wheel) and reaching the bottom OR top, the parent element "takes over" the "scroll request" : The document behind the tool box scrolls.
- Which is annoying and not what the user "asked for".

I'm using jQuery and thought I could stop this behaviour with event.stoppropagation():
$("#toolBox").scroll( function(event){ event.stoppropagation() });

It does enter the function, but still, propagation happens anyway (the document scrolls)
- It's surprisingly hard to search for this topic on SO (and Google), so I have to ask:
How to prevent propagation / bubbling of the scroll-event ?

Edit:
Working solution thanks to amustill (and Brandon Aaron for the mousewheel-plugin here:
https://github.com/brandonaaron/jquery-mousewheel/raw/master/jquery.mousewheel.js

$(".ToolPage").bind('mousewheel', function(e, d)  
    var t = $(this);
    if (d > 0 && t.scrollTop() === 0) {
        e.preventDefault();
    }
    else {
        if (d < 0 && (t.scrollTop() == t.get(0).scrollHeight - t.innerHeight())) {
            e.preventDefault();
        }
    }
});

推荐答案

It's possible with the use of Brandon Aaron's Mousewheel plugin.

Here's a demo: http://jsbin.com/jivutakama/edit?html,js,output

$(function() {

  var toolbox = $('#toolbox'),
      height = toolbox.height(),
      scrollHeight = toolbox.get(0).scrollHeight;

  toolbox.bind('mousewheel', function(e, d) {
    if((this.scrollTop === (scrollHeight - height) && d < 0) || (this.scrollTop === 0 && d > 0)) {
      e.preventDefault();
    }
  });

});

Javascript相关问答推荐

基于变量切换隐藏文本

在vercel throws上部署带有gunjs的sveltekit应用无法找到模块./' lib/文本编码'

Google maps API通过API返回ZERO_RESULTS,以获得方向请求,但适用于Google maps

我的服务工作器没有连接到我的Chrome扩展中的内容脚本.我该怎么解决这个问题?

当Redux提供程序访问Reduxstore 时,可以安全地从Redux提供程序外部调用钩子?

如何在输入元素中附加一个属性为checkbox?

Websocket错误—有一个或多个保留位开启:reserved1 = 1,reserved2 = 0,reserved3 = 0

为什么客户端没有收到来自服务器的响应消息?

为什么可选参数的顺序会导致问题?

MongoDB受困于太多的数据

无法读取未定义的属性(正在读取合并)-react RTK

为什么延迟在我的laravel项目中不起作用?

使用线性插值法旋转直线以查看鼠标会导致 skip

在Java脚本中构建接口的对象

Django模板中未加载JavaScript函数

如何检测当前是否没有按下键盘上的键?

使用Java脚本替换字符串中的小文本格式hashtag

用内嵌的含selenium的Java脚本抓取网站

JS/css:将数字输入的S函数和可调整大小的元素S函数绑定在一起

使用导航时,路径的所有子组件都必须是路径