简体   繁体   English

将滚动事件从父容器传播到子容器

[英]Propagate scroll event from parent container to a child container

I'm going to split the page in 2 columns, sometimes the left column will have an overflow, and the right column will not. 我将页面分为两列,有时左列会溢出,而右列则不会。 The right column would be the Parent, the left column is an overflow child. 右列为父级,左列为溢出子级。


 .parentDiv { background-color: red; } .childDiv { background-color: green; height: 100px; width: 300px; overflow-y: scroll; overflow-x: scroll; } 
 <div class="parentDiv"> scrolling in this area should scroll the inner overflow div <br> scrolling in this area should scroll the inner overflow div <br> scrolling in this area should scroll the inner overflow div <br> scrolling in this area should scroll the inner overflow div <br> scrolling in this area should scroll the inner overflow div <br> scrolling in this area should scroll the inner overflow div <br> <div class="childDiv"> this should scroll only form outside and inside<br> this should scroll only form outside and inside<br> this should scroll only form outside and inside<br> this should scroll only form outside and inside<br> this should scroll only form outside and inside<br> this should scroll only form outside and inside<br> this should scroll only form outside and inside<br> this should scroll only form outside and inside<br> this should scroll only form outside and inside<br> v this should scroll only form outside and inside<br> </div> </div> 


I set up a similar scenario here: http://jsfiddle.net/y1byh25d/1/ 我在这里设置了类似的方案: http : //jsfiddle.net/y1byh25d/1/

Basically, i want to capture scroll events in the red area, and cause the green overflow container to scroll. 基本上,我想捕获红色区域中的滚动事件,并导致绿色溢出容器滚动。 It's a bit of weird one. 这有点奇怪。

I think i have it figured out :). 我想我想通了:)。 Here is the JSFiddle 这是JSFiddle

$(".parentDiv").on("wheel", function(e){});

detects when a scroll over the parent div happens 检测何时发生父div滚动

if(e.originalEvent.deltaY < 0 && scroll > 0) {
    scroll -= 10;
    }
    else if(e.originalEvent.deltaY > 0 && scroll < maxScroll){
        scroll += 10;
    }

some conditional logic that detects if the user is scrolling up or down. 一些条件逻辑,用于检测用户是向上滚动还是向下滚动。 It also checks if the child div can be scrolled any further up/down. 它还检查子div是否可以进一步向上/向下滚动。 Then if those conditions are met it modifies the values that will be assigned to scrollTop(). 然后,如果满足这些条件,它将修改将分配给scrollTop()的值。

$(".childDiv").scrollTop(scroll);

New scroll value is applied to the child div and it scrolls. 新的滚动值将应用于子div并滚动。

.childDiv{
    overflow-y: hidden;
}

Apply overflow: hidden; 应用溢出:隐藏; because it's the easiest way to disable scrolling of the childDiv. 因为这是禁用childDiv滚动的最简单方法。

That's all! 就这样! Here is the complete code used, keep in mind this is using jQuery and thus needs to be enabled to use the script. 这是使用的完整代码,请记住,这是使用jQuery的,因此需要启用才能使用脚本。

html html

<div class="parentDiv">
    <p>nothing to see here <br>nothing to see here <br>nothing to see here <br>nothing to see here <br>nothing to see here <br>nothing to see here <br>nothing to see here <br>nothing to see here <br>nothing to see here <br>nothing to see here <br>nothing to see here <br>nothing to see here <br>

    </p>
    <div class="childDiv">
        this should scroll only form outside and inside<br>
        this should scroll only form outside and inside<br>
        this should scroll only form outside and inside<br>
        this should scroll only form outside and inside<br>
        this should scroll only form outside and inside<br>
        this should scroll only form outside and inside<br>
        this should scroll only form outside and inside<br>
        this should scroll only form outside and inside<br>
        this should scroll only form outside and inside<br>
        v
        this should scroll only form outside and inside<br>
    </div>
</div>

css 的CSS

.parentDiv
{
  background-color: red;
}

.childDiv
{
  background-color: green;
    height: 100px;
    width: 100%;
    overflow-y: hidden;
}

jQuery jQuery的

var scroll = 0;
var maxScroll = $(".childDiv").prop('scrollHeight')-$(".childDiv").height();
$(".parentDiv").on("wheel", function(e){
    if(e.originalEvent.deltaY < 0 && scroll > 0) {
    scroll -= 10;
    }
    else if(e.originalEvent.deltaY > 0 && scroll < maxScroll){
        scroll += 10;
    }
    $(".childDiv").scrollTop(scroll);
});

Based on the answer by RMo, i made a small change for this to behave properly with smooth scrolling, such as on macs. 根据RMo的回答,我对此做了一些小的更改,以使其在平滑滚动时(例如在Mac上)正常运行。

//enable event listener only when needed.
    $scope.$watch('$root.fpo.scrollLocked', function() {
        if(true === $scope.$root.fpo.scrollLocked)
        {
            $parent.on("wheel", function(e) {
                maxScroll = $container.prop('scrollHeight')-$container.height();

                if(e.originalEvent.deltaY < 0 && scroll > 0) {
                    scroll += e.originalEvent.deltaY;
                }

                else if(e.originalEvent.deltaY > 0 && scroll < maxScroll){
                    scroll += e.originalEvent.deltaY;
                }
                e.preventDefault();
                $container.scrollTop(scroll);
            });
        }
        else
        {
            $parent.off('wheel');
        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM