Skip to content

Instantly share code, notes, and snippets.

@JDMcKinstry
Forked from bmegod/gist:5731746
Last active December 18, 2015 07:29
Show Gist options
  • Save JDMcKinstry/5746680 to your computer and use it in GitHub Desktop.
Save JDMcKinstry/5746680 to your computer and use it in GitHub Desktop.
;(function($) {
if (!$.aniAutoHeight) {
$.extend({
aniAutoHeight: function(elm, speed) {
if (!speed) speed = 200;
return elm.each(function(index){
var curHeight = $(this).height(),
autoHeight = $(this).css('height', 'auto').height();
$(this).height(curHeight).animate({height: autoHeight}, speed, function() { $(this).height('auto'); });
});
}
});
$.fn.extend({
aniAutoHeight: function(speed) {
return $.aniAutoHeight($(this), speed);
}
});
}
})(jQuery);
var aniHeightSpeed = 1; // this variable controls the speed of all height animations
//**********************************************handle pages except home/contact************************************
function handleAjax(e) {
// get the page we want
getPage = $(this).attr('href');
// make AJAX call
$.get('ajax.php', {page: getPage}, function(data) {
result = $.parseJSON(data);
// fill in new page
$('#subnav').html(result.subnav);
$('#home-copy').html(result.copy);
// change background
$('#background img').attr("src",result.background);
// clear old nav
$('.current_page_item_green').removeClass('current_page_item_green');
$('.current_page_item').removeClass('current_page_item');
//here to do if the page is #about , the navbar text becomes white
// update navigation
if (result.nav_color == 'green') {
// add green
$('.nav-link').addClass('green');
$(result.current_page_item).addClass('current_page_item_green');
}
else {
$('.nav-link').removeClass('green');
$(result.current_page_item).addClass('current_page _item');
}
$('.nav-link').removeClass("open");
resizeCopyOnly();
});
}
function map1(num){
if (num == 1) {
$('#map1').toggle(aniHeightSpeed, function(e) {
if ($(this).is(":visible") && !$("#map2").is(":visible")) {
resizeCopyOnly();
}
else if (!$(this).is(":visible") && !$("#map2").is(":visible")) {
resizeOff();
if (!$("#map2").is(":visible")) {
$('#home-logo').animate({ height: 200 }, aniHeightSpeed);
$('#home-copy').animate({ height: 200, backgroundColor: '#004329', color: 'white', paddingTop: 0 }, aniHeightSpeed);
}
}//end second if
})//end function(e),toggle
}//enf first if
else {
$('#map2').toggle(aniHeightSpeed, function(e) {
if ($(this).is(":visible") && !$("#map1").is(":visible")) {
$('#home-copy').aniAutoHeight(aniHeightSpeed).animate({ backgroundColor: '#004329', color: 'white' }, aniHeightSpeed, function(e) {
$('#home-logo, #home-copy').css("height", "auto"); // reset both content box heights to "auto"
$('#home-logo').animate({ height: 200 }, aniHeightSpeed);
// check height of copy box, make sure it touches bottom, if not, set it to do so
if ($(document).height() > ($('#home-copy').offset().top + $('#home-copy').outerHeight())) {
$('#home-copy').height($(window).height() - $('#home-copy').offset().top);
}
$(window).on("resize", resizeCopyOnly);
});//end function(e)
}
else if (!$(this).is(":visible") && !$("#map1").is(":visible")) {
if (!$("#map1").is(":visible")) {
$('#home-logo').animate({ height: 200 }, aniHeightSpeed);
$('#home-copy').animate({ height: 200, backgroundColor: '#004329', color: 'white', paddingTop: 0 }, aniHeightSpeed);
}
}//end second if
})//end function(e),toggle
}//end else
}//end map1()
function resizeCopyLogo() {
$('#home-logo, #home-copy').css("height", "auto");
clearTimeout(tmrResize);
tmrResize = setTimeout (function() {
$('#home-logo, #home-copy').css("height", "auto");
if ($(document).height() > ($('#home-copy').offset().top + $('#home-copy').outerHeight())) {
var newHeight = $(document).height() - $('#home-copy').offset().top;
$('#home-logo').animate({ height: newHeight }, aniHeightSpeed)
$('#home-copy').animate({ backgroundColor: 'white', color: 'gray', height: newHeight }, aniHeightSpeed);
}
else {
var newHeight = $('#home-copy').height();
$('#home-logo').animate({ height: newHeight }, aniHeightSpeed)
}
$(window).off("resize", resizeCopyLogo).on("resize", resizeCopyLogo);
}, 250);
}
function resizeCopyOnly() {
$('#home-logo, #home-copy').css("height", "auto");
clearTimeout(tmrResize);
tmrResize = setTimeout (function() {
$('#home-logo, #home-copy').css("height", "auto");
if ($(document).height() > ($('#home-copy').offset().top + $('#home-copy').outerHeight())) {
var newHeight = $(document).height() - $('#home-copy').offset().top;
$('#home-copy').height(newHeight);
}
$(window).off("resize", resizeCopyOnly).on("resize", resizeCopyOnly);
}, 250);
}
function resizeOff() {
$(window).off("resize", resizeCopyLogo);
$(window).off("resize", resizeCopyOnly);
}
var tmrResize;
//**********************************************same thing as $(window).load()-handle contact/logo/home************************************
$(function() {
// set initial background image
$('#background img').attr("src", "img/layout/bg_home.jpg");
// set initial heights
$('#home-logo, #home-copy').css("height", "200px");
//****************handle logo//******************handle home
$(document).on("click", "#homelogo, #logo", function(e) {
// get home info
$.get('ajax.php', {page: 'home'}, function(data) {
result = $.parseJSON(data);
// reset background
$('#background img').attr("src",result.background);
// reset navigation
$('.current_page_item_green').removeClass('current_page_item_green');
$('.current_page_item').removeClass('current_page_item');
$('.nav-link').each(function() { $(this).removeClass('green'); });
// reset copy
$('#subnav').html('');
$('#home-copy').html(result.copy);
// reset sizes and colors
$('#home-logo').animate({ height: 200 }, aniHeightSpeed);
$('#home-copy').animate({ height: 200, backgroundColor: '#004329', color: 'white', paddingTop: 0 }, aniHeightSpeed);
});
});
//**************handle contact
$(document).on("click", "#contact-link", function(e) {
$.get('ajax.php', {page: 'contact'}, function(data) {
result = $.parseJSON(data);
// reset background
$('#background img').attr("src",result.background);
// reset navigation
$('.current_page_item_green').removeClass('current_page_item_green');
$('.current_page_item').removeClass('current_page_item');
$('.nav-link').each(function() { $(this).removeClass('green'); });
// reset copy
$('#subnav').html('');
$('#home-copy').html(result.copy);
// reset sizes and colors
$('#home-logo').animate({ height: 200 }, aniHeightSpeed);
$('#home-copy').animate({ height: 200, backgroundColor: '#004329', color: 'white', paddingTop: 0 }, aniHeightSpeed);
});
});
// handle AJAX page calls
$(document).on("click", ".nav-link-ajax", handleAjax);
/// for glitch when clicking on services (sometimes doesn't lose focus)
$(document).on("mouseleave", ".nav-link, .nav-link a", function(e) { $(this).blur(); });
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment