【发布时间】:2019-07-22 21:12:58
【问题描述】:
在滚动到某个点而不牺牲我的 css 布局的其余部分时,我遇到了固定容器 div 位置的问题。
我试图让包装器 profile_overview 和向下滚动时定位的指示器都固定。但是,每次添加相应的 CSS 类时,布局都会被破坏。
$(".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 个块的高度,同时使它们固定在屏幕顶部。这同样适用于下面的指标,也应该固定。
非常感谢任何帮助
【问题讨论】:
-
是的,很抱歉那条评论,我弄错了
标签: jquery css fixed scrolltop