starting to implement a docking nav?

This commit is contained in:
Dave Gamache
2014-12-01 20:05:29 -08:00
parent 4f8392275a
commit a4ab5f6f4e
3 changed files with 156 additions and 83 deletions

View File

@ -1,10 +1,11 @@
$(document).ready(function() {
// Variables
var codeIsActive = true,
$codeSnippets = $('.code-example-body'),
$sections = $('.docs-section'),
var $codeSnippets = $('.code-example-body'),
$nav = $('.navbar'),
$body = $('body'),
$window = $(window),
navOffsetTop = $nav.offset().top,
entityMap = {
"&": "&",
"<": "&lt;",
@ -15,10 +16,24 @@ $(document).ready(function() {
}
function init() {
$('.code-toggler').on('click', toggleCode)
$window.on('scroll', onScroll)
$window.on('resize', resize)
buildSnippets();
}
function resize() {
navOffsetTop = $nav.offset().top
}
function onScroll() {
if(navOffsetTop < $window.scrollTop() && !$body.hasClass('has-docked-nav')) {
$body.addClass('has-docked-nav')
}
if(navOffsetTop > $window.scrollTop() && $body.hasClass('has-docked-nav')) {
$body.removeClass('has-docked-nav')
}
}
function escapeHtml(string) {
return String(string).replace(/[&<>"'\/]/g, function (s) {
return entityMap[s];
@ -32,26 +47,6 @@ $(document).ready(function() {
})
}
function toggleCode() {
var windowScrollTop = $window.scrollTop()
var offsetHeight = windowScrollTop
$sections.each(function (i) {
if($(this).children('.code-example').length > 0) {
var codeExampleHeight = $(this).children('.code-example').outerHeight(true),
sectionBottomPadding = parseInt($('.docs-section').css('padding-bottom'))
if(windowScrollTop > $(this).offset().top + $(this).outerHeight() - sectionBottomPadding) {
if(codeIsActive == false) {
offsetHeight += codeExampleHeight
} else {
offsetHeight -= codeExampleHeight
}
}
}
})
$('body').toggleClass('code-snippets-visible')
codeIsActive = !codeIsActive
$window.scrollTop(offsetHeight)
}
init();