http://communitychessclub.com/index.phpで position:fixed メニューを使用して、ユーザーがスクロールしてもメニューを画面に表示したままにします。
CSS:
#sticky {margin:0 auto; display:table}
#sticky.stick {position: fixed; top: 0; margin-left:48px; z-index: 10000; }
JS:
<script>
function sticky_relocate() {
var window_top = $(window).scrollTop();
var div_top = $('#sticky-anchor').offset().top;
if (window_top > div_top)
$('#sticky').addClass('stick')
else
$('#sticky').removeClass('stick');
}
$(function() {
$(window).scroll(sticky_relocate);
sticky_relocate();
});
$(window).scroll((function() {
var a='', // a buffer to the hash
w = $(window);
return function() {
var h = location.hash;
// if hash is different from the previous one, which indicates
// the hash changed by user, then scroll the window down
// update the buffer
if (h != a) {
a = h;
w.scrollTop(w.scrollTop()-150)
}
};
})());
</script>
しかし、同じページの ID にリンクすると、画面が突然スクロールするという苦情が寄せられています。そこで、 http://www.spydertrap.com/blog/2012/08/user-experience-jquery-smooth-page-anchor-transitions/で「jQuery Smooth Page Anchor Transitions を使用したユーザー エクスペリエンスの向上」を見つけ、デモを気に入りました。とても気に入ったので、スムーズな jquery スクロールのために、以下の jquery コードと上記の jquery コードをブレンドしたいと思います。
彼らはこれを使用します(ただし、他のバージョンがここに存在します...問題は2つを結合する方法です)
jQuery コード:
function goToByScroll(id){
$('html,body').animate({scrollTop: $(id).offset().top},'slow');
}
HTML:
<ul class="nav">
<li><a href="#chapter1">Chapter 1</a></li>
<li><a href="#chapter2">Chapter 2</a></li>
<li><a href="#chapter3">Chapter 3</a></li>
</ul>
JS ランチャー:
$(document).ready(function(){
$('.nav a').click(function(){
goToByScroll($(this).attr('href'));
return false;
});
});
上記のjqueryコードと統合することは可能ですか?