锚定导航 - 目标div应始终位于容器顶部

时间:2014-06-09 19:19:03

标签: javascript jquery html css

我想实现下一个行为: 当我点击链接到锚点时 - 目标div应该重新定位在他的容器顶部顶部(如果需要,应该增加容器的高度)。 这是一个“html / css - draft” - http://jsfiddle.net/k2dGD/

请用javascript帮我。

HTML:

 <div class="triggers">
    <a href="#one">One</a>
    <a href="#two">Two</a>
    <a href="#three">Three</a>
    <a href="#four">Four</a>
    <a href="#five">Five</a>
</div>

    <div class="blocks">
    <div id="one" class="block">
        BLOCK ONE --- Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptatem exercitationem dolores officiis omnis nostrum molestiae quo laboriosam iure placeat velit sit porro illum eius repellendus suscipit nemo dignissimos! Delectus atque.
    </div>
    <div id="two" class="block">
        BLOCK TWO --- Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptatem exercitationem dolores officiis omnis nostrum molestiae quo laboriosam iure placeat velit sit porro illum eius repellendus suscipit nemo dignissimos! Delectus atque.
    </div>
    <div id="three" class="block">
        BLOCK THREE --- Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptatem exercitationem dolores officiis omnis nostrum molestiae quo laboriosam iure placeat velit sit porro illum eius repellendus suscipit nemo dignissimos! Delectus atque.
    </div>
    <div id="four" class="block">
        BLOCK FOUR --- Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptatem exercitationem dolores officiis omnis nostrum molestiae quo laboriosam iure placeat velit sit porro illum eius repellendus suscipit nemo dignissimos! Delectus atque.
    </div>
    <div id="five" class="block">
        BLOCK FIVE --- Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptatem exercitationem dolores officiis omnis nostrum molestiae quo laboriosam iure placeat velit sit porro illum eius repellendus suscipit nemo dignissimos! Delectus atque.
    </div>
</div>

1 个答案:

答案 0 :(得分:0)

$(function(){

    $('.triggers a').click(function(){
        var $item = $( $(this).attr('href') );
        $(this).closest('div').next('.blocks').prepend( $item );
    });

});