如何使用jQuery向上或向下滚动页面到锚点?

ade*_*123 168 anchor jquery jquery-plugins slide

我正在寻找一种方法来包含幻灯片效果,当您单击页面上或下的本地锚点链接时.

我想要一个你有这样一个链接的东西:

<a href="#nameofdivetc">link text, img etc.</a>
Run Code Online (Sandbox Code Playgroud)

也许添加了一个类,所以你知道你希望这个链接是一个滑动链接:

<a href="#nameofdivetc" class="sliding-link">link text, img etc.</a>
Run Code Online (Sandbox Code Playgroud)

然后,如果单击此链接,页面将向上或向下滑动到所需位置(可以是div,标题,页面顶部等).


这就是我以前的情况:

    $(document).ready(function(){
    $(".scroll").click(function(event){
        //prevent the default action for the click event
        event.preventDefault();

        //get the full url - like mysitecom/index.htm#home
        var full_url = this.href;

        //split the url by # and get the anchor target name - home in mysitecom/index.htm#home
        var parts = full_url.split("#");
        var trgt = parts[1];

        //get the top offset of the target anchor
        var target_offset = $("#"+trgt).offset();
        var target_top = target_offset.top;

        //goto that anchor by setting the body scroll top to anchor top
        $('html, body').animate({scrollTop:target_top}, 1500, 'easeInSine');
    });
});
Run Code Online (Sandbox Code Playgroud)

dkn*_*ack 405

描述

您可以使用jQuery.offset()和执行此操作jQuery.animate().

查看jsFiddle演示.

样品

function scrollToAnchor(aid){
    var aTag = $("a[name='"+ aid +"']");
    $('html,body').animate({scrollTop: aTag.offset().top},'slow');
}

scrollToAnchor('id3');
Run Code Online (Sandbox Code Playgroud)

更多信息

  • 这也可以是通用的,用于页面中的所有内部锚链接:`$("a [href ^ =#]").click(function(e){e.preventDefault(); var dest = $(this ).attr('href'); console.log(dest); $('html,body').animate({scrollTop:$(dest).offset().top},'slow');}); ` (50认同)
  • 仅供参考@ bardo的解决方案,你应该在使用最新的jQuery时逃避哈希,$("a [href ^ = \\#]")/sf/ask/540226921/ - 当单击-的锚链接/ 18365991#18365991 (5认同)
  • 太棒了!可以使用任何#id或.class (3认同)
  • @bardo 还添加 `history.pushState(null, null, dest);` 因为您正在阻止默认位置哈希更改 (2认同)

San*_*nez 23

假设您的href属性链接到具有相同名称的标记iddiv(通常),您可以使用以下代码:

HTML

<a href="#goto" class="sliding-link">Link to div</a>

<div id="goto">I'm the div</div>
Run Code Online (Sandbox Code Playgroud)

JAVASCRIPT - (Jquery)

$(".sliding-link").click(function(e) {
    e.preventDefault();
    var aid = $(this).attr("href");
    $('html,body').animate({scrollTop: $(aid).offset().top},'slow');
});
Run Code Online (Sandbox Code Playgroud)


小智 8

这让我的生活变得如此简单.你基本上把你的元素id标签和它的卷轴放在它没有很多代码

http://balupton.github.io/jquery-scrollto/

在Javascript中

$('#scrollto1').ScrollTo();
Run Code Online (Sandbox Code Playgroud)

在你的HTML中

<div id="scroollto1">
Run Code Online (Sandbox Code Playgroud)

在这里,我一直在页面上


ipe*_*sus 7

function scroll_to_anchor(anchor_id){
    var tag = $("#"+anchor_id+"");
    $('html,body').animate({scrollTop: tag.offset().top},'slow');
}
Run Code Online (Sandbox Code Playgroud)

  • 真正的问题,+""在第二行做了什么? (3认同)

Mat*_*att 6

您还应该考虑目标具有填充,因此使用position代替offset。您还可以考虑不希望与目标重叠的潜在导航栏。

const $navbar = $('.navbar');

$('a[href^="#"]').on('click', function(e) {
    e.preventDefault();

    const scrollTop =
        $($(this).attr('href')).position().top -
        $navbar.outerHeight();

    $('html, body').animate({ scrollTop });
})
Run Code Online (Sandbox Code Playgroud)


Tim*_*nen 5

我使用 jQuery 的方法只是让所有嵌入的锚链接滑动而不是立即跳转

它与Santi Nunez的答案非常相似,但更可靠

支持

  • 多框架环境。
  • 在页面加载完成之前。
<a href="#myid">Go to</a>
<div id="myid"></div>
Run Code Online (Sandbox Code Playgroud)
// Slow scroll with anchors
(function($){
    $(document).on('click', 'a[href^=#]', function(e){
        e.preventDefault();
        var id = $(this).attr('href');
        $('html,body').animate({scrollTop: $(id).offset().top}, 500);
    });
})(jQuery);
Run Code Online (Sandbox Code Playgroud)