Cant figure out what I'm doing wrong here.
Im using jquery to update a variable on screen resize. The purpose of the variable is to fix an overshoot on a scrollTop function when the CSS hits the media query 768px.
Here is the code I have for the resize function:
$(window).resize(function(){
if (window.matchMedia('(max-width: 768px)').matches) {
var scrollovershoot = 40;console.log(scrollovershoot);
} else {
var scrollovershoot = 0;console.log(scrollovershoot);
}
});
Now the function above works exactly as it should in the sense that it logs the correct scrollovershoot variable when the screen size hits 768 or below ie a value of 40. The variable doesn't seem to update in my other scrollTop function though (it doesn't update the scrollTop offset). Here is the code for the scrolling function:
$(".fixed-navigation-follow li a, .see-next a, .more-info-cta, .flipper-container a, #main-atf-cta").click(function (){
var scrollmeto = $(this).attr("href");
$('html, body').animate({
scrollTop: $(scrollmeto).offset().top - scrollovershoot
}, 1000);
return false;
});
When I resize the screen I get the automatic console logging from my first function displaying the correct value however when I stop resizing and just type console.log(scrollovershoot); in the console I get the scrollovershoot is not defined message. Why is this?