scrollTo跳转到元素不滚动

时间:2013-09-16 11:02:26

标签: scroll hyperlink scrollto

我在实现scrollTo时遇到了问题。

<div id="container">    
        <section class="rcWrapper">
            <ul>
                <li class="slide" id="slide-1">1</li>
                <li class="slide" id="slide-2">2</li>
                <li class="slide" id="slide-3">3</li>
                <li class="slide" id="slide-4">4</li>
                <li class="slide" id="slide-5">5</li>
            </ul>
        </section>

        <nav id="navigation">
            <a href="#slide-1">slide-1</a>
            <a href="#slide-2">slide-2</a>
            <a href="#slide-3">slide-3</a>
            <a href="#slide-4">slide-4</a>
            <a href="#slide-5">slide-5</a>
        </nav>
    </div><!-- end of container -->

这是我的js:

$(function(){
    $('#navigation a:first-child').click(function(){
        $(window).scrollTo('#slide-1', 800);
    });
});

我可能错过了一些东西,但我看不到它。任何人??谢谢!

2 个答案:

答案 0 :(得分:0)

   .click(function(){    
     $(window).animate({
        scrollTop: $('#slide-1').offset().top,
        // scrollLeft: $('#slide-1').offset().left
     }, 1000);
     return false; // <- don't forget about this line, because if it won't be there, browser will follow link.
   });

答案 1 :(得分:0)

不用JS代码试试。

<div id="container">    
    <section class="rcWrapper">
        <ul>
            <li class="slide" id="slide-1">1</li>
            <li class="slide" id="slide-2">2</li>
            <li class="slide" id="slide-3">3</li>
            <li class="slide" id="slide-4">4</li>
            <li class="slide" id="slide-5">5</li>
        </ul>
    </section>

    <nav id="navigation">
        <a href="#slide-1">slide-1</a>
        <a href="#slide-2">slide-2</a>
        <a href="#slide-3">slide-3</a>
        <a href="#slide-4">slide-4</a>
        <a href="#slide-5">slide-5</a>
    </nav>
</div><!-- end of container -->

属性href =“id_element”