Help to animate Back to top button. Javascript or Jquery

Asked

Viewed 353 times

0

Good afternoon, I need to "animate" a Scroll to top button.

It’s already perfectly formatted in CSS, and it works (back to top), what I really need is for it not to appear when the page loads, but only after the page scrolls down a few pixels.

example of the site: https://codigofonte.com.br

Thanks for your help, I’m a beginner.

<a id='icon-smoothscroll-top' class="icon-smoothscroll-top" href="#top" alt="Voltar ao topo" > <title>Voltar ao topo</title> </a>

  <script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
  <script type="text/javascript">
$(document).ready(function(){

    $(window).scroll(function(){
        if ($(this).icon-smoothscroll-top() > 100) {
            $('#icon-smoothscroll-topp').fadeIn();
        } else {
            $('#icon-smoothscroll-top').fadeOut();
        }
    });

    $('#scrollToTop').click(function(){
        $('html, body').animate({scrollTop : 0},800);
        return false;
    }); 
});

</script>
@media (min-width: 992px){

    .icon-smoothscroll-top::before{
        font-family: icomoon; 
        content: '\e112';
         color: #ffffff;
         display: inline-block; 
         font-weight: 400; 
         font-size: 42px;
         border-radius: 33px;  
         background-color: #353535;
         position:fixed;
         text-align:center;
         opacity: .8;
         z-index:99;
         width:60px;
         height:60px; 
         line-height:44px;
         right:10px;
         bottom:50px;
         padding: 5px;
         text-decoration: none; 
         } 

     .icon-smoothscroll-top:hover::before {
         content: '\e112'; 
         color: #ffffff;  
         background-color: #000000;
         text-decoration: none;
     }

     .icon-smoothscroll-top a:hover::before{
        font-family: icomoon; 
        content: '\e112'; 
        color: #ffffff;
        text-decoration: none;
    } 
    }
  • I can open the script inside the html, or call it through href?

1 answer

2

Use the function scrollTop Jquery and check that the returned value is greater than the desired number of pixels.

$(document).ready(function() {

  $('#icon-smoothscroll-top').hide(); //Esconder o elemento após carregar o JQuery.

  $(window).scroll(function() {
    if ($(this).scrollTop() > 100) {
      $('#icon-smoothscroll-top').fadeIn();
    } else {
      $('#icon-smoothscroll-top').fadeOut();
    }
  });

  $('#scrollToTop').click(function() {
    $('html, body').animate({
      scrollTop: 0
    }, 800);
    return false;
  });

});
div {
  width: 200px;
  height: 1000px;
}

#icon-smoothscroll-top {
  position: fixed;
  bottom: 20px;
  right: 10px;
  width: 100px;
  padding: 5px 5px;
  color: white;
  background-color: purple;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
<a id="icon-smoothscroll-top">Voltar ao Topo</a>
</div>

  • Victor, is my html code correct? Do I need to make any changes?

  • At first it’s yes @Jean. I don’t see anything wrong with the HTML code you posted here. Why? The answer I gave didn’t solve the problem?

  • Unfortunately not, it keeps popping up when the page loads, I am opening the <script> tag just below the html code; This may be the error?

  • I use the 2 codes you provided? I can put the 2 inside the same <script tag>?

  • @Jean updated my answer so as not to confuse you. Before closing the tag </body> you add javascript code. Thus, you ensure that the elements manipulated by js have already been loaded.

  • Hello Victor, unfortunately now the button disappears completely, and no longer appears. Could there be some error in CSS, since I’m working with Pseudo elements? Thanks a lot for your help

  • Note that in the example I gave the code works normally. The error may be in your stylesheet, note that you use a media query @media (min-width: 992px){, that is, the styles defined in the block of this query will be applied in screens with minimum width of 992px, below that these settings will not be applied.

  • The media query is purposeful, because there is already another button enabled for screens equal to or less than 991px, at this time I use, one of 1800px.

  • Render your page again and open the developer tool to inspect the HTML of your page. Look for the element referring to the button to go back to the top and check the css properties that are being applied to it. In my example, I hide the button via js not to do this by css, because there may be the case that js is not pressed and your button is hidden the merce of an event.

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.