2017-09-12 5 views
0

Googleをしばらく検索した後、アンカーとアンカーの両方が同じページにある場合、別のサイトのアンカーリンクをクリックした後でアンカーにスムーズにスクロールする

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

しかし、別のサイトでアンカーリンクをクリックすると、問題が解決します。

例:アンカーがある

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

team.php

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

この作品

index.phpののようなアンカーリンクがあります提案されているとおりですが、アニメーションはありません。

私はteam.phpにこのコードを試みたが、あなただけのクエリ文字列を使用し、その上でJavaScriptの行為をさせ、スクロールダウンするためにJavaScriptを使用したい場合は、

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

答えて

0

を動作しません。

<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); 
    } 
}); 
関連する問題