在滚动到某个点而又不牺牲其余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个块的高度,同时使它们固定在屏幕顶部。下方的指标也应相同。
我们非常感谢您的帮助
答案 0 :(得分:0)
布局“中断”的原因是因为将标头设置为fixed
定位会使标头脱离常规布局。就像absolute
定位一样,这意味着它过去在文档中所占的空间也被删除了,因此当<p>test</p>
时,您会看到adjust_height_profile
元素移到该空白空间中类已添加。
最直接的解决方案是始终将position: fixed
应用于profile_overview标头,并向其后的元素添加边距。但是,您仍然会看到标题后面的内容“滚动”,因为您已应用了上边距并且未指定背景色。这意味着当非固定内容滚动时,它将通过标题显示。您需要将标题粘贴到容器的顶部,并将margin-top: 2%
替换为padding-top: 2%
;
您的active_indicator容器不是profile_overview标头的一部分吗?
<!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>