在div上滚动激活另一个div的滚动

的jsfiddle

当我在滚动之外时,我试图激活当前滚动,特别是在#DivDet

这是我试过的:

 $("div#DivDet").scroll(function () { // I don't know what i should have here // something like $("div#scrlDiv").scroll(); }); 

听起来你想通过滚动另一个div来响应一个div的滚动。

您已经确定了如何挂钩scroll事件。 要设置元素的滚动位置(另一个div ),可以设置元素的scrollTopscrollLeft值(以像素为单位)。 例如,如果你想让两个div以近齐的方式滚动,你可以将源div的scrollTopscrollLeft分配给目标div

示例: 实时复制 | 资源

相关的JavaScript:

 (function() { var target = $("#target"); $("#source").scroll(function() { target.prop("scrollTop", this.scrollTop) .prop("scrollLeft", this.scrollLeft); }); })(); 

或者( 来源 ):

 (function() { var target = $("#target")[0]; // <== Getting raw element $("#source").scroll(function() { target.scrollTop = this.scrollTop; target.scrollLeft = this.scrollLeft; }); })(); 

完整页面:

      Scroll Example    

Scroll the left div, watch the right one.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20