javascript - 将一个 div 分成两部分,一半显示在固定 div 下方,一半显示在上方

标签 javascript jquery css html

我在页面中间有一个固定的 div。我有另一个包含图片库的 div。目前,图像按预期滚动“undearneath”固定的 div。但我想要一种图像永远不会隐藏在固定 div 下方的效果。与其深入到 div 之下,不如将它们分开,图像的一部分显示在下方,一部分显示在上方。本质上,我希望将一个 div(带有图像库的那个)分成两部分,一部分显示在固定 div 下方,一部分显示在上方。这里有一些精美的图形可以更好地说明我希望做什么。

我很乐意使用所需的 css/html/javascript 的任意组合。

这可能吗?

  Current Behavior           Desired Behavior
|===================|     |===================|
|  =====    =====   |     |  =====    =====   |
| |     |  |     |  |     | |     |  |     |  |
| | img |  | img |  |     | | img |  | img |  |
| |  1  |  |  2  |  |     | |  1  |  |  2  |  |
|  =====    =====   |     |  =====    =====   |
|===================|     |===================|
|   fixed   div     |     |   fixed   div     |
|===================|     |===================|
| |  3  |  |  4  |  |     |  =====    =====   |
|  =====    =====   |     | |     |  |     |  |
|  =====    =====   |     | | img |  | img |  |
| |     |  |     |  |     | |  3  |  |  4  |  |
| | img |  | img |  |     |  =====    =====   |
|===================|     |===================|

滚动一下

|===================|     |===================|
| | img |  | img |  |     | | img |  | img |  |
| |  1  |  |  2  |  |     | |  1  |  |  2  |  |
|  =====    =====   |     |  =====    =====   |
|  =====    =====   |     |  =====    =====   |
| |     |  |     |  |     | |     |  |     |  |
|===================|     |===================|
|   fixed   div     |     |   fixed   div     |
|===================|     |===================|
|  =====    =====   |     | | img |  | img |  |
| |     |  |     |  |     | |  3  |  |  4  |  |
| | img |  | img |  |     |  =====    =====   |
| |  5  |  |  6  |  |     |  =====    =====   |
|  =====    =====   |     | |     |  |     |  |
|===================|     |===================|

再滚动一点

|===================|     |===================|
|  =====    =====   |     |  =====    =====   |
|  =====    =====   |     |  =====    =====   |
| |     |  |     |  |     | |     |  |     |  |
| | img |  | img |  |     | | img |  | img |  |
| |  3  |  |  4  |  |     | |  3  |  |  4  |  |
|===================|     |===================|
|   fixed   div     |     |   fixed   div     |
|===================|     |===================|
| | img |  | img |  |     |  =====    =====   |
| |  5  |  |  6  |  |     |  =====    =====   |
|  =====    =====   |     | |     |  |     |  | 
|  =====    =====   |     | | img |  | img |  |
| |     |  |     |  |     | |  5  |  |  6  |  |
|===================|     |===================|

最佳答案

在这里,我快速组合了 html、css 和 jquery,它们在 Chrome 中对我有用。

jsfiddle demo

html:

    <div id="toppart">
        <div class="tiles">
            <div class="tile">1</div>
            <div class="tile">2</div>
            <div class="tile">3</div>
            <div class="tile">4</div>
            <div class="tile">5</div>
            <div class="tile">6</div>
        </div>
    </div>
    <div id="strap">
        <p>the fixed strap</p>
    </div>
    <div id="bottompart"></div>

css 的关键部分:

#strap {
    position:absolute;
    top:45%;
    height: 10%;
    background-color:blue;
}
#toppart, #bottompart {
    background-color:#fff;
    position:absolute;
    height:45%;
    overflow:auto;
}
#bottompart {
    top:55%;
    z-index:-1;
}

#bottompart div {
    position:relative;
    top:-100%;
}

javascript代码:

    $(document).ready(function () {
          //mirror contents
          $('#bottompart').append($('#toppart').html());
          //scroll bottom with top
          $("#toppart").scroll(function(){
                 $("#bottompart").scrollTop($("#toppart").scrollTop());
          });
          //scroll top with bottom
          $("#bottompart").scroll(function(){
                 $("#toppart").scrollTop($("#bottompart").scrollTop());
          });
    });

希望你明白了 ;-)

关于javascript - 将一个 div 分成两部分,一半显示在固定 div 下方,一半显示在上方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15991212/

相关文章:

javascript - 从存储在对象中的回调调用函数,在数组中

javascript - Uncaught TypeError : modal. on(...).trigger 在使用 Zurb Foundation 5.5.3 时不是函数?

css - 为什么 margin 是: 0 auto centering text on image without width specified?

javascript - 使用 puppeteer 在页面中基于文本的值搜索中使用正则表达式

javascript - 如何加入拆分词?

javascript - 如何清除 RTCPeerConnection (WebRTC)?

javascript - 如何在 jquery 中使用 javascript 实例/通过 jquery 选择 javascript 句柄?

javascript - 调整 html 表格列的大小

html - 每个属性的类

javascript - 当标题长度不同时如何使用CSS对齐html文本