简体   繁体   English

单击其他站点上的锚链接后,平滑滚动到锚

[英]Smooth scrolling to an anchor after clicking an anchor link on a different site

After searching google for a while I found several ways to animate the way to an anchor, when both (anchor-link and anchor) are on the same page: 在Google搜索了一段时间后,当两者(anchor-link和anchor)都在同一页面上时,我发现了几种将其锚定的方法:

$('a[href^=#]').on('click', function(e){
  var href = $(this).attr('href');
  $('html, body').animate({scrollTop:$(href).offset().top},'slow');
  e.preventDefault();
});

But i coulnt solve the problem, when the anchor link is clicked on a different site. 但是当在另一个站点上单击锚链接时,我无法解决问题。

EXAMPLE: 例:

index.php there is an anchor link like: index.php有一个类似的锚链接:

<a href="team.php#marketing">marketing team</a>

team.php there is the anchor: team.php有锚点:

<h2 id="marketing">our marketing team</h2>

This works as suggested, but without animation... 这按建议的方式工作,但没有动画...

I tried this code in the team.php but it doesn't work 我在team.php中尝试了此代码,但是它不起作用

$(document).ready(function(){
  $('html,body').animate({scrollTop:$(location.hash).offset().‌​top}, 500);
});

If you want to use JavaScript to scroll down, just use a query string and let JavaScript act on that: 如果要使用JavaScript向下滚动,只需使用查询字符串,然后让JavaScript对其执行操作:

<a href="team.php?target=marketing">marketing team</a>

$(document).ready(function(){
    if (window.location.search === "?target=marketing") {
        var target = $("#marketing");
        $('html,body').animate({
            scrollTop: target.offset().top
        }, 500);
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM