Return to top button [duplicate]

1

I created a basic script to show / hide a return button at the top of the page:

$(document).ready(function(){
        // Add smooth scrolling to all links
        $("a").on('click', function(event) {
            // Make sure this.hash has a value before overriding default behavior
            if (this.hash !== "") {
                // Prevent default anchor click behavior
                event.preventDefault();
                // Store hash
                var hash = this.hash;
                // Using jQuery's animate() method to add smooth page scroll
                // The optional number (800) specifies the number of milliseconds it takes to scroll to the specified area
                $('html, body').animate({
                    scrollTop: $(hash).offset().top
                }, 800, function(){
                    // Add hash (#) to URL when done scrolling (default click behavior)
                    window.location.hash = hash;
                });
            } // End if
        });
        //Nav BG and TopBTN function
        (function scrollFn(){
            if($(document).scrollTop() >= 1){
                $("nav").css({"background-color":"rgba(0,0,0,.6)"});
                $(".up-scroll").addClass("display");
                setTimeout(function(){
                    $(".up-scroll").addClass("active");
                },100);
            };
            $(document).scroll(function(){
                if($(this).scrollTop() >= 1){
                    $("nav").css({"background-color":"rgba(0,0,0,.6)"});
                    $(".up-scroll").addClass("display");
                    setTimeout(function(){
                        $(".up-scroll").addClass("active");
                    },100);
                }else{
                    $("nav").css({"background-color":"rgba(0,0,0,0)"});
                    $(".up-scroll").removeClass("active");
                };
            });
        })();
    });
div{
   height:3000px
}
.up-scroll{
        background-color:transparent;
        border: 3px solid rgba(0,0,0,1);
        color:#000;
        padding:6px;
        width:40px;
        height:40px;
        border-radius:50%;
        position:fixed;
        bottom:20px;
        right:20px;
        z-index:50;
        text-align:center;
        opacity:0;
        display:none;
        transition:opacity .4s ease-in-out;
    }
    .up-scroll.display{
        display:block;
    }
    .up-scroll.display.active{
        opacity:1;
    }
<div>

</div>
<a href="#home" class="up-scroll"><span class="sr-only">Voltar ao topo</span><i class="fas fa-arrow-up"></i></a>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

In this case, in this snippet the button will not work, when clicking, however, my problem is in making the button hidden when the user returns to position 0 in the page scroll.

  • No errors pointed to in console
asked by anonymous 17.04.2018 / 19:44

1 answer

1

The problem is this setTimeout I commented:

$(document).scroll(function(){
    if($(this).scrollTop() >= 1){
        $("nav").css({"background-color":"rgba(0,0,0,.6)"});
        $(".up-scroll").addClass("display");
//                    setTimeout(function(){
            $(".up-scroll").addClass("active");
//                    },100);
    }else{
        $("nav").css({"background-color":"rgba(0,0,0,0)"});
        $(".up-scroll").removeClass("active");
    };
});

It is re-adding the class .active when the scroll reaches 0 because it has a delay of 100ms when the scroll is still equal to or greater than 1 . This time of 100ms is a high value in relation to the time the scroll changes from 1 to 0 . You can solve by decreasing the value to 10, which is enough so that the timer does not reinsert the class in the element.

Example:

$(document).ready(function(){
        // Add smooth scrolling to all links
        $("a").on('click', function(event) {
            // Make sure this.hash has a value before overriding default behavior
            if (this.hash !== "") {
                // Prevent default anchor click behavior
                event.preventDefault();
                // Store hash
                var hash = this.hash;
                // Using jQuery's animate() method to add smooth page scroll
                // The optional number (800) specifies the number of milliseconds it takes to scroll to the specified area
                $('html, body').animate({
                    scrollTop: $(hash).offset().top
                }, 800, function(){
                    // Add hash (#) to URL when done scrolling (default click behavior)
                    window.location.hash = hash;
                });
            } // End if
        });
        //Nav BG and TopBTN function
        (function scrollFn(){
            if($(document).scrollTop() >= 1){
                $("nav").css({"background-color":"rgba(0,0,0,.6)"});
                $(".up-scroll").addClass("display");
                setTimeout(function(){
                    $(".up-scroll").addClass("active");
                },100);
            };
            $(document).scroll(function(){
                if($(this).scrollTop() >= 1){
                    $("nav").css({"background-color":"rgba(0,0,0,.6)"});
                    $(".up-scroll").addClass("display");
                    setTimeout(function(){
                        $(".up-scroll").addClass("active");
                    },10);
                }else{
                    $("nav").css({"background-color":"rgba(0,0,0,0)"});
                    $(".up-scroll").removeClass("active");
                };
            });
        })();
    });
body{
   margin: 0;
}
div{
   height:3000px
}
.up-scroll{
        background-color:transparent;
        border: 3px solid rgba(0,0,0,1);
        color:#000;
        padding:6px;
        width:40px;
        height:40px;
        border-radius:50%;
        position:fixed;
        bottom:20px;
        right:20px;
        z-index:50;
        text-align:center;
        opacity:0;
        display:none;
        transition:opacity .4s ease-in-out;
    }
    .up-scroll.display{
        display:block;
    }
    .up-scroll.display.active{
        opacity:1;
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><divid="home">
home
</div>
<a href="#home" class="up-scroll"><span class="sr-only">Voltar ao topo</span><i class="fas fa-arrow-up"></i></a>

Using fadeIn, fadeOut

$(document).ready(function(){
     // Add smooth scrolling to all links
     $("a").on('click', function(event) {
         // Make sure this.hash has a value before overriding default behavior
         if (this.hash !== "") {
             // Prevent default anchor click behavior
             event.preventDefault();
             // Store hash
             var hash = this.hash;
             // Using jQuery's animate() method to add smooth page scroll
             // The optional number (800) specifies the number of milliseconds it takes to scroll to the specified area
             $('html, body').animate({
                 scrollTop: $(hash).offset().top
             }, 800, function(){
                 // Add hash (#) to URL when done scrolling (default click behavior)
                 window.location.hash = hash;
             });
         } // End if
     });
     //Nav BG and TopBTN function
     (function scrollFn(){
         if($(document).scrollTop() >= 1){
             $("nav").css({"background-color":"rgba(0,0,0,.6)"});
             $(".up-scroll").fadeIn("slow");
         };
         $(document).scroll(function(){
             if($(this).scrollTop() >= 1){
                 $("nav").css({"background-color":"rgba(0,0,0,.6)"});
                 $(".up-scroll").fadeIn("slow");
             }else{
                 $("nav").css({"background-color":"rgba(0,0,0,0)"});
                 $(".up-scroll").fadeOut("slow");
             };
         });
     })();
 });
body{
   margin: 0;
}
div{
   height:3000px
}
.up-scroll{
        background-color:transparent;
        border: 3px solid rgba(0,0,0,1);
        color:#000;
        padding:6px;
        width:40px;
        height:40px;
        border-radius:50%;
        position:fixed;
        bottom:20px;
        right:20px;
        z-index:50;
        text-align:center;
        /*opacity:0;*/
        display:none;
        /*transition:opacity .4s ease-in-out;*/
    }
/*    .up-scroll.display{
        display:block;
    }
    .up-scroll.display.active{
        opacity:1;
    }*/
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><divid="home">
home
</div>
<a href="#home" class="up-scroll"><span class="sr-only">Voltar ao topo</span><i class="fas fa-arrow-up"></i></a>
    
17.04.2018 / 20:10