How do you make a div follow as you scroll?

前端 未结 6 1975
孤街浪徒
孤街浪徒 2020-12-23 16:33

I have a div on the left hand side which includes the business hours and weather. I would like that div to scroll down and up according to how the user scrolls. So it would

6条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-23 16:41

    A better JQuery answer would be:

    $('#ParentContainer').scroll(function() { 
        $('#FixedDiv').animate({top:$(this).scrollTop()});
    });
    

    You can also add a number after scrollTop i.e .scrollTop() + 5 to give it buff.

    A good suggestion would also to limit the duration to 100 and go from default swing to linear easing.

    $('#ParentContainer').scroll(function() { 
        $('#FixedDiv').animate({top:$(this).scrollTop()},100,"linear");
    })
    

提交回复
热议问题