This code achieves the affect, but is it a resource friendly way of doing it?
Just to clarify: target div.fixMe and fix it to the top of the window when the user scrolls past its natural position. (I do realise there are plugins to achieve this result, but I wanted to build it myself)
Is this the best way to achieve the desired result? Is it a good idea that the whole function triggers whenever the user scrolls, or should I be splitting up code?
(function() {
var fixedElement = $('.fixMe').offset(),
scrolled = $(window).scroll(function() {
var winScrolled = $(this).scrollTop();
if(winScrolled > fixedElement.top - 10) {
$('.fixMe').css({'position': 'fixed','top' : '10px'})
}
else {
$('.fixMe').css({'position': 'static'})
}
});
})()
any feedback/criticism welcome. I should probably get/set the width of div.fixMe to avoid display issues.