修复一个容器 div 在其他 div 的滚动条上

Fix a container div on scroll of other div

我在滚动到某个点时固定容器 div 的位置时遇到问题,而不会牺牲我的 css 布局的其余部分。

我试图在向下滚动时使包装器 profile_overview 和指示器的位置固定。但是,每次添加相应的 CSS class 时,布局都会被破坏。

$(".profile_right").scroll(function() {
        if ($(".profile_right").scrollTop() <= 1) {
           $('.profile_overview').removeClass('adjust_height_profile');
        } else {
            $('.profile_overview').addClass('adjust_height_profile');
        }
    });
html {
  overflow-y: hidden;
  overflow-x: hidden;
}

body {
  margin: 0px;
  padding: 0px;
  background-color: transparent;
  font-family: 'Arial', sans-serif;
}


::-webkit-scrollbar {
  display: none;
}

/* Styling of the main wrapper + setting up the left and right side of the user.php page */
.main_wrapper {
  width: 100%;
  height: 100vh;
}

/*
*===========================================
*/

/* Left hand side navigation items */
.nav_items_left {
  width: 5%;
  height: 100vh;
  float: left;
  background-color: green;
}

/*
*===========================================
*/

/* Right hand side profile content */
.profile_right {
  width: 95%;
  height: 100vh;
  float: right;
  background-color: yellow;
  overflow: scroll;
  overflow-x: hidden;
}

.header_profiles {
  width: 100%;
  height: 5vh;
}

/*
*===========================================
*/

.profile_overview {
  margin-top: 2%;
  width: 100%;
  height: 40vh;
  display: flex;
  justify-content: space-between;
  transition: 0.4s ease-in-out;
  transition-delay: 1s;
}

.adjust_height_profile {
  height: 10vh;
  transition: 0.2s ease-in-out;
  position: fixed;
}

.profile_container {
  width: 22.5%;
  height: 100%;
  border-radius: 8px;
  background-color: white;
  box-shadow: 0 5px 20px rgba(0,0,0,0.1);
  cursor: pointer;
}

/* Active indicator */
.active_indicator {
  width: 100%;
  height: 0.5vh;
  display: flex;
  justify-content: space-between;
  margin: 1% 0 1% 0;
}

.active_profile_indicator {
  width: 22.5%;
  background-color: lightgrey;
  border-radius: 8px;
}
<!DOCTYPE html>
<html lang="DE">
<head>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

</head>

<body>

<!-- Wrapper around navigation items and profile overview -->
<div class="main_wrapper">

    <!-- Nav items positioned on the left hand side -->
    <div class="nav_items_left">

    </div>


    <!-- Profiles positioned on the right hand side -->
    <div class="profile_right">

        <div class="header_profiles">
          
        </div>


        <!-- ========================================= -->

        <!-- Different blocks -->

        <main>

            <div class="profile_overview">

                <!-- Block 1 -->
                <div class="profile_container">
                </div>

                <!-- Block 2 -->
                <div class="profile_container">
                </div>

                <!-- Block 3 -->
                <div class="profile_container">
                </div>

                <!-- Block 4 -->
                <div class="profile_container">
                </div>

            </div>

            <!-- ========================================= -->

            <!-- Show which profile is active -->
            <div class="active_indicator">
                <div class="active_profile_indicator"></div>
                <div class="active_profile_indicator"></div>
                <div class="active_profile_indicator"></div>
                <div class="active_profile_indicator"></div>
            </div>
            
            <!-- ======================================== --> 
            
            <p>test</p>
            <p>test</p>
            <p>test</p>
            <p>test</p>
            <p>test</p>
            <p>test</p>
            <p>test</p>
            <p>test</p>
            <p>test</p>
            <p>test</p>
            <p>test</p>
            <p>test</p>
            <p>test</p>
            <p>test</p>
            <p>test</p>
            <p>test</p>
            
           
        </main>
    </div>
</div>

</body>
</html>

目标是降低滚动触发的右侧4个块的高度,同时使它们固定在屏幕顶部。同样适用于下方的指标,也应修复。

非常感谢任何帮助

布局 "breaks" 的原因是将 header 设置为 fixed 定位使其脱离正常布局。就像 absolute 定位一样,这意味着它曾经在文档中占据的 space 被删除了,所以你会看到 <p>test</p> 元素向上移动到那个空的 space 添加 adjust_height_profile class 时。

最直接的解决方案是始终将 position: fixed 应用于 profile_overview header,并为其后的元素添加边距。但是,您仍然会在 header 后面看到内容 "scrolling",因为您应用了上边距并且没有指定背景颜色。这意味着当 non-fixed 内容滚动时,它将通过 header 显示。您需要将 header 附加到容器顶部,并将 margin-top: 2% 替换为 padding-top: 2%;

您的 active_indicator 容器不属于 profile_overview header 的原因是什么?

<!DOCTYPE html>
<html>

<head>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

  <style>
    html {
      overflow-y: hidden;
      overflow-x: hidden;
    }
    
    body {
      margin: 0px;
      padding: 0px;
      background-color: transparent;
      font-family: 'Arial', sans-serif;
    }
    
    ::-webkit-scrollbar {
      display: none;
    }
    /* Styling of the main wrapper + setting up the left and right side of the user.php page */
    
    .main_wrapper {
      width: 100%;
      height: 100vh;
    }
    /*
*===========================================
*/
    /* Left hand side navigation items */
    
    .nav_items_left {
      width: 5%;
      height: 100vh;
      float: left;
      background-color: green;
    }
    /*
*===========================================
*/
    /* Right hand side profile content */
    
    .profile_right {
      width: 95%;
      height: 100vh;
      float: right;
      background-color: yellow;
      overflow: scroll;
      overflow-x: hidden;
    }
    
    .header_profiles {
      width: 100%;
      height: 5vh;
    }
    /*
*===========================================
*/
    
    .profile_overview {
      width: 100%;
      height: 40vh;
      display: flex;
      justify-content: space-between;
      transition: 0.4s ease-in-out;
      transition-delay: .5s;
      /* This is changed */
      padding-top: 2%;
      /* margin-top: 2%; */
      position: fixed;
      top: 0;
      background-color: yellow;
    }
    
    .adjust_height_profile {
      height: 10vh;
      transition: 0.2s ease-in-out;
      /*position: fixed;*/
    }
    
    .profile_container {
      width: 22.5%;
      height: 100%;
      border-radius: 8px;
      background-color: white;
      box-shadow: 0 5px 20px rgba(0, 0, 0, 0.1);
      cursor: pointer;
    }
    /* Active indicator */
    
    .active_indicator {
      width: 100%;
      height: 0.5vh;
      display: flex;
      justify-content: space-between;
      margin: 1% 0 1% 0;
    }
    
    .active_profile_indicator {
      width: 22.5%;
      background-color: lightgrey;
      border-radius: 8px;
    }
    /* This is added */
    
    .content {
      margin-top: 40vh;
    }
    
    .adjust_height_profile+.content {
      margin-top: 10vh;
    }
  </style>
</head>

<body>

  <!-- Wrapper around navigation items and profile overview -->
  <div class="main_wrapper">

    <!-- Nav items positioned on the left hand side -->
    <div class="nav_items_left">

    </div>


    <!-- Profiles positioned on the right hand side -->
    <div class="profile_right">

      <div class="header_profiles">

      </div>


      <!-- ========================================= -->

      <!-- Different blocks -->

      <main>

        <div class="profile_overview">

          <!-- Block 1 -->
          <div class="profile_container">
          </div>

          <!-- Block 2 -->
          <div class="profile_container">
          </div>

          <!-- Block 3 -->
          <div class="profile_container">
          </div>

          <!-- Block 4 -->
          <div class="profile_container">
          </div>

        </div>

        <!-- ========================================= -->

        <!-- Show which profile is active -->
        <div class="active_indicator">
          <div class="active_profile_indicator"></div>
          <div class="active_profile_indicator"></div>
          <div class="active_profile_indicator"></div>
          <div class="active_profile_indicator"></div>
        </div>

        <!-- ======================================== -->
        <div class="content">
          <p>test 1</p>
          <p>test</p>
          <p>test</p>
          <p>test</p>
          <p>test</p>
          <p>test</p>
          <p>test</p>
          <p>test</p>
          <p>test</p>
          <p>test</p>
          <p>test</p>
          <p>test</p>
          <p>test</p>
          <p>test</p>
          <p>test</p>
          <p>test</p>
        </div>

      </main>
    </div>
  </div>

</body>

</html>

<script>
  $(".profile_right").scroll(function() {
    if ($(".profile_right").scrollTop() <= 1) {
      $('.profile_overview').removeClass('adjust_height_profile');
    } else {
      $('.profile_overview').addClass('adjust_height_profile');
    }
  });
</script>