简体   繁体   English

滚动固定元素的内容以及父级

[英]Scroll content of fixed element along with parent

Question title might be bit confusing, but please have a look at this fiddle for more info. 问题标题可能有点令人困惑,但请详细了解这个小提琴。

I have a container with scrollbar and one element fixed left to it. 我有一个带滚动条的容器,一个元素固定在它上面。 When I am scrolling I would like to make fixed element content also scrolling. 当我滚动时,我想使固定元素content也滚动。 Here is the code 这是代码

 $('.parent').on('scroll', function(){
   var top = $(this).scrollTop(), _top = $('.right').offset().top;
   $('.left').animate({
      scrollTop : top - _top
   },0)
 });

Please refer to fiddle for demo. 请参考小提琴进行演示。

This is working but there is some non uniform scrolling and I am unable to fix it. 这是有效的,但有一些非统一的滚动,我无法解决它。

I think the extra subtraction of _top is causing the uniformity issue, since it is recalculating the offset top each time, and continually subtracting it. 我认为_top的额外减法会导致均匀性问题,因为它每次重新计算偏移顶部,并不断减去它。

$('.parent').on('scroll', function(){
    var top = $(this).scrollTop(), _top = $('.right').offset().top;
    $('.left').animate({
        scrollTop : top
    },0)
});

Appears to let the scrolling be more uniform, minus some padding issues. 似乎让滚动更加统一,减去一些填充问题。

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

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