你在滚动时如何跟随div?

你在滚动时如何跟随div?

问题描述:

我在左边有一个div,包括营业时间和天气。我希望该div根据用户的滚动方式向下滚动。所以它会随着页面上下移动。我将如何尝试这个?这是我的网站 judystropicalgarden.com

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 follow and move up and down with the page. How would I attempt this? This is my website judystropicalgarden.com

谢谢

Thanks

您可以使用css属性Fixed,或者如果您需要更加精细的调整,那么您需要使用javascript并跟踪scrollTop属性,该属性定义用户代理的滚动条位置是(0位于顶部,x位于底部)

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;
}

或使用jQuery:

or with jQuery:

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