0

私の 1 ページのWeb サイトで、さまざまな div ボックス間をスムーズに移動するためのスクリプトを追加しました。しかし、突然ナビが動かなくなりました。それを壊したのは最後です。

<script>
$(function(){
var sections = {},
    _height  = $(window).height(),
    i        = 0;

// Grab positions of our sections 
$('.section').each(function(){
    sections[this.name] = $(this).offset().top;
});

$(document).scroll(function(){
    var pos = $(this).scrollTop();

    // Look in the sections object and see if any section is viewable on the screen. 
    // If two are viewable, the lower one will be the active one. 
    for(i in sections){
        if(sections[i] > pos && sections[i] < pos + _height){
            $('a').removeClass('active');
            $('#nav_' + i).addClass('active');
            }  
        }
    });
});




        $(".scroll").click(function(event){
            event.preventDefault();
            var full_url = this.href;
            var parts = full_url.split("#");
            var trgt = parts[1];
            var target_offset = $("#"+trgt).offset();
            var target_top = target_offset.top;
            $('html, body').animate({scrollTop:target_top}, 500);
        }); 

</script>  
4

2 に答える 2

0

コンソールにエラーが表示されるline 14

$('#nav').localScroll();

またCannot read property 'top' of undefined エラー

アップデート

id section1による要素がないように見えます

nav_section1である必要があります

この行

$("#"+trgt).offset();

察するに

$("#nav_"+trgt).offset();  OR  $('[href="#' + trgt + "]').offset()

また、値を取得するときに要素が非表示offset()になっていないことを確認してください..

于 2012-12-14T03:43:38.130 に答える
-1

Chrome のインスペクタが気に入らなければなりません...セミコロンが抜けています。

<script type="text/javascript">
$(function(){
    $('#nav').localScroll();
});
</script>

なぜあなたは使用してい<nav>ますか?説明書には、<div>


これを試してください:

<div id="nav">
    <ul>
        <li><a id="nav_section1" href="#section1" class="scroll active">Section 1</a></li>
        <li><a id="nav_section2" href="#section2" class="scroll">&gt;Section 2</a></li>
        <li><a id="nav_section3" href="#section3" class="scroll">&gt;Section 3</a></li>
        <li><a id="nav_section4" href="#section4" class="scroll">&gt;Section 4</a></li>
        <li><a id="nav_section5" href="#section5" class="scroll">&gt;Section 5</a></li>
        <li><a id="nav_section6" href="#section6" class="scroll">&gt;Section 6</a></li>
        <li><a id="nav_section7" href="#section7" class="scroll">&gt;Section 7</a></li>
     </ul>
</div>
于 2012-12-14T03:45:18.907 に答える