我可以将 DIV 始终保留在屏幕上,但不始终位于固定位置吗?
我的网站有一个主表单,我想将一个 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
以下解决方案对我有用:
HTML
CSS
The below solution worked for me:
HTML
CSS
我发布了一个示例作为评论,所以我想我会写出一个完整的答案。
标记非常简单,但每个部分都有一些重要的注释。
HTML
CSS
JavaScript
Note #1
滚动标题将使用
position:fixed
附加到页面顶部,但此样式将从内容流中删除内容,这将导致内容跳转,除非其容器保持原来的高度。注意 #2
样式属于 CSS,但是可能很难将某些元素与其原始位置正确对齐。您可能需要通过 JavaScript 动态设置
left
或right
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
CSS
JavaScript
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
orright
css property via JavaScript.截至 2018 年 7 月,是时候重新审视这个问题了。
position: Sticky
正是针对像您这样的问题而引入的,并且具有不错的浏览器支持< /a>.它的工作原理如下:
其中
top
是滚动时 div 应停留在视口顶部的距离。 必须指定top
。其他选项,例如bottom
、left
或right
,都可以<目前无法工作。粘性 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:
where
top
is the distance to the viewport top the div should stay at when you scroll. Specifyingtop
is mandatory. Other options likebottom
,left
orright
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.
你需要 jQuery 或类似的东西,请参阅我的小提琴 此处
编辑
jQuery函数,其中
.form
是内容 div,.banner
是停靠的 divYou'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我创建了一个新的小提琴,希望它有用。 DIV 可以在页面中任意定位,并且在滚动时保持可见。
http://jsfiddle.net/mM4Df/
CSS:
可能有比使用“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/
CSS:
probably there is a better solution than using a "ghost" but I do not know which.
假设标题 div 的顶部位置(到屏幕顶部)一开始是 100px,你可以这样做:
如果窗口滚动顶部超过100px,则将标题div设置为顶部0px固定位置;
如果窗口滚动顶部小于100px,则使用默认布局设置标题div的位置。
对于jquery,它是这样的:
它是通过滚动事件来实现的。
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:
it is implemented with the scroll event.
使用 CSS 来固定其位置。
假设您的
有一个 ID“topdiv”:
请注意,您必须为内容设置 margin-top,因为固定 div 将显示在内容“上方”:
检查此 < a href="http://jsfiddle.net/didierg/29VeC/" rel="nofollow">小提琴。
Use CSS to fix its position.
Assuming your
<div>
has an ID "topdiv":Note you'll have to set a margin-top for the content, because the fixed div will display "over" the content:
Check this fiddle.
听起来您正在寻找的是一个具有两个属性的 header div:
简而言之,有点像“max-top”(它不作为 css 属性存在)。
如果您想完成所有这些,最快的方法可能涉及一些 JavaScript。
尝试这个;如果我稍后有时间,我会用更多代码更新它。
It sounds like what you're looking for is a header div with two properties:
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.
我相信您正在寻找向下滚动时跟随的 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/
你可以试试这个CSS。也许这就是您正在寻找的:
You can try this CSS. Maybe it is what you are looking for:
在此修改后的示例中,滚动时横幅 div 保留在屏幕中,并且也保留在容器 div 中。
当容器div底部到达屏幕顶部时,banner div返回到相对位置并与容器一起向上滚动
http://jsfiddle.net/SeeTS/198/
JQuery
CSS
HTML
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
CSS
HTML