0

I can scroll on the top or the bottom of my page. But after 5seconds, I want my scroll div disapear. I use the property delay & hide of Jquery. I must refresh my page to works my code once my div disapear.

$(function () {

        var scrollTrigger = 100, // px
                backToTop = function () {
                    var scrollTop = $(window).scrollTop();
                    if (scrollTop > scrollTrigger) {

                        $("a[href='#top']").addClass('show').delay(2000).hide(500);
                        $("a[href='#bottom']").addClass('show').delay(2000).hide(500);

                    } else {
                        $("a[href='#top']").removeClass('show');
                        $("a[href='#bottom']").removeClass('show');
                    }
                };
        backToTop();

        $(window).on('scroll', function () {
            backToTop();
        });
        $("a[href='#top']").click(function() {
            $("html, body").animate({ scrollTop: 0 }, "slow");
            return false;
        });

        $("a[href='#bottom']").click(function() {
        $('html,body').animate({ scrollTop: 9999 }, 'slow');
        return false;
        });
    });
MasterSinge
  • 665
  • 3
  • 9
  • 22

2 Answers2

0

use the setTimeout() function. ref.
https://www.w3schools.com/jsref/met_win_settimeout.asp, https://www.sitepoint.com/jquery-settimeout-function-examples/

example

               backToTop = function () {

                    $("a[href='#top']").show(500);
                    $("a[href='#bottom']").show(500);

                setTimeout(function() {
                    $("a[href='#top']").hide(500);
                    $("a[href='#bottom']").hide(500);
                }, 5000)
            };
0

Here's a function :

var goToTop = function(){
  window.scrollTo(0,/*y-coordinate (should be 0 if well-designed website)*/);
  setTimeout(function(){$(this).hide(500);},5000);
}

Use it as a click event bind on your button just like :

$(document).ready(
  $(my button JQ selector).click(goToTop);
);

Then, when you scroll you should show(500)'it again.

Vivick
  • 3,434
  • 2
  • 12
  • 25