I need a div
element to be displayed as a "scroll back to top" button on my webpage when the user has scrolled past a certain point. To achieve this, I am using the JavaScript code provided below:
$(document).ready(function() {
$(window).scroll(function() {
var scrollVal = $(this).scrollTop();
if ( scrollVal > 1500) {
$('#backtotop').css({'display':'block'});
} else {
$('#backtotop').css({'display':'none'});
}
});
});
The script works well, except for the fact that the div only appears while actively scrolling. The CSS style for #backtotop
is set to "display:none" in the stylesheet. If I change it to "display:block", then the issue becomes that the div is always visible and fades out when scrolling above the 1500 value, only to reappear when scrolling stops.
In essence, I want the div to appear when reaching a scroll point, as it currently does, but I also want it to remain visible even when scrolling stops, which is not happening at the moment.