In following piece of code, I'm unable to hide the appended div before the user reaches the last paragraph. The div is showing right on pageload, and I'd like to have it show only when the user reaches the "last" ID.
$('body').append('<div id="optslidebox"></div>');
$(".slide-content > p").attr("id", "last");
window.scrollBox = function () {
$(window).scroll(function () {
/* when reaching the element with id "last" we want to show
the slidebox. Let's get the distance from the top to the element */
var distanceTop = window.$('#last').offset().top - window.$(window).height();
if (window.$(window).scrollTop() > distanceTop) {
window.$('#optslidebox').animate({'right': '0px'}, 300);
} else {
window.$('#optslidebox').stop(true).animate({'right': '-430px'}, 100);
}
});
/* remove the slidebox when clicking the cross */
$('#optslidebox.close').bind('click', function () {
$(this).parent().remove();
});
};
More in Technology & Science (4 of 23 articles)
The Social Impact of Scientific Research and new Technologies
Read More »