简体   繁体   English

我如何在左div完成滚动之前固定左DIV

[英]How can I fix a left hand DIV while until the right hand div has finished scrolling

I have a test web page with three sections. 我有一个包含三个部分的测试网页。 The first and third sections are both full page size (100% width and height). 第一部分和第三部分均为完整页面尺寸(宽度和高度均为100%)。 The center section however, is split into two halves with a title on the left and content on the right. 但是,中间部分分为两半,标题在左侧,内容在右侧。 What I'm trying to achieve is that when you scroll to the second section, the left hand side locks into place when it's scrolled to the top of the screen and then continues to scroll once the content on the right has been passed. 我要实现的目标是,当您滚动到第二部分时,当左侧滚动到屏幕顶部时,左侧将锁定到位,然后在右侧的内容通过后继续滚动。

I've so far attempted to achieve this with Bootstraps affix property but can't seem to get that to work no matter what I try. 到目前为止,我一直试图通过Bootstraps的affix属性来实现这一点,但是无论我如何尝试,似乎都无法使它正常工作。 This is my test markup... 这是我的测试标记...

<!DOCTYPE html>
<html lang="en">
<head>
    <title>TEST</title>
    <link href="style.css" rel="stylesheet">
</head>
<body>

    <!-- Section 1 -->
    <section class="fullpage" style="background-color:#0ff;">
    </section>

    <!-- Section 2 -->
    <section class="fullpage">

        <div class="sectiontitle">
            <h1>THIS SHOULD STAY IN PLACE</h1>
            <h1>UNTIL GREEN GETS TO BOTTOM</h1>
        </div>

        <div class="sectiondetail">
            <p>Hello world this is a test</p><p>Hello world this is a test</p><p>Hello world this is a test</p>
        </div>

    </section>

    <!-- Section 3 -->
    <section class="fullpage" style="background-color:#ff0;">
    </section>

</body>
</html>

And the CSS I'm using... 我正在使用的CSS ...

html, body{
  height:100%;
  margin:0;
  padding:0;
}
h1, h2, h3, p{
  margin:0;
  padding:0;
}
h1{
  padding:50px 0 50px 50px;
}
p{
  padding:20px;
}

.fullpage{
  min-height:100vh;
  width:100%;
  position:relative;
  display:block;
}

.sectiontitle{
  position:absolute;
  width:50%;
  left:0px;
  background-color:#f00;
  height:100vh;
}

.sectiondetail{
  position:relative;
  width:50%;
  left:50%;
  background-color:#0f0;
  height:200vh;
}

I'd actually prefer to use Bootstrap but is there a nice easy way to achieve this with just CSS and/or JQuery? 我实际上更喜欢使用Bootstrap,但是有没有一种很好的简便方法仅使用CSS和/或JQuery来实现呢?

EDIT: Found this page which does something similar to what I'm looking for. 编辑:找到此页面,该页面与我正在寻找的内容相似。 Just scroll down beyond the first page. 只需向下滚动到第一页即可。

Example website with required behaviour 具有所需行为的示例网站

Thanks in advance, Simon 预先感谢西蒙

you can try following this is with pure css idea is to tweak with z-indexes.if you need more smooth and clean effect you can use jquery or parallax plugins 您可以尝试使用纯css跟随此操作,想法是使用z-indexes进行调整。如果您需要更平滑和干净的效果,则可以使用jquery或视差插件

 html, body { height: 100%; margin: 0; padding: 0; } h1, h2, h3, p { margin: 0; padding: 0; } h1 { padding: 50px 0 50px 50px; } p { padding: 20px; } .fullpage { min-height: 100vh; width: 100%; position: relative; display: block; } .sectiontitle { position: absolute; width: 50%; left: 0px; background-color: #f00; height: 100vh; } .sectiondetail { position: relative; width: 50%; left: 50%; height: 100vh; } .magic { width: 100%; position: absolute; top: 0; left: 0; z-index: 999; overflow-x: scroll; overflow-y: none; } 
 <section class="fullpage" style="background-color:#0ff;"> </section> <!-- Section 2 --> <section class="fullpage" style="position:relative;background:green;"> <div class="sectiontitle"> <h1>THIS SHOULD STAY IN PLACE</h1> <h1>UNTIL GREEN GETS TO BOTTOM</h1> </div> <div class="magic"> <div class="sectiondetail"> <p>Hello world this is a test</p> <p>Hello world this is a test</p> <p>Hello world this is a test</p> <p>Hello world this is a test</p> <p>Hello world this is a test</p> <p>Hello world this is a test</p> <p>Hello world this is a test</p> <p>Hello world this is a test</p> <p>Hello world this is a test</p> <p>Hello world this is a test</p> <p>Hello world this is a test</p> <p>Hello world this is a test</p> <p>Hello world this is a test</p> <p>Hello world this is a test</p> <p>Hello world this is a test</p> <p>Hello world this is a test</p> <p>Hello world this is a test</p> <p>Hello world this is a test</p> <p>Hello world this is a test</p> <p>Hello world this is a test</p> <p>Hello world this is a test</p> <p>Hello world this is a test</p> <p>Hello world this is a test</p> <p>Hello world this is a test</p> <p>Hello world this is a test</p> <p>Hello world this is a test</p> <p>Hello world this is a test</p> <p>Hello world this is a test</p> <p>Hello world this is a test</p> <p>Hello world this is a test</p> <p>Hello world this is a test</p> <p>Hello world this is a test</p> <p>Hello world this is a test</p> </div> </div> </section> <!-- Section 3 --> <section class="fullpage" style="background-color:#ff0;"> </section> 

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

相关问题 如何在div左右手的HR中设置样式? - How to style in HR on left and right hand side of div? 如何让一个文本显示在左侧,另一个显示在div的右侧 - how to have one piece of text display on left hand side and the other on the right hand side of a div 如何在左侧放置 div 滚动条? - How to position a div scrollbar on the left hand side? 如何将我的文本更改为在左侧,将表格更改为在右侧 - How can i change my Text to be on left hand and form to be on right hand 如何使左侧div中的文本围绕右侧的关闭按钮流动(也可以在IE8中使用!) - How to get the text in the left-hand div flowing around the close button on the right (to work in IE8 as well!) 如何在左下角放置DIV? - How to place a DIV regarding to lower left hand-side corner? 如何在页面右下方添加Div - How to add Div at the page bottom right hand side 如何在页面的右上角放置div - How to place div in top right hand corner of page div将显示在右上角,而不考虑分辨率和最小化浏览器,并且不应将其叠加在左侧div上 - div to be show on the Top Right Corner irrespective of resolution and when browser is Minimized and it should not overlay on the Left hand side div 滚动时如何使div同时向右和向左粘 - How to make div sticky to the right and to the left at the same time while scrolling
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM