$(function() { $(window).on("scroll", function() { if ($(this).scrollTop() > 100) { $(".pagetop").fadeIn("fast"); } else { $(".pagetop").fadeOut("fast"); } scrollHeight = $(document).height(); scrollPosition = $(window).height() + $(window).scrollTop(); footHeight = $("footer").innerHeight(); if ( scrollHeight - scrollPosition <= footHeight ) { $(".pagetop").css({ "position":"absolute", }); } else { $(".pagetop").css({ "position":"fixed", }); } }); });