当用户向下滚动时,我无法找到创建不同导航菜单的方法。我正在寻找这样的东西。 http://www.kamellazaarfoundation.org
当用户向下滚动时,我希望#small-menu替换#big-menu。
<div id="container">
<div id="big-menu">
<div id="big-logo">Big Logo</div>
<div id="big-navigation">Navigation</div>
<div id="big-search-bar">Search</div>
</div>
<!--<div id="small-menu"> ---Small Menu I want to replace the big menu with when user scrolls down---
<div id="small-logo">Small Logo</div>
<div id="small-navigation">Navigation</div>
<div id="small-search-bar">Search</div>
</div>-->
<div id="content"></div>
</div>
#container {
width: 600px;
border: 1px solid green;
padding: 10px;
}
#big-menu {
height: 100px;
border: 1px solid red;
}
#big-logo {
width: 100px;
height: 80px;
border: 1px solid blue;
margin-top: 10px;
float: left;
}
#big-navigation {
width: 300px;
height: 20px;
border: 1px solid orange;
float: left;
margin: 70px 0 0 10px;
}
#big-search-bar {
width: 150px;
height: 20px;
border: 1px solid pink;
float: right;
margin: 70px 10px 0 0;
}
#small-menu {
height: 60px;
border: 1px solid teal;
}
#small-logo {
height: 60px;
width: 60px;
float: left;
border: 1px solid red;
}
#small-navigation {
width: 300px;
height: 20px;
border: 1px solid black;
margin: 30px 0 0 10px;
float: left;
}
#small-search-bar {
width: 150px;
height: 20px;
border: 1px solid aqua;
float: right;
margin: 30px 10px 0 0;
}
#content {
height: 1200px;
margin-top: 10px;
border: 1px solid purple;
}
$(function() {
// Stick the #nav to the top of the window
var nav = $('#big-menu');
var navHomeY = nav.offset().top;
var isFixed = false;
var $w = $(window);
$w.scroll(function() {
var scrollTop = $w.scrollTop();
var shouldBeFixed = scrollTop > navHomeY;
if (shouldBeFixed && !isFixed) {
nav.css({
position: 'fixed',
top: 0,
left: nav.offset().left,
width: nav.width()
});
isFixed = true;
}
else if (!shouldBeFixed && isFixed)
{
nav.css({
position: 'static'
});
isFixed = false;
}
});
});
答案 0 :(得分:1)
不要使用大型DOM替换,而应考虑使用classNames和CSS,f.ex:
<div id="container">
<div id="menu">
<div id="logo">Big Logo</div>
<div id="navigation">Navigation</div>
<div id="earch-bar">Search</div>
</div>
<div id="content"></div>
</div>
和JS一样:
$(window).scroll(function() {
var isSmall = /* true/false; detect scrolling distance */;
$('#menu').toggleClass('small', isSmall);
})
当检测到某个滚动距离时,这会将类small
切换到#menu
容器。 CSS可能看起来像:
#logo{height:100px} /* default (big) */
#menu.small #logo{height:60px} /* small */
这些只是示例,您需要自己确定设计的实现:)
使用CSS还可以为您提供以后进行花式施法动画的好处。
答案 1 :(得分:1)
这将是你的jQuery代码:
var $document = $(document),
$element = $('.fixed-menu'),
className = 'hasScrolled';
$document.scroll(function() {
if ($document.scrollTop() >= 100) {
$element.addClass(className);
} else {
$element.removeClass(className);
}
});
我在这里设置jsFiddle为例