我有一個固定在某個高度的導航欄(通過克隆原始導航欄容器並僅在滾動後顯示克隆完成)。在另一個滾動功能中隱藏/顯示滾動條div
此navbar-container(廣告)中有一個div,我想在用戶向下滾動時隱藏,並在向上滾動時再次出現。但是,我沒有取得任何成功!
導航欄的基本HTML:
<div class="toolbar-container">
<div class="nav-ad"> ... </div>
<div class="toolbar"> link 1 • link 2 • link 3 ... </div>
</div>
我的代碼不工作:
$(window).scroll(function() {
if ($(this).scrollTop()>0) {
$('.cloned.nav-ad').fadeOut();
} else {
$('.cloned.nav-ad').fadeIn();
}
});
的jQuery的導航欄克隆(一個很好的解決方案,從http://codepen.io/senff/pen/ayGvD以防止它跳躍):
$('.toolbar-container').addClass('original').clone().insertAfter('.toolbar-container').addClass('cloned').css('position','fixed').css('top','0').css('margin-top','0').css('z-index','500').removeClass('original').hide();
scrollIntervalID = setInterval(stickIt, 10);
function stickIt() {
var orgElementPos = $('.original').offset();
orgElementTop = orgElementPos.top;
if ($(window).scrollTop() >= (orgElementTop)) {
// scrolled past the original position; now only show the cloned, sticky element.
// Cloned element should always have same left position and width as original element.
orgElement = $('.original');
coordsOrgElement = orgElement.offset();
leftOrgElement = coordsOrgElement.left;
widthOrgElement = orgElement.css('width');
$('.cloned').css('left',leftOrgElement+'px').css('top',0).css('width', widthOrgElement).show();
$('.original').css('visibility','hidden');
} else {
// not scrolled past the menu; only show the original menu.
$('.cloned').hide();
$('.original').css('visibility','visible');
}
});
我在正確的軌道上嗎?廣告和工具欄都是flexbox。導航欄中的鏈接顯示懸停下拉(這也很棒)。只是無法弄清楚導航廣告!
@bibs這是否幫助你。 –