You are doing okay, there's just a problem with your if else statements and conditions. If you put this on top if (windowpos >= 670) {
, even if you reached the bottom this condition remains true and will still be executed.
I also see you included a var pos = s.position();
but never really used it, so I think what you may be trying to do is to set sticky if you reached the $("#side-div")
's position.
$(document).ready(function() {
var s = $("#side-div");
var pos = s.offset().top;
$(window).scroll(function() {
var windowpos = $(window).scrollTop() ;
if(windowpos + $(window).height() == $(document).height()){
s.removeClass('stick');
}else if(windowpos >= pos){
s.addClass('stick');
}else{
s.removeClass('stick');
}
});
});
Here is a sample fiddle