1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 // Switches the sidebar between floating on the left and position:fixed
8 // depending on whether it's scrolled into view, and manages the scroll-to-top
9 // button: click logic, and when to show it.
12 var sidebar = document.getElementById('gc-sidebar');
13 var scrollToTop = document.getElementById('scroll-to-top');
14 var offsetTop = sidebar.offsetTop;
17 // Obviously, this code executes every time the window scrolls, so avoid
18 // putting things in here.
19 var isFloatingSidebar = sidebar.classList.contains('floating');
20 var isShowingScrollToTop = !scrollToTop.classList.contains('hidden');
22 var floatSidebar = false;
23 var showScrollToTop = false;
25 if (window.scrollY > offsetTop) {
26 // Scrolled past the top of the sidebar.
27 if (window.innerHeight >= sidebar.scrollHeight) {
28 // The whole sidebar fits in the window. Make it always visible.
31 // Whole sidebar doesn't fit, so show the scroll-to-top button instead.
32 showScrollToTop = true;
36 if (floatSidebar != isFloatingSidebar)
37 sidebar.classList.toggle('floating', floatSidebar);
38 if (isShowingScrollToTop != showScrollToTop)
39 scrollToTop.classList.toggle('hidden', !showScrollToTop);
42 window.addEventListener('scroll', relayout);
43 setTimeout(relayout, 0);
45 scrollToTop.addEventListener('click', function() {
46 window.scrollTo(0, 0);