【发布时间】:2017-04-18 11:45:21
【问题描述】:
我有一个固定在某个高度的导航栏(通过克隆原始导航栏容器并仅在滚动后显示克隆来完成)。
这个导航栏容器(一个广告)中有一个 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');
}
});
我在正确的轨道上吗?广告和工具栏都是弹性框。导航栏中的链接在悬停时显示一个下拉菜单(这也很好用)。只是无法弄清楚导航广告!
【问题讨论】:
标签: javascript jquery html