How to scroll to next div using Javascript?

前端 未结 4 1444
日久生厌
日久生厌 2020-12-15 17:25

So I\'m making a website with a lot of Divs that take 100% height. And I want to make a button so when it\'s clicked to smoothly scroll to next div. I\'ve coded something s

4条回答
  •  余生分开走
    2020-12-15 17:54

    Use same class name for container.Start with first element.Each time click target the next scroller element

    var f = $('.p1');
    var nxt = f;
    $(".next").click(function() {
    
      if (nxt.next('.scroller').length > 0) {
        nxt = nxt.next('.scroller');
      } else {
        nxt = f;
      }
      $('html,body').animate({
          scrollTop: nxt.offset().top
        },
        'slow');
    });
    body {
      margin: 0;
      height: 100%;
    }
    
    .p1 {
      height: 100vh;
      width: 70%;
      background-color: #2196F3;
    }
    
    .p2 {
      height: 100vh;
      width: 70%;
      background-color: #E91E63;
    }
    
    .p3 {
      height: 100vh;
      width: 70%;
      background-color: #01579B;
    }
    
    .admin {
      background-color: #B71C1C;
      height: 100vh;
      position: fixed;
      right: 0%;
      top: 0%;
      width: 30%;
      float: left;
    }
    
    

提交回复
热议问题