javascript – The script turns up to the anchor with a bug

Question:

There is a script for smooth scrolling to the anchor, but after execution for 0.2 ms, some kind of jump appears on the screen, after which it scrolls to the anchor.

What could be the problem?

$(function() {
  if ($(window).scrollTop() >= "250") 
    $("#ToTop").fadeIn("slow")
    
  $(window).scroll(function() {
    if ($(window).scrollTop() <= "250") 
      $("#ToTop").fadeOut("slow")
    else 
      $("#ToTop").fadeIn("slow")
  });

  if ($(window).scrollTop() <= $(document).height() - "") 
    $("#rentacar").fadeIn("slow")
    
  $(window).scroll(function() {
    if ($(window).scrollTop() >= $(document).height() - "") 
      $("#rentacar").fadeOut("slow")
    else 
      $("#rentacar").fadeIn("slow")
  });

  $("#ToTop").click(function() {
    $("html,body").animate({
      scrollTop: 0
    }, "slow")
  })
  
  $("#rentacar").click(function() {
    $("html,body").animate({
      scrollTop: 1500
    }, "slow")
  })
});

Answer:

Here you are:

$(function() {
    function top() {
        if ($(window).scrollTop() <= "250")
            $("#ToTop").fadeOut("slow")
        else
            $("#ToTop").fadeIn("slow")
    }
    top()

    function scr() {
        if ($(window).scrollTop() >= $(document).height() - "")
            $("#rentacar").fadeOut("slow")
        else
            $("#rentacar").fadeIn("slow")
    }
    scr()
    $(window).scroll(function() {
        top()
        scr()
    });

    $("#ToTop").on('click', function() {
        $("body").animate({
            scrollTop: 0
        }, 500, "linear")
        return false;
    })

    $("#rentacar").click(function() {
        $("html,body").animate({
            scrollTop: 1500
        }, "slow")
        return false;
    })
});
Scroll to Top