I have the following code:
$(window).on("load resize",
function(){
stickerHeight = $('#sticker').height();
if ($(window).width() > 480) {
var stickerTop = parseInt($('#sticker').offset().top);
$(window).scroll(function() {
$("#sticker").css((parseInt($(window).scrollTop()) + parseInt($("#sticker").css('margin-top')) > stickerTop) ? {
position: 'fixed',
top: '0px'
} : {
position: 'relative'
});
if ($('#sticker').css('position') === 'fixed') {
$('#page').css('padding-top',stickerHeight + 20);}
else {$('#page').css('padding-top','20px');}
});
}
else {$('#page').css('padding-top','20px');}
});
I'm trying to get the padding of #page to be fixed at 20px if the screen is smaller than 480 px wide. If the page is wider than that, I want it to run this section of the code:
var stickerTop = parseInt($('#sticker').offset().top);
$(window).scroll(function() {
$("#sticker").css((parseInt($(window).scrollTop()) + parseInt($("#sticker").css('margin-top')) > stickerTop) ? {
position: 'fixed',
top: '0px'
} : {
position: 'relative'
});
if ($('#sticker').css('position') === 'fixed') {
$('#page').css('padding-top',stickerHeight + 20);}
else {$('#page').css('padding-top','20px');}
});
But for some reason it is running that code all the time, and is affecting the padding-top value of #page even when the screen is less than 480px. I've picked over the code for about an hour and its driving me crazy, any pointers where i'm going wrong would be great, thank you.