smooth scroll to top

前端 未结 12 1224
眼角桃花
眼角桃花 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: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;
    }

    your text here

提交回复
热议问题