我可以将 DIV 始终保留在屏幕上,但不始终位于固定位置吗?

发布于 2024-12-20 12:31:05 字数 160 浏览 3 评论 0原文

我的网站有一个主表单,我想将一个 div 停靠在主表单内内容区域的顶部。无论滚动位置如何,该 div 都应始终可见。这可能吗?

一张图片会更好地解释它。

在此处输入图像描述

I have a master form for my website, and I want to dock a div to the top of the content area inside the master form. This div should always be visible despite scroll position. Is this possible?

A picture would explain it better.

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

时间海 2024-12-27 12:31:06

以下解决方案对我有用:

HTML

<div class="sticky">
    ...
</div>

CSS

.sticky {
    position: sticky;
    top: 0;
    height: 100%;
}

The below solution worked for me:

HTML

<div class="sticky">
    ...
</div>

CSS

.sticky {
    position: sticky;
    top: 0;
    height: 100%;
}
荒岛晴空 2024-12-27 12:31:05

我发布了一个示例作为评论,所以我想我会写出一个完整的答案。

标记非常简单,但每个部分都有一些重要的注释。

HTML

<div id="page">
    <div id="header">
        <div id="header-inner"> <!-- Note #1 -->
            <img src="http://placehold.it/300x100" />
        </div>
    </div>
    <div id="content">
        <!-- Some Content Here -->
    </div>
</div>

CSS

#page {
    padding: 100px;
    width: 300px;
}

#header,
#header-inner { /* Note #1 */
    height: 100px;
    width: 300px;
}

.scrolling { /* Note #2 */
    position: fixed;
    top: 0;
}

JavaScript

//jQuery used for simplicity
$(window).scroll(function(){
  $('#header-inner').toggleClass('scrolling', $(window).scrollTop() > $('#header').offset().top);

  //can be rewritten long form as:
  var scrollPosition, headerOffset, isScrolling;
  scrollPosition = $(window).scrollTop();
  headerOffset = $('#header').offset().top;
  isScrolling = scrollPosition > headerOffset;
  $('#header-inner').toggleClass('scrolling', isScrolling);
});

Note #1

滚动标题将使用 position:fixed 附加到页面顶部,但此样式将从内容流中删除内容,这将导致内容跳转,除非其容器保持原来的高度。

注意 #2

样式属于 CSS,但是可能很难将某些元素与其原始位置正确对齐。您可能需要通过 JavaScript 动态设置 leftright css 属性。

I posted a sample as a comment, so I suppose I'll write out a full answer to this.

The markup is pretty straight-forward, but there are some important notes for each section.

HTML

<div id="page">
    <div id="header">
        <div id="header-inner"> <!-- Note #1 -->
            <img src="http://placehold.it/300x100" />
        </div>
    </div>
    <div id="content">
        <!-- Some Content Here -->
    </div>
</div>

CSS

#page {
    padding: 100px;
    width: 300px;
}

#header,
#header-inner { /* Note #1 */
    height: 100px;
    width: 300px;
}

.scrolling { /* Note #2 */
    position: fixed;
    top: 0;
}

JavaScript

//jQuery used for simplicity
$(window).scroll(function(){
  $('#header-inner').toggleClass('scrolling', $(window).scrollTop() > $('#header').offset().top);

  //can be rewritten long form as:
  var scrollPosition, headerOffset, isScrolling;
  scrollPosition = $(window).scrollTop();
  headerOffset = $('#header').offset().top;
  isScrolling = scrollPosition > headerOffset;
  $('#header-inner').toggleClass('scrolling', isScrolling);
});

Note #1

The scrolling header will be attached to the top of the page using position: fixed, but this style will remove the content from content flow, which will cause the content to jump unless its container maintains the original height.

Note #2

Styles belong in CSS, however it may be difficult to properly align some elements with their original position. You may need to dynamically set the left or right css property via JavaScript.

清秋悲枫 2024-12-27 12:31:05

截至 2018 年 7 月,是时候重新审视这个问题了。 position: Sticky 正是针对像您这样的问题而引入的,并且具有不错的浏览器支持< /a>.

它的工作原理如下:

<div style="position: sticky; top: 0;"> Header </div>

其中 top 是滚动时 div 应停留在视口顶部的距离。 必须指定top其他选项,例如bottomleftright,都可以<目前无法工作。

粘性 div 在所有方面都像普通 div 一样,除了当您滚动经过它时,它会粘在浏览器的顶部。

这里有一个 jsfiddle 给你一个想法。

As of July 2018 it is time to revisit this issue. position: sticky was introduced exactly for problems like yours, and has decent browser support.

It works like this:

<div style="position: sticky; top: 0;"> Header </div>

where top is the distance to the viewport top the div should stay at when you scroll. Specifying top is mandatory. Other options like bottom, left or right do not work currently.

The sticky div will act like a normal div in all ways except when you scroll past it, then it will stick to the top of the browser.

Here's a jsfiddle to give you an idea.

别把无礼当个性 2024-12-27 12:31:05

你需要 jQuery 或类似的东西,请参阅我的小提琴 此处

编辑

jQuery函数,其中 .form 是内容 div,.banner 是停靠的 div

$(window).scroll(function() {
    t = $('.form').offset();
    t = t.top;

    s = $(window).scrollTop();

    d = t-s;

    if (d < 0) {
        $('.banner').addClass('fixed');
        $('.banner').addClass('paddingTop');
    } else {
        $('.banner').removeClass('fixed');
        $('.banner').removeClass('paddingTop');
    }
});

.fixed {
    position:fixed;
    top:0px;
}
.paddingTop{
    padding-top:110px;
}

You'll need jQuery or the like, see my fiddle here

Edit

jQuery function, where .form is the content div and .banner is the div that is docked

$(window).scroll(function() {
    t = $('.form').offset();
    t = t.top;

    s = $(window).scrollTop();

    d = t-s;

    if (d < 0) {
        $('.banner').addClass('fixed');
        $('.banner').addClass('paddingTop');
    } else {
        $('.banner').removeClass('fixed');
        $('.banner').removeClass('paddingTop');
    }
});

.fixed {
    position:fixed;
    top:0px;
}
.paddingTop{
    padding-top:110px;
}
虚拟世界 2024-12-27 12:31:05

我创建了一个新的小提琴,希望它有用。 DIV 可以在页面中任意定位,并且在滚动时保持可见。

http://jsfiddle.net/mM4Df/

<div id="mydiv">
  fixed div
</div>

<div class="ghost">
  fixed div
</div>

CSS:

#mydiv { position: fixed;  background-color:Green; float:left; width:100%}
.ghost{opacity: 0}

可能有比使用“ghost”更好的解决方案但我不知道是哪一个。

I created a new fiddle which I hope can be useful. The DIV can be arbitrary positioned in the page and stays visible when scrolling.

http://jsfiddle.net/mM4Df/

<div id="mydiv">
  fixed div
</div>

<div class="ghost">
  fixed div
</div>

CSS:

#mydiv { position: fixed;  background-color:Green; float:left; width:100%}
.ghost{opacity: 0}

probably there is a better solution than using a "ghost" but I do not know which.

像极了他 2024-12-27 12:31:05

假设标题 div 的顶部位置(到屏幕顶部)一开始是 100px,你可以这样做:
如果窗口滚动顶部超过100px,则将标题div设置为顶部0px固定位置;
如果窗口滚动顶部小于100px,则使用默认布局设置标题div的位置。
对于jquery,它是这样的:

$(document).scroll(function() {
    if ($(this).scrollTop() > 100) {
        $('div#header').css({ 
            "position" : 'fixed',
            "top" : 0 });
    } else {
        $('div#header').css({ "position" : 'relative', "top" : 0 });
    }
});

它是通过滚动事件来实现的。

Assume the top position(to the top of the screen) of the header div is 100px in the beginning, you can do like this:
if the scroll top of window is over 100px, set the header div to fix position with top 0px;
if the scroll top of window is less than 100px, set the position of the header div with the default layout.
With jquery, it is sth like this:

$(document).scroll(function() {
    if ($(this).scrollTop() > 100) {
        $('div#header').css({ 
            "position" : 'fixed',
            "top" : 0 });
    } else {
        $('div#header').css({ "position" : 'relative', "top" : 0 });
    }
});

it is implemented with the scroll event.

ゃ人海孤独症 2024-12-27 12:31:05

使用 CSS 来固定其位置。

假设您的

有一个 ID“topdiv”:

#topdiv {
    position: fixed;
    top: 0;
    left: 0
}

请注意,您必须为内容设置 margin-top,因为固定 div 将显示在内容“上方”:

#contentarea { margin-top: 35px; }

检查此 < a href="http://jsfiddle.net/didierg/29VeC/" rel="nofollow">小提琴。

Use CSS to fix its position.

Assuming your <div> has an ID "topdiv":

#topdiv {
    position: fixed;
    top: 0;
    left: 0
}

Note you'll have to set a margin-top for the content, because the fixed div will display "over" the content:

#contentarea { margin-top: 35px; }

Check this fiddle.

盗琴音 2024-12-27 12:31:05

听起来您正在寻找的是一个具有两个属性的 header div:

  1. 当它通常可见时,它会保持在默认位置。
  2. 当它通常不可见时,它会出现在屏幕顶部。

简而言之,有点像“max-top”(它不作为 css 属性存在)。

如果您想完成所有这些,最快的方法可能涉及一些 JavaScript。
尝试这个;如果我稍后有时间,我会用更多代码更新它。

It sounds like what you're looking for is a header div with two properties:

  1. When it would normally be visible, it stays in its default position.
  2. When it would normally be invisible, it appears at the top of the screen.

In short, something a little bit like "max-top" (which doesn't exist as a css property).

If you want to do all of that, the quickest way may involve some JavaScript.
Try this; if I get some time later I'll update this with some more code.

ら栖息 2024-12-27 12:31:05

我相信您正在寻找向下滚动时跟随的 div。许多网站上的购物车都采用了这种实现方式。您可能需要查看 http://kitchen.net-perspective.com/ open-source/scroll-follow/

另一个很好的链接是:http://jqueryfordesigners.com/fixed-floating-elements/

一些相关的滚动示例集合:http://webdesign14.com/30-tutorials-and-plugins-jquery-scroll/

I believe you are looking for a div that follows when you scroll down. This implementation can be seen for shopping carts on number of sites. You may want to look at http://kitchen.net-perspective.com/open-source/scroll-follow/

Another good link is: http://jqueryfordesigners.com/fixed-floating-elements/

Some related collection of scroll examples: http://webdesign14.com/30-tutorials-and-plugins-jquery-scroll/

吃兔兔 2024-12-27 12:31:05

你可以试试这个CSS。也许这就是您正在寻找的:

    html, body{
        text-align:center;
        margin:0px;
        background:#ABCFFF;
        height:100%;
        }
    .header-cont {
        width:100%;
        position:fixed;
        top:0px;
    }
    #header {
        height:50px;
        background:#F0F0F0;
        border:1px solid #CCC;
        width: 100%;
        margin:auto;
    }
    .content-cont {
        width:100%;
        position:absolute; /* to place it somewhere on the screen */
        top:60px;
        bottom:60px; /* makes it lock to the bottom */
        overflow:auto;
    }
    #content {
        background:#F0F0F0;
        border:1px solid #CCC;
        width:960px;
        position:relative;  
        min-height:99.6%;
        height:auto;
        overflow: hidden;
        margin:auto;            
    }
    .footer-cont {
        width:100%;
        position:fixed;
        bottom:0px;
    }
    #footer {
        height:50px;
        background:#F0F0F0;
        border:1px solid #CCC;
        width: 100%;
        margin:auto;
    }

You can try this CSS. Maybe it is what you are looking for:

    html, body{
        text-align:center;
        margin:0px;
        background:#ABCFFF;
        height:100%;
        }
    .header-cont {
        width:100%;
        position:fixed;
        top:0px;
    }
    #header {
        height:50px;
        background:#F0F0F0;
        border:1px solid #CCC;
        width: 100%;
        margin:auto;
    }
    .content-cont {
        width:100%;
        position:absolute; /* to place it somewhere on the screen */
        top:60px;
        bottom:60px; /* makes it lock to the bottom */
        overflow:auto;
    }
    #content {
        background:#F0F0F0;
        border:1px solid #CCC;
        width:960px;
        position:relative;  
        min-height:99.6%;
        height:auto;
        overflow: hidden;
        margin:auto;            
    }
    .footer-cont {
        width:100%;
        position:fixed;
        bottom:0px;
    }
    #footer {
        height:50px;
        background:#F0F0F0;
        border:1px solid #CCC;
        width: 100%;
        margin:auto;
    }
浅笑轻吟梦一曲 2024-12-27 12:31:05

在此修改后的示例中,滚动时横幅 div 保留在屏幕中,并且也保留在容器 div 中。
当容器div底部到达屏幕顶部时,banner div返回到相对位置并与容器一起向上滚动
http://jsfiddle.net/SeeTS/198/

JQuery

$(window).scroll(function() {
t = $('.form').offset();
t = t.top;
s = $(window).scrollTop();
d = t-s;
b = $('.banner').height();
f = $('.form').height();

if (d < 5) {
    if(d < -(f-b)-15)
        {
        $('.banner').addClass('bottom');
        $('.banner').removeClass('fixed');
        }
    else {
        $('.banner').addClass('fixed');
        $('.banner').removeClass('bottom');
    }
}
else {
    $('.banner').removeClass('fixed');

}
});

CSS

    .form {
    position:relative;
    width:480px;
    margin: 100px auto;
    padding:10px;
}
p {
  border:2px dotted #000;
}
.banner {
    border-radius:8px;
    background:#660000;
    height:100px;
    width:60px;
}
.fixed{
    position:fixed;
    top:5px;
}
.bottom {
  position:absolute;
  bottom:0px;
}

HTML

<table class="form" id="form">
  <tr>
    <td valign="top" width="70px">
      <div class="banner"></div>
    </td>
    <td>
    <p>Cum sociis natoque penatibus...</p>
    </td>
  </tr>
</table>
<div style="position:relative;height:500px;">
</div>

in this modified example banner div stays in the screen when scrolling and also stays in the container div.
when the container divs bottom reaches to the top of the screen, banner div returns to relative position and scrolls up with container
http://jsfiddle.net/SeeTS/198/

JQuery

$(window).scroll(function() {
t = $('.form').offset();
t = t.top;
s = $(window).scrollTop();
d = t-s;
b = $('.banner').height();
f = $('.form').height();

if (d < 5) {
    if(d < -(f-b)-15)
        {
        $('.banner').addClass('bottom');
        $('.banner').removeClass('fixed');
        }
    else {
        $('.banner').addClass('fixed');
        $('.banner').removeClass('bottom');
    }
}
else {
    $('.banner').removeClass('fixed');

}
});

CSS

    .form {
    position:relative;
    width:480px;
    margin: 100px auto;
    padding:10px;
}
p {
  border:2px dotted #000;
}
.banner {
    border-radius:8px;
    background:#660000;
    height:100px;
    width:60px;
}
.fixed{
    position:fixed;
    top:5px;
}
.bottom {
  position:absolute;
  bottom:0px;
}

HTML

<table class="form" id="form">
  <tr>
    <td valign="top" width="70px">
      <div class="banner"></div>
    </td>
    <td>
    <p>Cum sociis natoque penatibus...</p>
    </td>
  </tr>
</table>
<div style="position:relative;height:500px;">
</div>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文