简体   繁体   English

滚动时将div放在页面顶部

[英]keep div on top of the page while scrolling

================== ==================

Name: //html text box// 名称:// html文本框//

age: //text box// 年龄://文本框//

//div// // div //

//table// //表//

================== ==================

Assume the above as a HTML page. 假设以上内容为HTML页面。 Also assume the table has atleast 50 rows so that, the entire page could be scrolled. 还要假定该表至少有50行,以便可以滚动整个页面。 currently, when I scroll the page, the entire page (div, table) scrolls. 当前,当我滚动页面时,整个页面(div,表)都会滚动。 I want the div to be at top of the page while scrolling such as the figure below: 我希望div在滚动时位于页面顶部,如下图所示:

================== ==================

//div// // div //

... ...

... ...

... ...

//row21// // row21 //

//row22// // row22 //

... ...

... ...

================== ==================

I would like to know if this is possible at all. 我想知道这是否完全可能。 I tried using CSS for div: 我尝试将CS​​S用于div:

//CSS for div: // div的CSS:

position: fixed; 位置:固定;

width: 100; 宽度:100;

But, it displays the position of the div exactly where it was earlier. 但是,它会显示div的确切位置。 But, I would like to move the div to the top of the page while scrolling. 但是,我想在滚动时将div移到页面顶部。

Thanks. 谢谢。

This is NOT trivial 这不是小事

  • You will need to use JavaScript to copy div and make its position fixed. 您将需要使用JavaScript复制div并使其位置固定。
  • You will need to handle scroll event to hide and show fixed div 您将需要处理滚动事件以隐藏和显示固定的div

I have a small library to do such thing for table headers , I think you can read the source code or use as-it-is for a table 我有一个小的库可以为表头做这样的事情,我认为您可以阅读源代码或按原样使用表

demo : http://www.agyey.com/demo/stickyhead/demo.html 演示: http : //www.agyey.com/demo/stickyhead/demo.html
code: https://bitbucket.org/anuraguniyal/stickyhead 代码: https//bitbucket.org/anuraguniyal/stickyhead

This is not possible in the CSS alone. 单靠CSS是不可能的。 As you already know you can use: 如您所知,您可以使用:

position: fixed

to keep the element in the same place with respect to the browser window, but in order to move it to the top when the content is scrolled you need to use JavaScript. 要将元素相对于浏览器窗口保持在同一位置,但是要在滚动内容时将其移到顶部,您需要使用JavaScript。

You may want to look at this SO post to get an idea how to achieve that effect. 您可能想看一下这篇SO帖子 ,以了解如何实现该效果。

You need to add this to the css. 您需要将此添加到CSS。

  top:100px;//adjust til the div is below the name and age section.
  position:fixed;

I think that's what you are looking for. 我认为这就是您想要的。

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

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