滚动在子div(固定)应滚动父div

编程入门 行业动态 更新时间:2024-10-27 16:30:40
本文介绍了滚动在子div(固定)应滚动父div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个父母和子女div。 Child div绝对位于顶部并可滚动。我的要求是滚动父div,当我滚动里面的子div和滚动应该是相同的,即如果我在子div滚动100px然后外部div也应滚动100px。

Jsfiddle

HTML

< div class =outer> < div class =inner> < p> Lorem ipsum dolor sit amet,consectetur adipiscing elit。 Curabitur mollis gravida metus,ac lacinia quam lobortis vel。 Cras interdum purus aliquam,porttitor metus nec,ornare quam。 Morbi eget lobortis elit ... ... < / p> < / div> < / div>

CSS .outer { height:1600px; 宽度:100%; 背景:#ddd; } .inner { position:absolute; height:200px; width:400px; 背景:红色; margin:auto; top:40px; right:0px; left:0px; overflow:scroll;

解决方案

$ c>滚动事件。在事件处理函数中,设置父级的滚动顶端等于孩子的位置。

类似如下: <$ ('scroll',function(){ var sTop = $(this).scrollTop(); $($'$'$'$' '#parent')。scrollTop(sTop); });

JSFiddle

I have one parent and child div. Child div is absolutely positioned to the top and is scrollable. My requirement is to scroll the parent div when i scroll inside the child div and scrolling should be same i.e if i scroll 100px in child div then outer div should also scroll by 100px.

Jsfiddle

HTML

<div class="outer"> <div class="inner"> <p> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur mollis gravida metus, ac lacinia quam lobortis vel. Cras interdum purus aliquam, porttitor metus nec, ornare quam. Morbi eget lobortis elit ... ... </p> </div> </div>

CSS

.outer{ height:1600px; width:100%; background:#ddd; } .inner{ position:absolute; height:200px; width:400px; background:red; margin:auto; top:40px; right:0px; left:0px; overflow:scroll; }

解决方案

Listen for the scroll event on the child. In the event handler function, set the parent's scroll top equal to the child's.

Something like the following:

$('#child').on('scroll', function(){ var sTop = $(this).scrollTop(); $('#parent').scrollTop(sTop); });

JSFiddle

更多推荐

滚动在子div(固定)应滚动父div

本文发布于:2023-10-08 03:11:09,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:div

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!