要素を調べることで、多くの掘り下げを行うことができます。私はナビゲーションを見てみました、そしてそれはのid
を持っていdevdoc-nav
ます。doc.js
リソースに移動すると、次のコードを含むjsファイルがあります。
// Set up fixed navbar
var prevScrollLeft = 0; // used to compare current position to previous position of horiz scroll
$(window).scroll(function(event) {
if ($('#side-nav').length == 0) return;
if (event.target.nodeName == "DIV") {
// Dump scroll event if the target is a DIV, because that means the event is coming
// from a scrollable div and so there's no need to make adjustments to our layout
return;
}
var scrollTop = $(window).scrollTop();
var headerHeight = $('#header').outerHeight();
var subheaderHeight = $('#nav-x').outerHeight();
var searchResultHeight = $('#searchResults').is(":visible") ?
$('#searchResults').outerHeight() : 0;
var totalHeaderHeight = headerHeight + subheaderHeight + searchResultHeight;
var navBarShouldBeFixed = scrollTop > totalHeaderHeight;
var scrollLeft = $(window).scrollLeft();
// When the sidenav is fixed and user scrolls horizontally, reposition the sidenav to match
if (navBarIsFixed && (scrollLeft != prevScrollLeft)) {
updateSideNavPosition();
prevScrollLeft = scrollLeft;
}
// Don't continue if the header is sufficently far away
// (to avoid intensive resizing that slows scrolling)
if (navBarIsFixed && navBarShouldBeFixed) {
return;
}
if (navBarIsFixed != navBarShouldBeFixed) {
if (navBarShouldBeFixed) {
// make it fixed
var width = $('#devdoc-nav').width();
$('#devdoc-nav')
.addClass('fixed')
.css({'width':width+'px'})
.prependTo('#body-content');
// add neato "back to top" button
$('#devdoc-nav a.totop').css({'display':'block','width':$("#nav").innerWidth()+'px'});
// update the sidenaav position for side scrolling
updateSideNavPosition();
} else {
// make it static again
$('#devdoc-nav')
.removeClass('fixed')
.css({'width':'auto','margin':''})
.prependTo('#side-nav');
$('#devdoc-nav a.totop').hide();
}
navBarIsFixed = navBarShouldBeFixed;
}
resizeNav(250); // pass true in order to delay the scrollbar re-initialization for performance
});
var navBarLeftPos;
if ($('#devdoc-nav').length) {
setNavBarLeftPos();
}
起こっていることすべてを必ずしも理解する必要はありませんが、私があなたに示したい基本的な考え方は、あなたが見るすべてがCSSを通して行われるということです。javascriptは、そのCSSを適切なタイミングで適用するために使用されます。ユーザーが特定のポイントを超えてスクロールすると、Webページはと呼ばれるクラスを適用しfixed
ます(これはで見つけることができますdefault.css
)
#devdoc-nav.fixed {
position: fixed;
margin:0;
top: 20px; }
そうそう、うまくいけば、ここからあなたはアイデアを得ることができます。これを行うには多くの方法がありますが、この例を示したので、私は単にそれらが持っているコードを示しており、おそらくそれから何かを引き出すことができます。ヘルプが必要な場合は、遠慮なくコメントしてください。