我有一个简单的页面,其中包含header
和nav
。在窗口滚动中,将header
和nav
设置为fixed
的位置。如果页面足够长,可以滚动,则一切正常。如果页面处于一定高度,我会出现闪烁的情况。
var prev = 0;
var $window = $(window);
var nav = $('.context-nav');
var $navbar = $('.navbar');
$(window).scroll(function(e) {
var scrollTop = $window.scrollTop();
nav.toggleClass('fixed', scrollTop >= prev);
$navbar.toggleClass('fixed', scrollTop >= prev);
prev = scrollTop;
});
* {
box-sizing: border-box;
padding: 0;
margin: 0;
text-decoration: none;
}
html,
body {
font-size: 1rem;
background-image: linear-gradient(to right, #8ca986, #789a71);
}
.sp {
height: 700px;
}
header {
position: relative;
background: #fff;
height: 80px;
text-align: center;
box-shadow: 0 1px 15px rgba(0, 0, 0, 0.04), 0 1px 6px rgba(0, 0, 0, 0.04);
transition: all 500ms cubic-bezier(0.25, 0.46, 0.45, 0.94) 0s;
}
header.fixed {
position: fixed;
top: 0;
left: 0;
right: 0;
height: 60px;
}
.page-container {
padding: 2rem;
position: relative;
color: white;
}
.page-container .context-nav {
display: flex;
flex-direction: row;
color: white;
position: relative;
margin-bottom: 3rem;
padding: 1rem;
transition: all 0.4s ease;
}
.page-container .context-nav.fixed {
position: fixed;
left: 0;
right: 0;
top: 60px;
background-color: rgba(255, 255, 255, 0.08);
}
.page-container .context-nav .nav-item {
padding: 20px;
border: 1px solid white;
margin-right: 1rem;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<header class="navbar">
My header goes here
</header>
<div class="page-container">
<div class="context-nav">
<div class="nav-item">Item 1</div>
<div class="nav-item">Item 2</div>
<div class="nav-item">Item 3</div>
</div>
<h1 class="sp">Scroll me... </h1>
</div>
如果将类.sp
的高度调整为较小的大小,则可以看到问题。在我的示例中,我将其设置为700px
。当我尝试滚动时,它将设置该类,但直接将其删除,这将导致闪烁行为。当我将.sp
类的高度设置为1200px
时,问题就不存在了。
有人有解决此问题的想法吗?
答案 0 :(得分:0)
之所以会发生闪烁,是因为当您将元素设置为固定位置时,它会从流程中移出并改变偏移量,如果您要查找的是在滚动时显示stickyElements,我建议您克隆您要复制的元素想要显示为粘性到固定容器中,然后显示该容器,这样原始元素将保持在其原始位置,并且不会引起内容跳跃,我也注意到您正在使用计数器来显示和隐藏stickyElements,除非您有不同的想法,否则思考是无效的。
这是我迅速从您的代码库中派生出来的Codepen,以显示我的意思:
var $window = $(window);
var $stickyHeader = $('.stickyHeader');
var $nav = $('.context-nav');
var navOffset = $nav.offset().top;
var $navbar = $('.navbar');
var $newNavBar = $navbar.clone();
var $newNav = $nav.clone();
$stickyHeader.append($newNavBar);
$stickyHeader.append($newNav);
var timer;
$(window).scroll(function (e) {
if(timer) {
window.clearTimeout(timer);
}
timer = window.setTimeout(function(){
if($window.scrollTop() > navOffset) {
$stickyHeader.addClass('active');
}
else {
$stickyHeader.removeClass('active');
}
}, 100);
});
* {
box-sizing: border-box;
padding: 0;
margin: 0;
text-decoration: none;
}
html,
body {
font-size: 1rem;
background-image: linear-gradient(to right, #8ca986, #789a71);
}
.sp {
height: 700px;
}
header {
position: relative;
background: #fff;
height: 80px;
text-align: center;
box-shadow: 0 1px 15px rgba(0, 0, 0, 0.04), 0 1px 6px rgba(0, 0, 0, 0.04);
}
.active header {
width: 100%;
height: 60px;
}
.page-container {
padding: 2rem;
position: relative;
color: white;
}
.context-nav {
display: flex;
flex-direction: row;
color: white;
position: relative;
margin-bottom: 3rem;
padding: 1rem;
}
.active .context-nav {
left: 0;
right: 0;
background-color: rgba(255, 255, 255, 0.08);
}
.context-nav .nav-item {
padding: 20px;
border: 1px solid white;
margin-right: 1rem;
}
.stickyHeader {
width: 100%;
position: fixed;
height: 0;
overflow: hidden;
top: -140px;
transition: height 200ms ease, top 200ms ease;
}
.stickyHeader.active {
height: 140px;
top: 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="stickyHeader">
</div>
<header class="navbar">
My header goes here
</header>
<div class="page-container">
<div class="context-nav">
<div class="nav-item">Item 1</div>
<div class="nav-item">Item 2</div>
<div class="nav-item">Item 3</div>
</div>
<h1 class="sp">Scroll me... </h1>
</div>
https://codepen.io/j-lastforone/pen/bzddmK