A way to scroll an underlying div when mouse is on top of a fixed div?

前端 未结 4 1125
情歌与酒
情歌与酒 2020-12-03 05:15

The question is so long that coming up with a title that summarises it proved tricky.

So anyway. I have a div that has overflow: auto and t

4条回答
  •  攒了一身酷
    2020-12-03 06:19

    var fixedElement = document.getElementById("fixed");
    
    function fixedScrolled(e) {
        var evt = window.event || e;
        var delta = evt.detail ? evt.detail * (-120) : evt.wheelDelta; //delta returns +120 when wheel is scrolled up, -120 when scrolled down
        $("#content").scrollTop($("#content").scrollTop() - delta);
    }
    
    var mousewheelevt = (/Gecko\//i.test(navigator.userAgent)) ? "DOMMouseScroll" : "mousewheel";
    if (fixedElement.attachEvent)
        fixedElement.attachEvent("on" + mousewheelevt, fixedScrolled);
    else if (fixedElement.addEventListener)
        fixedElement.addEventListener(mousewheelevt, fixedScrolled, false);
    

    jsFiddle Demo - Scroll!

提交回复
热议问题