简体   繁体   English

jQuery“返回顶部”箭头在滚动前跳转到页面顶部

[英]jQuery “Back-to-top” arrow jumps to top of page before scrolling

I have a static html/css/js webpage which has a jQuery Back-to-Top button that appears at the bottom after the page has scrolled down 20px.我有一个静态 html/css/js 网页,它有一个 jQuery Back-to-Top 按钮,在页面向下滚动 20px 后显示在底部。 The arrow, scrolling, etc all work great, except for one super-annoying bug that's bothering me:箭头、滚动等都很好用,除了一个让我烦恼的超级烦人的错误:

When I click on the Back-to-Top arrow, the arrow itself jumps up to the top of the screen for literally a second, and then it returns to it's proper spot and the page scrolls like it's supposed to.当我点击 Back-to-Top 箭头时,箭头本身会跳到屏幕顶部一秒钟,然后它返回到它的正确位置,页面像它应该的那样滚动。 Here's a gif screenshot.这是一个gif截图。 Watch it carefully;仔细观察; the arrow jumps very quickly: https://drive.google.com/open?id=0B243NwSBmRtdUDRvczZMQ0p6QVE箭头跳得很快: https : //drive.google.com/open?id=0B243NwSBmRtdUDRvczZMQ0p6QVE

And here's the HTML:这是 HTML:

...</nav>
<a href="#" id="toTopBtn">
    <img src="img/arrow-up-white.png" alt="Back to Top" title="Back to Top"/>
</a>
<section ...

And the CSS:和 CSS:

/** BACK TO TOP **/
#toTopBtn {
    display: none; position: fixed;
    bottom: 20px; right: 30px;
    z-index: 98; padding: 15px; 
}
#toTopBtn img {width:80%;}
#myBtn:hover {
    background-color: #555; /* Add a dark-grey background on hover */
}

And the Javascript/Jquery:和 Javascript/Jquery:

/******* BACK TO TOP BUTTON *******/
window.onscroll = function() {scrollFunction()};

// Show "Back to Top" button when user scrolls down 20px from the top of the document
function scrollFunction() {
    if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) {
        $("#toTopBtn").style.display = "block";
    } else {
        $("#toTopBtn").style.display = "none";
    }
}

// When the user clicks on the button, scroll to the top of the document
$("#toTopBtn").click(function() {
    $('html, body').animate({
      scrollTop: $("section#home").offset().top }, 1000);
});

Any tips for how to fix this?有关如何解决此问题的任何提示?

I have been using this one on many sites, all working well so give a try.我一直在许多网站上使用这个,一切都很好,所以试一试。

 $(document).ready(function() { $(window).scroll(function() { if ($(this).scrollTop() > 20) { $('#toTopBtn').fadeIn(); } else { $('#toTopBtn').fadeOut(); } }); $('#toTopBtn').click(function() { $("html, body").animate({ scrollTop: 0 }, 1000); return false; }); });
 /** BACK TO TOP **/ #toTopBtn { display: none; position: fixed; bottom: 20px; right: 30px; z-index: 98; padding: 15px; } #toTopBtn img { width: 80%; } #myBtn:hover { background-color: #555; /* Add a dark-grey background on hover */ }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <span id="toTopBtn"> <img src="http://placehold.it/50x50" alt="Back to Top" title="Back to Top" /> </span> <pre> a a a a a a a a a a a a a a aa a a a aa a a a a b b b b b b b b b b bb b b b b bb b b b b bb b b b b b2 2 2 2 2 2 2 2 2 2 2 b </pre>

Here you have solution for Up + Down.在这里你有向上+向下的解决方案。

    <span id="toTopBtn">
      <img src="http://placehold.it/50x50" alt="Back to Top" title="Back to Top" />
    </span>
    <span id="toBottomBtn">
      <img src="http://placehold.it/50x50" alt="Back to Bottom" title="Back to Bottom" />
    </span>

    <style>     
        #toTopBtn {
          display: none;
          position: fixed;
          bottom: 20px;
          right: 30px;
          z-index: 98;
        }

        #toTopBtn img {
          width: 80%;
        }

        #myBtn:hover {
          background-color: #555;
          /* Add a dark-grey background on hover */
        }

        #toBottomBtn {
          display: none;
          position: fixed;
          bottom: 20px;
          right: 75px;
          z-index: 98;
        }

        #toBottomBtn img {
          width: 80%;
        }
    </style>

    <script>
      $(function(){
          $(window).scroll(function() {
            if ($(this).scrollTop() > 20) {
              $('#toTopBtn').fadeIn();
            } else {
              $('#toTopBtn').fadeOut();
            }
          });

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

          $(window).scroll(function() {
            if (($(document).height() - $(window).height() - $(window).scrollTop()) == 0) {
              $('#toBottomBtn').fadeOut();
            } else {
              $('#toBottomBtn').fadeIn();
            }
          });

          $('#toBottomBtn').click(function() {
            $("html, body").animate({
              scrollTop: $(document).height()
            }, 1000);
            return false;
          });
      });
    </script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM