简体   繁体   English

滚动时如何使 div 跟随?

[英]How do you make a div follow as you scroll?

I have a div on the left hand side which includes the business hours and weather.我在左侧有一个 div,其中包括营业时间和天气。 I would like that div to scroll down and up according to how the user scrolls.我希望该 div 根据用户的滚动方式向下和向上滚动。 So it would follow and move up and down with the page.所以它会跟随页面上下移动。 How would I attempt this?我将如何尝试? This is my website judystropicalgarden.com这是我的网站judystropicalgarden.com

Thanks谢谢

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)您可以使用 CSS 属性 Fixed,或者如果您需要更精细的调整,则需要使用 javascript 并跟踪 scrollTop 属性,该属性定义了用户代理的滚动条位置(0 位于顶部...而 x 位于在底部)

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

or with jQuery:或使用 jQuery:

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

The post is old but I found a perfect CSS for the purpose and I want to share it.这篇文章很旧,但我为此目的找到了一个完美的 CSS,我想分享它。

A sticky element toggles between relative and fixed, depending on the scroll position.粘性元素在相对和固定之间切换,具体取决于滚动位置。 It is positioned relative until a given offset position is met in the viewport - then it "sticks" in place (like position:fixed).它是相对定位的,直到在视口中遇到给定的偏移位置 - 然后它“粘”在适当的位置(如位置:固定)。

    div.sticky {
        position: -webkit-sticky; /* Safari */
        position: sticky;
        top: 0;
        background-color: green;
        border: 2px solid #4CAF50;
    }

Source: https://www.w3schools.com/css/css_positioning.asp来源: https ://www.w3schools.com/css/css_positioning.asp

Using styling from CSS, you can define how something is positioned.使用 CSS 中的样式,您可以定义某物的定位方式。 If you define the element as fixed , it will always remain in the same position on the screen at all times.如果您将元素定义为fixed ,它将始终保持在屏幕上的相同位置。

div
{
    position:fixed;
    top:20px;
}

You can use the fixed CSS position property to accomplish this.您可以使用fixed的 CSS 位置属性来完成此操作。 There is a basic tutorial on this here .这里有一个基本教程。

EDIT: However, this approach is NOT supported in IE versions < IE7, and only in IE7 if it is in standards mode.编辑:但是,IE 版本 < IE7 不支持这种方法,并且仅在 IE7 处于标准模式时才支持。 This is discussed in a little more detail here . 此处将对此进行更详细的讨论。

There is also a hack, explained here , that shows how to accomplish fixed positioning in IE6 without affecting absolute positioning.还有一个 hack,解释在这里,它展示了如何在 IE6 中完成固定定位而不影响绝对定位。 What version of IE are you targeting your website for?您的网站针对的是哪个版本的 IE?

A better JQuery answer would be:更好的 JQuery 答案是:

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

You can also add a number after scrollTop ie .scrollTop() + 5 to give it buff.您还可以在 scrollTop 之后添加一个数字,即 .scrollTop() + 5 以赋予它增益。

A good suggestion would also to limit the duration to 100 and go from default swing to linear easing.一个好的建议是将持续时间限制为 100 并从默认摆动变为线性缓动。

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

the position:fixed;位置:固定; property should do the work, I used it on my Website and it worked fine.财产应该做的工作,我在我的网站上使用它,它工作得很好。 http://www.w3schools.com/css/css_positioning.asp http://www.w3schools.com/css/css_positioning.asp

just use an background attachment and set it to scroll in your css.只需使用背景附件并将其设置为在您的 CSS 中滚动即可。

like this:像这样:

background-attachment: scroll;

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

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