2

好的 - 我已将其简化为最简单的形式:

<div id="bar>47 px high bar</div>
<div id="header">
    <ul>
        <li>Nav1</li>
    </ul>
</div>

.

//Down
$(function() {
    var interval = setInterval(function() {
        if ($(window).scrollTop() >= 47) {
            $("#header").attr("id","header2");
            clearInterval(interval);
        }
    }, 50);
});
//Up
$(function() {
    var interval = setInterval(function() {
        if ($(window).scrollTop() < 47) {
            $("#header2").attr("id","header");
            clearInterval(interval);
        }
    }, 50);
});

它有效,但仅限于一个方向 - 如果我在页面顶部加载它可以向下滚动。如果我加载到页面的一半,它会向上滚动。在此之后转到另一个方向,没有任何反应。有谁知道为什么?

4

3 回答 3

1

阅读 John Resig(jQuery 的创建者)撰写的这篇关于缓存和计时器的文章。

$(function() {
    var $window = $(window),
        $header = $('#header'),
        scrolled = false;

    $window.scroll(function () {
        scrolled = true;
    });

    setInterval(function() {
        if ( ! scrolled ) return;
        scrolled = false;
        $header.attr('id', $window.scrollTop() < 47 ? 'header' : 'header2');
    }, 50);
});

PS我不确定你到底想在这里完成什么,但似乎你正试图通过CSS定位标题,在这种情况下你最好切换一个类而不是弄乱id:

$header.toggleClass('alternate-header', $window.scrollTop() > 46);

这是小提琴:http: //jsfiddle.net/6KdQG/

于 2012-11-13T00:18:14.253 回答
0

If you clear the interval (clearInterval) what's starting it again, once you scroll back up/down? You have to start it somehow.

You don't need to do it this way. No need for setInterval. window has a scroll event which you can use for this kind of stuff.

An example using the scroll event (jquery)

var $window = $(window),
    $header = $('#header');

$window.on('scroll', function () {
    $header.toggleClass('header2', $window.scrollTop() >= 47);
});​

Here's a fiddle.

于 2012-11-13T00:13:52.533 回答
0

我认为您的目标的工作样本:

    <!DOCTYPE HTML>
    <html lang="en-US">
    <head>
        <meta charset="UTF-8">
        <title></title>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
        <script type="text/javascript">
            var cacheState = $(window).scrollTop(); //detect when scroll has changed
             function downAndUp() {
                if (cacheState != $(window).scrollTop()) {
                    var target = $("li.first").offset().top;
                    if ($(window).scrollTop() >= target) {
                        console.log("Scrolled past li.first");
                        $("#header").addClass("header2");
                        cacheState = $(window).scrollTop();
                    }
                    else {
                        target = $("#bar").outerHeight();
                        if ($(window).scrollTop() <= target) {
                            console.log("Scrolled before li.first");
                            $("#header").removeClass("header2");
                            cacheState = $(window).scrollTop();
                        }
                    }
                }
            } 
            $(document).ready(function(){
                setInterval('downAndUp()',45)
            }); 
        </script>   
        <style type="text/css">
        .header2 {}
        </style>
    </head>
    <body>
    <div id="bar" style="height:300px;">Height:~300px</div>
    <div id="header">
        <ul style="display:block;">
            <li class="first">THIS</li>
            <li>&hellip;</li>
            <li>&hellip;</li>
            <li>&hellip;</li>
            <li>&hellip;</li>
            <li>&hellip;</li>
            <li>&hellip;</li>
            <li>&hellip;</li>
            <li>&hellip;</li>
            <li>&hellip;</li>
            <li>&hellip;</li>
            <li>&hellip;</li>
            <li>&hellip;</li>
            <li>&hellip;</li>
            <li>&hellip;</li>
            <li>&hellip;</li>
            <li>&hellip;</li>
            <li>&hellip;</li>
            <li>&hellip;</li>
            <li>&hellip;</li>
            <li>&hellip;</li>
            <li>&hellip;</li>
            <li>&hellip;</li>
            <li>&hellip;</li>
            <li>&hellip;</li>
            <li>&hellip;</li>
            <li>&hellip;</li>
            <li>&hellip;</li>
            <li>&hellip;</li>       
        </ul>
    </div>
    </body>
    </html>
于 2012-11-13T00:22:07.620 回答