如何用滚动在div中制作一个固定的盒子?

时间:2023-02-09 18:55:54

How I can make a box to be fixed within a div with scroll?

我怎么能用卷轴在一个div中修复一个盒子?

I'm trying like this:

我这样想:

HTML:

HTML:

<div id="wrapper">
    <div class="main">
        <div class="container">
            <div class="container2">
                <div class="test"></div>
                <div class="test"></div>
                <div class="test"></div>
            </div>
        </div>
    </div>
</div>

CSS:

CSS:

#wrapper {
    position: relative;
    width: 100%;
    height: 100%;
    color: #a3265e;
    font-family: 'GillSans-SemiBold';
}
.main {
    border: 1px solid red;
    position: relative;
    width: 100%;
    height: 100%;
    margin: 0 auto;
    padding-top: 380px;
}
.container {
    border: 1px solid green;
    position: relative;
    /*width: 946px;*/
    height: 500px;
    margin: 0 auto;
    overflow: scroll;
}
.container2 {
    height: 1500px;
    margin: 0 auto;
}
.test {
  width: 500px;
  height: 500px;
  position: fixed;
  left: 50%;
  margin-left: -250px;
  background: black;
}

But the box is going along with the page, not only within the div.

但是该框与页面一起,不仅在div中。

What am i doing wrong here??? Can someone show me the way?

我在这做错了什么???有人能给我指路吗?

Thank you guys.

感谢你们。


EDIT

编辑

Example -> https://jsfiddle.net/kzhuh7sv/embedded/result/

示例 - > https://jsfiddle.net/kzhuh7sv/embedded/result/

3 个解决方案

#1


1  

Try this solution https://jsfiddle.net/yyt8eope/2/ I added a div that wraps both the container div and the class='test' div at the same level so the test div can be absolute inside the wrapper and be always at a fixed position

尝试这个解决方案https://jsfiddle.net/yyt8eope/2/我添加了一个div,它将容器div和class ='test'div包装在同一级别,因此测试div在包装器中可以是绝对的并且总是在固定的位置

<div id="wrapper">
    <div class="main">
        <div class="scroll-container">
          <div class="container">
            <div class="container2">
            </div>
          </div>
          <div class="test">Fixed inside scroll container</div>
        </div>
     </div>
</div>

CSS:

CSS:

#wrapper {
position: relative;
width: 100%;
height: 100%;
color: #a3265e;
font-family: 'GillSans-SemiBold';
}
.main {
    border: 1px solid red;
    position: relative;
    width: 100%;
    height: 100%;
    margin: 0 auto;
    padding-top: 380px;
}
.scroll-container{
    position: relative;
    height: 500px;
}
.container {
    border: 1px solid green;
    position: relative;
    /*width: 946px;*/
    height: 500px;
    margin: 0 auto;
    overflow: scroll;
}
.container2 {
    height: 1500px;
    margin: 0 auto;
}
.test {
  width: 500px;
  height: 200px;
  color: white;
  position: absolute;
  top:0;
  left: 50%;
  margin-left: -250px;
  background: black;
  z-index: 1;
}

#2


0  

Try getting rid of 'position: fixed;' and add this 'overflow: scroll;'.

尝试摆脱'位置:固定;'并添加'overflow:scroll;'。

JSFiddle.

的jsfiddle。

EDIT

编辑

Changed the JSFiddle, has been updated.

改了JSFiddle,已经更新了。

#3


0  

You can't do it with position: fixed since it always ties to the viewport. You want it fixed within it's context.

您无法使用position:fixed,因为它始终与视口绑定。你希望它固定在它的上下文中。

http://jsfiddle.net/zq1m49wf/2/

http://jsfiddle.net/zq1m49wf/2/

The black box stays in place as container3 scrolls

当容器3滚动时,黑匣子保持在原位

<div id="wrapper">
    <div class="main">
        <div class="container">
            <div class="container2">
                <div class="container3"></div>
            </div>
            <div class="test"></div>
        </div>
    </div>
</div>

#wrapper {
    position: relative;
    width: 100%;
    height: 100%;
}
.main {
    width: 100%;
    height: 100%;
}
.container {
    position: relative;
    height: 500px;
    padding-top: 200px;
}
.container2 {
    height: 500px;
    margin: 0 auto;
    overflow: scroll;
}
.container3 {
    height: 1500px;
}
.test {
    width: 500px;
    height: 500px;
    bottom: 0;
    background-color: #000000;
    position: absolute;
}

#1


1  

Try this solution https://jsfiddle.net/yyt8eope/2/ I added a div that wraps both the container div and the class='test' div at the same level so the test div can be absolute inside the wrapper and be always at a fixed position

尝试这个解决方案https://jsfiddle.net/yyt8eope/2/我添加了一个div,它将容器div和class ='test'div包装在同一级别,因此测试div在包装器中可以是绝对的并且总是在固定的位置

<div id="wrapper">
    <div class="main">
        <div class="scroll-container">
          <div class="container">
            <div class="container2">
            </div>
          </div>
          <div class="test">Fixed inside scroll container</div>
        </div>
     </div>
</div>

CSS:

CSS:

#wrapper {
position: relative;
width: 100%;
height: 100%;
color: #a3265e;
font-family: 'GillSans-SemiBold';
}
.main {
    border: 1px solid red;
    position: relative;
    width: 100%;
    height: 100%;
    margin: 0 auto;
    padding-top: 380px;
}
.scroll-container{
    position: relative;
    height: 500px;
}
.container {
    border: 1px solid green;
    position: relative;
    /*width: 946px;*/
    height: 500px;
    margin: 0 auto;
    overflow: scroll;
}
.container2 {
    height: 1500px;
    margin: 0 auto;
}
.test {
  width: 500px;
  height: 200px;
  color: white;
  position: absolute;
  top:0;
  left: 50%;
  margin-left: -250px;
  background: black;
  z-index: 1;
}

#2


0  

Try getting rid of 'position: fixed;' and add this 'overflow: scroll;'.

尝试摆脱'位置:固定;'并添加'overflow:scroll;'。

JSFiddle.

的jsfiddle。

EDIT

编辑

Changed the JSFiddle, has been updated.

改了JSFiddle,已经更新了。

#3


0  

You can't do it with position: fixed since it always ties to the viewport. You want it fixed within it's context.

您无法使用position:fixed,因为它始终与视口绑定。你希望它固定在它的上下文中。

http://jsfiddle.net/zq1m49wf/2/

http://jsfiddle.net/zq1m49wf/2/

The black box stays in place as container3 scrolls

当容器3滚动时,黑匣子保持在原位

<div id="wrapper">
    <div class="main">
        <div class="container">
            <div class="container2">
                <div class="container3"></div>
            </div>
            <div class="test"></div>
        </div>
    </div>
</div>

#wrapper {
    position: relative;
    width: 100%;
    height: 100%;
}
.main {
    width: 100%;
    height: 100%;
}
.container {
    position: relative;
    height: 500px;
    padding-top: 200px;
}
.container2 {
    height: 500px;
    margin: 0 auto;
    overflow: scroll;
}
.container3 {
    height: 1500px;
}
.test {
    width: 500px;
    height: 500px;
    bottom: 0;
    background-color: #000000;
    position: absolute;
}