粘贴菜单快速跳到顶部,而标题未完全隐藏或滚动到顶部

时间:2015-04-26 06:07:53

标签: javascript jquery html css css3

我正在尝试使用jquery创建粘性导航。我的导航快速跳到顶部,而标题尚未完全隐藏或滚动到顶部。我希望在导航固定到其位置之前将标题完全滚动到顶部。

问题:滚动后标题流完全隐藏到顶部之后,如何使导航棒固定到其位置?

这是我的jsfiddle链接http://jsfiddle.net/rodgefhi/577br99o/插图。

这是我的代码

<style type="text/css">
    .clearer {
        clear: both;
        margin: 0;
        padding: 0;
    }
    .header {
        width: 84%;
            margin: 0 auto;
    }
    .dummy-content {
        width: 10%;
        margin: auto;
    }
    .fixed {
        position: fixed;
        top: 0;
    }
    .nav-placeholder {
        width: 84%; 
        margin: 0 auto;
        background: transparent;
        height: auto;
    }
    nav {
        width: 84%;
        background: red;
        }
        nav ul li {
            list-style: none;
            float: left;
            padding: 10px;
            text-align: center;
            }
            nav ul li:hover {
                cursor: pointer;
                background: white;
            }
</style>
<script type="text/javascript" src="jquery-1.10.2.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
        var navOffset = $("nav").offset().left;

        $("nav").wrap('<div class="nav-placeholder"></div>');
        $(".nav-placeholder").height($('nav').outerHeight());

        $(window).scroll(function() {
            var scrollPos = $(window).scrollTop();
            $(".value").html(scrollPos);
            if (scrollPos >= navOffset) {
                $("nav").addClass("fixed");
            } else {
                $("nav").removeClass("fixed");
            }
        });
    });
</script>
<div class="header">
    <p>this is header</p>
    <p>this is header</p>
    <p>this is header</p>
    <p>this is header</p>
    <p>this is header</p>
</div>
<nav>
    <ul>
        <li>One</li>
        <li>Two</li>
        <li>Three</li>
        <li>Four</li>
    </ul>
    <div class="clearer"></div>
</nav>
<div class="value"></div>
<div class="dummy-content">
    <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum. Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante. Etiam sit amet orci eget eros faucibus tincidunt. Duis leo. Sed fringilla mauris sit amet nibh. Donec sodales sagittis magna. Sed consequat, leo eget bibendum sodales, augue velit cursus nunc,</p>
</div>

谢谢!

2 个答案:

答案 0 :(得分:1)

您正在使用此获取左侧偏移:

var navOffset = $("nav").offset().left;

我认为你想要顶部值:

var navOffset = $("nav").offset().top;

更新小提琴:http://jsfiddle.net/577br99o/8/

答案 1 :(得分:0)

如果你想要你可以隐藏标题然后在回调函数中添加固定类或删除它像这样

$(document).ready(function() {
        var navOffset = $("nav").offset().left;

        $("nav").wrap('<div class="nav-placeholder"></div>');
        $(".nav-placeholder").height($('nav').outerHeight());

        $(window).scroll(function() {
            var scrollPos = $(window).scrollTop();
            $(".value").html(scrollPos);
            if (scrollPos >= navOffset) {

                $('.header').slideUp(500,function(){
                    $("nav").addClass("fixed");
                });

            } else {
                $('.header').slideDown(500,function(){
                    $("nav").removeClass("fixed");
                });
            }
        });
    });

我希望它有所帮助