你在滚动时如何跟随div? [英] How do you make a div follow as you scroll?

查看:79
本文介绍了你在滚动时如何跟随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());
});

这篇关于你在滚动时如何跟随div?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆