锚链接到隐藏div内的内容

时间:2015-08-19 15:38:54

标签: javascript jquery anchor

我尝试使用

链接到外部网页的内容
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <linux/types.h>
    #include <asm/types.h>
    #include <linux/socket.h>
    #include <asm/socket.h>
    #include <netinet/in.h>
    #include <errno.h>

<a name="anchor"></a>

锚点所在的内容隐藏在抽屉内,使用jQuery:

<a href="/somepage/#anchor"></a>

请参阅小提琴:http://jsfiddle.net/bd1mbu5j/1/

我已经尝试用found here方法包围我,但我知道那里有一些我不知道的东西。

$(".toggle_container").hide();

还应该注意的是,我不只是试图打开抽屉,而是链接到某些抽屉内的特定内容。

1 个答案:

答案 0 :(得分:0)

请参阅此小提琴http://jsfiddle.net/bd1mbu5j/2/

  $(".toggle_container").hide();
  $("h3.trigger").click(function(){
    var _this = this;
    $(this).toggleClass("active").next().slideToggle("normal", function () {
        var anchor = $(_this).data('anchor');
        console.log(anchor);

        if (anchor && $('#'+anchor).length) {
            console.log($('#'+anchor).offset().top);
            $('html, body').animate({scrollTop: $('#'+anchor).offset().top+'px'});
        }
    });

    return false; //Prevent the browser jump to the link anchor
  });

我将目标元素id添加为数据属性,并在幻灯片切换完成后将其选中。然后,如果它存在,我会动画到该元素。

相关问题