【发布时间】:2016-08-19 11:27:15
【问题描述】:
我刚刚创建了一页网站,并希望修复我的导航栏。目前看起来如图:
为此,我将徽标浮动到左侧,将导航栏浮动到右侧并将位置设置为绝对位置,以便将右侧和底部设置为零。
这是我的 html 和一些 css:
<div class="navigation">
<nav id="site-navigation" class="main-navigation" role="navigation">
<div class="menu-menu-1-container">
<ul id="primary-menu" class="menu">
<li id="menu-item-55" class="menu-item menu-item-type-post_type menu-item-object-page current-menu-item page_item page-item-5 current_page_item menu-item-55"><a href="http://localhost/scentology/">Home</a></li>
<li id="menu-item-107" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-107"><a href="#about">About</a></li>
<li id="menu-item-144" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-144"><a href="#products-and-services">Products & Services</a></li>
<li id="menu-item-142" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-142"><a href="#scent-marketing">Scent Marketing</a></li>
<li id="menu-item-145" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-145"><a href="#clients-and-industries">Clients & Industries</a></li>
<li id="menu-item-143" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-143"><a href="#contact">Contact Us</a></li>
</ul>
</div>
</nav>
</div>
.navigation{
height:40px; line-height:40px;
}
nav#site-navigation {
position: absolute;
right: 0;
bottom: 0;
}
我现在还不太了解脚本,但正在尝试调整这个 sn-p:
<script>
// Create a clone of the menu, right next to original.
$('.navigation').addClass('original').clone().insertAfter('.navigation').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');
}
}
</script>
菜单已修复,但问题是我无法正确看到它,因为我假设的位置属性。我必须更改底部值才能看到它。所以只有当我滚动到底部时才会起作用。当我尝试滚动回顶部时,菜单将不可见。
当我向下滚动时,如何将导航栏固定在顶部,当我向上滚动时,如何将导航栏固定到默认位置?
【问题讨论】:
标签: javascript jquery html css