How do you make a div follow as you scroll?

前端 未结 6 1970
孤街浪徒
孤街浪徒 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:57

    You can either use the css property Fixed, or if you need something more fine-tuned then you need to use javascript and track the scrollTop property which defines where the user agent's scrollbar location is (0 being at the top ... and x being at the bottom)

    .Fixed
    {
        position: fixed;
        top: 20px;
    }
    

    or with jQuery:

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

提交回复
热议问题