CSS从固定到绝对的转换,顶部的变化不起作用

CSS transition from fixed to absolute with change in top not working

本文关键字:转换 变化 不起作用 顶部 CSS      更新时间:2023-09-26

我的博客有一个固定的顶部标题栏。我希望当用户向下滚动某个特定点时,在第一个标题的顶部显示另一个标题栏。

我实现它与jQuery路径点。

我已经添加了过渡,这样当它出现时它就会向下滑动。

但问题是,当我向上滚动时,转换不会发生。

下面是我的代码:
<div id="share-bar" class="sticky-wrapper">This is second header</div>
<div id="header">This is normal header</div>
<p id="trigger">THIS IS THE PLACE WHERE SECOND BAR COMES UP</p>

JS

$('#trigger').waypoint(function(){
    $( "#share-bar" ).toggleClass("stuck");
});
CSS

#header {
    height: 55px;
    background-color: black;
    color: white;
    position: fixed;
    top: 0;
    width: 100%;
}
#share-bar {
    background: none repeat scroll 0 0 #FFFFFF;
    border-bottom: 1px solid #DDDDDD;
    box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.15);
    float: left;
    height: 55px;
    padding: 0;
    width: 100%;
    transition: all 0.2s ease 0s;
}
.sticky-wrapper { position:absolute; top:-55px; }    
.stuck {
    position: fixed;
    top: 0;
    width: 100%;
    z-index: 999;
    transition: all 0.2s ease 0s;
} 

我有一个工作的小提琴在这里

我的问题是:
  1. 为什么过渡工作从-55px绝对到0固定,而不是反之亦然?

我做错了什么?请引导我。我卡住了。

移动

'position: fixed;'
'z-index: 999;'

to #share-bar and off .stuck修复。

试试这个