Take a look at the interactive example in this fiddle (based on your code), showcasing a functional implementation with a footer height of 100px.
Below is the necessary JavaScript:
$(window).scroll(function () {
if ($(window).scrollTop() + $(window).height() >= $(document).height() - 100) {
$('div#scrolled').css({'bottom' : '100px'});
} else {
$('div#scrolled').css({'bottom' : '0px'});
}
});
This script monitors scrolling to determine if the user has reached within 100 pixels of the bottom, adjusting the position of the fixed element accordingly. To make the footer appear right at the absolute bottom, simply remove the 100 from the if statement:
if ($(window).scrollTop() + $(window).height() >= $(document).height()) { ...
UPDATE:
For a smoother experience, check out the "progressive" version of the code here.
$(window).scroll(function () {
if ($(window).scrollTop() + $(window).height() >= $(document).height() - 100) {
var distance = 100 - ($(document).height() - ($(window).scrollTop() + $(window).height())) + "px";
$('div#scrolled').css({'bottom' : distance});
} else {
$('div#scrolled').css({'bottom' : '0px'});
}
});
Now, instead of a sudden change when nearing the bottom, this version smoothly adjusts the position depending on the scroll position.
I hope you find this helpful!