简体   繁体   English

HTML5-可滚动的Div容器,其中包含部分固定的div

[英]HTML5 - Scrollable Div container with partial fixed divs in it

I'm trying to create an app container which behaves similar to Microsoft Excel. 我正在尝试创建一个行为类似于Microsoft Excel的应用程序容器。 It should scroll both horizontally and vertically, with headers on the left and top which are fixed but scroll with the content. 它应该水平和垂直滚动,左侧和顶部的标头是固定的,但随内容一起滚动。 Something like: 就像是:

+---ScrollableContainer---------+
|+-a-++----b--------------------|-------------------+
||   ||                         |                   |
||   |+-------------------------|-------------------|
||   |+----Content--------------|-------------------+
||   ||                         |                   |
||   ||                         |                   |
+-------------------------------+                   |
 |   ||                                             | 
 |   ||                                             |
 +---++---------------------------------------------+

The ScrollableContainer is the target container of all. ScrollableContainer是所有对象的目标容器。 It's scrollable horizontally and vertically. 它可以水平和垂直滚动。

The content of the ScrollableContainer is 3 div s: a , b and Content . ScrollableContainer的内容为3 div s: abContent The problem I'd like to resolve is that the position of div a must be fixed on left but it have to scroll up and down when ScrollableContainer scrolls up and down. 我想解决的问题是, div a的位置必须固定在左侧,但是当ScrollableContainer上下滚动时,它必须上下滚动。 Also, the div b must be fixed on top but it have to scroll left and right when ScrollableContainer scrolls up and down. 同样, div b必须固定在顶部,但是当ScrollableContainer上下滚动时,它必须左右滚动。 The Content div, finally, is free to scroll in any direction. 最后, Content div可以在任何方向上自由滚动。 Try to imagine an "Agenda". 尝试想象一个“议程”。 The a div is the "HOUR" pivot, the b div is the "DAY" pivot and the Content is the agenda, split by hours and days. a div是“ HOUR”枢轴, b div是“ DAY”枢轴, Content是议程,按小时和天划分。 Pivots had to follow scroll. 枢轴必须跟随滚动。

EDIT: 编辑:

Below is a semi-working example: - AndyM 下面是一个半工作示例:-AndyM

 .rows { height:200px; width:400px; background-color:rgb(240,240,240); position:relative; } .rows > header { position:absolute; top:0; left:0; bottom:0; width:40px; background-color:blue; } .rows > article { position:absolute; top:0; left:40px; bottom:0; right:0; overflow-x:scroll; } .cols { position:relative; height:100%; } .cols header { height:40px; position:absolute; top:0; left:0; background-color:green; } .cols article { position:absolute; top:40px; bottom:0; left:0; overflow-y:scroll; } row { display:block; height:40px; white-space:nowrap; } cell { display:inline-block; height:100%; width:70px; } 
 <section class="rows"> <header> <row> <cell>Text</cell> </row> <row> <cell>Text</cell> </row> <row> <cell>Text</cell> </row> <row> <cell>Text</cell> </row> <row> <cell>Text</cell> </row> <row> <cell>Text</cell> </row> </header> <article> <section class="cols"> <header> <row> <cell>Text</cell> <cell>Text</cell> <cell>Text</cell> <cell>Text</cell> <cell>Text</cell> <cell>Text</cell> <cell>Text</cell> </row> </header> <article> <row> <cell>Text</cell> <cell>Text</cell> <cell>Text</cell> <cell>Text</cell> <cell>Text</cell> <cell>Text</cell> <cell>Text</cell> </row> <row> <cell>Text</cell> <cell>Text</cell> <cell>Text</cell> <cell>Text</cell> <cell>Text</cell> <cell>Text</cell> <cell>Text</cell> </row> <row> <cell>Text</cell> <cell>Text</cell> <cell>Text</cell> <cell>Text</cell> <cell>Text</cell> <cell>Text</cell> <cell>Text</cell> </row> <row> <cell>Text</cell> <cell>Text</cell> <cell>Text</cell> <cell>Text</cell> <cell>Text</cell> <cell>Text</cell> <cell>Text</cell> </row> <row> <cell>Text</cell> <cell>Text</cell> <cell>Text</cell> <cell>Text</cell> <cell>Text</cell> <cell>Text</cell> <cell>Text</cell> </row> <row> <cell>Text</cell> <cell>Text</cell> <cell>Text</cell> <cell>Text</cell> <cell>Text</cell> <cell>Text</cell> <cell>Text</cell> </row> </article> </section> </article> </section> 

Problem with it is that the vertical scroll bar is out of site, and the .col header doesn't scroll. 问题在于垂直滚动条不在站点中,并且.col标头不滚动。 Should be enough to give an idea of what the goal is. 应该足以给出目标是什么的想法。

If you want this sort of scrolling behavior, you will either need to use an iframe or do write custom scrolling in javascript. 如果您想要这种滚动行为,则需要使用iframe或在javascript中编写自定义滚动。 With an iframe, you could just set divs 'a' and 'b' to be fixed in their position, but there could be a few complications. 使用iframe,您可以将div的“ a”和“ b”设置为固定在其位置,但是可能会有一些麻烦。 With javascript, you could write something like this: 使用javascript,您可以编写如下内容:

var x=0, y=0;
ScrollableContainer.onwheel = function(e) {
  if(firefox) {
    x+=e.deltaX*16;
    y+=e.deltaY*16;
  } else {
    x+=e.deltaX;
    y+=e.deltaY;
  }
  a.style.marginTop = y + 'px';
  b.style.marginLeft = x + 'px';
  content.style.marginTop = y + 'px';
  content.style.marginLeft = y + 'px';
};

This has not been tested, but for this to work, divs 'a', 'b', and 'content' must each be in their own container, but you can use these containers to position your divs however you want to. 尚未对此进行测试,但是为了使其正常工作,div'a','b'和'content'必须分别位于各自的容器中,但是您可以根据需要使用这些容器定位div。

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

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