Skip to content Skip to sidebar Skip to footer

How To Show "back To Top" Button Using Jquery Only If Browser Height Is Shorter Than Page?

How to add/show 'back to top' button at bottom in a div using jquery only if height browser height is shorter than page, other wise it should be hidden?

); if (wrapper.outerHeight(true) > $(window).height()) { wrapper.append('<p><a href="#' + wrapper.attr('id') + '">Back to top</a></p>'); }

Solution 2:

Do something like this:

$(document).ready(function(){
    showHideControl();
    $(window).resize(function(){
        showHideControl();
    });        
});

functionshowHideControl() {
    var h = $(window).height();
    var ch = $("#content").height();
    if (ch < h) {
        $("#backControl").hide();
    }
    else {
        $("#backControl").show();
    }
}

The html needs to be updated a little too:

<divid="mainwrapper"><divid="content"><p> Paragraph 1 </p><p> Paragraph 1 </p><p> Paragraph 1 </p><p> Paragraph 1 </p></div><pid="backControl"><ahref="#mainwrapper">Back to top</a></p></div>

Post a Comment for "How To Show "back To Top" Button Using Jquery Only If Browser Height Is Shorter Than Page?"