【发布时间】:2014-06-19 22:26:22
【问题描述】:
我在使用某些功能时遇到了一些问题。
我有一个绝对定位在英雄图像上方的标题。当用户向下滚动时,我希望导航从顶部出现并处于固定位置(但滑入)。然后,当用户向上滚动时,导航会在“再次显示英雄导航”之前向上滑动。
基本喜欢这个网站:http://www.invisionapp.com/
我已经设法让它的一半工作(从顶部向下滑动),但只有当标题的初始顶部位置为 0px 时。请参阅:(http://jsfiddle.net/k2jyS/1/) - 但是,它也没有滑回 - 只是突然消失了。
这是我标记的基本大纲
<header>
This is a header
</header>
<div class="hero">
<!-- hero image -->
<img src="http://placekitten.com/g/500/300" />
</div>
<section class="content">
LONG PAGE CONTENT
</section>
CSS
header {
position: absolute;
width: 100%;
top: 40px;
left: 0px;
right: 0px;
background-color: red;
text-align: center;
z-index: 100;
transition: all 0.5s ease;
}
.hero {
height: 300px;
overflow: hidden;
position: relative;
}
.hero img {
display: block;
min-width: 100%;
max-width: none;
position: absolute;
left: 50%;
-webkit-transform: translateX(-50%);
transform: translateX(-50%);
}
.fixed header {
left: 0px;
right: 0px;
position: fixed;
background-color: red;
top: -80px;
border-bottom: solid 1px #e4e4e4;
}
到目前为止的 jQuery
var nav = $('body');
var scrolled = false;
$(window).scroll(function () {
if (200 < $(window).scrollTop() && !scrolled) {
nav.addClass('fixed');
$('header').animate({top: 0},0);
scrolled = true;
}
if (200 > $(window).scrollTop() && scrolled) {
nav.removeClass('fixed');
$('header').removeAttr('style');
scrolled = false;
}
});
这是小提琴示例,我的导航显示距离顶部 40 像素: http://jsfiddle.net/k2jyS/
(由于顶部为 40px,因此从底部开始滑动动画)。
知道如何达到我想要的效果吗?谢谢大家!
EDIT - 这是最终工作的 jQuery。我还必须从标题中删除 CSS 过渡。
var nav = $('body');
var scrolled = false;
$(window).scroll(function () {
if (200 < $(window).scrollTop() && !scrolled) {
nav.addClass('fixed');
$('header').animate({top: 0}, 1000);
scrolled = true;
}
if (200 > $(window).scrollTop() && scrolled) {
$('header').animate({top: -80}, 200, function() {
nav.removeClass('fixed');
$('header').removeAttr('style');
});
scrolled = false;
}
});
【问题讨论】: