You are facing scoping issue, fixDiv()
method being declared inside onload handler. You could use instead:
function fixDiv() {
var $cache = $('.pin');
if ($(window).scrollTop() > 100) $cache.css({
'position': 'fixed',
'top': '10px',
width: $('.pin-wrapper').width()
});
else $cache.css({
'position': 'relative',
'top': 0,
'width': $('#header').width()
});
}
$(window).on('load resize scroll', fixDiv);
But in your posted HTML markup, it appears than there is no element with class header
, looks like you are targeting element with ID header
. But then, #header
is the same element as $cache
, quite confusing.