smooth scroll to top

前端 未结 12 1157
眼角桃花
眼角桃花 2020-12-07 22:58

I\'ve bean searching for this for a few hours now and I have no solution. I want a smooth scroll to the top of the page. I already have smooth scrolling to separate anchors

相关标签:
12条回答
  • 2020-12-07 23:21
    window.scroll({top: 0, behavior: "smooth"})
    

    Just use this piece of code and it will work perfectly, You can wrap it into a method or event.

    0 讨论(0)
  • 2020-12-07 23:26

    Pure Javascript only

    var t1 = 0;
    window.onscroll = scroll1;
    
    function scroll1() {
      var toTop = document.getElementById('toTop');
      window.scrollY > 0 ? toTop.style.display = 'Block' : toTop.style.display = 'none';
    }
    
    function abcd() {
      var y1 = window.scrollY;
      y1 = y1 - 1000;
      window.scrollTo(0, y1);
      if (y1 > 0) {
        t1 = setTimeout("abcd()", 100);
      } else {
        clearTimeout(t1);
      }
    }
    #toTop {
      display: block;
      position: fixed;
      bottom: 20px;
      right: 20px;
      font-size: 48px;
    }
    
    #toTop {
      transition: all 0.5s ease 0s;
      -moz-transition: all 0.5s ease 0s;
      -webkit-transition: all 0.5s ease 0s;
      -o-transition: all 0.5s ease 0s;
      opacity: 0.5;
      display: none;
      cursor: pointer;
    }
    
    #toTop:hover {
      opacity: 1;
    }
    <p>your text here</p>
    <img id="toTop" src="http://via.placeholder.com/50x50" onclick="abcd()" title="Go To Top">

    0 讨论(0)
  • 2020-12-07 23:26

    Hmm comment function off for me,

    try this

    $(document).ready(function(){
        $("#top").hide();
        $(function toTop() {
            $(window).scroll(function () {
                if ($(this).scrollTop() > 100) {
                    $('#top').fadeIn();
                } else {
                    $('#top').fadeOut();
                }
            });
    
            $('#top').click(function () {
                $('body,html').animate({
                    scrollTop: 0
                }, 800);
                return false;
            });
        });         
                
                
        
        });
    #top {
      float:right;
      width:39px;
      margin-top:-35px; 
    }
    #top {
        transition: all 0.5s ease 0s;
        -moz-transition: all 0.5s ease 0s;
        -webkit-transition: all 0.5s ease 0s;
        -o-transition: all 0.5s ease 0s;
        opacity: 0.5;
        display:none;
        cursor: pointer;
    }
    #top:hover {
        opacity: 1;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
    <div id="top" onclick="toTop()"><img src="to_top.png" alt="no pic "/> klick to top</div>

    0 讨论(0)
  • 2020-12-07 23:27

    Elegant easy solution using jQuery.

    <script>
        function call() {
            var body = $("html, body");
            body.stop().animate({scrollTop:0}, 500, 'swing', function() {
            });
        }
    </script>
    

    and in your html : <div onclick="call()"><img src="../img/arrow-up@2x.png"></div>

    0 讨论(0)
  • 2020-12-07 23:28

    Here is my proposal implemented with ES6

    const scrollToTop = () => {
      const c = document.documentElement.scrollTop || document.body.scrollTop;
      if (c > 0) {
        window.requestAnimationFrame(scrollToTop);
        window.scrollTo(0, c - c / 8);
      }
    };
    scrollToTop();
    

    Tip: for slower motion of the scrolling, increase the hardcoded number 8. The bigger the number - the smoother/slower the scrolling.

    0 讨论(0)
  • 2020-12-07 23:28

    Some time has passed since this was asked.

    Now it is possible to not only specify number to window.scroll function, but also pass an object with three properties: top, left and behavior. So if we would like to have a smooth scroll up with native JavaScript, we can now do something like this:

    let button = document.querySelector('button-id');
    let options = {top: 0, left: 0, behavior: 'smooth'}; // left and top are coordinates
    button.addEventListener('click', () => { window.scroll(options) });
    

    https://developer.mozilla.org/en-US/docs/Web/API/Window/scroll

    0 讨论(0)
提交回复
热议问题