Smooth scroll to specific div on click

后端 未结 7 1433
抹茶落季
抹茶落季 2020-11-28 02:36

What I\'m trying to do is make it so that if you click on a button, it scrolls down (smoothly) to a specific div on the page.

What I need is if you click on the butt

相关标签:
7条回答
  • 2020-11-28 02:39

    do:

    $("button").click(function() {
        $('html,body').animate({
            scrollTop: $(".second").offset().top},
            'slow');
    });
    

    Updated Jsfiddle

    0 讨论(0)
  • 2020-11-28 02:39

    What if u use scrollIntoView function?

    var elmntToView = document.getElementById("sectionId");
    elmntToView.scrollIntoView(); 
    

    Has {behavior: "smooth"} too.... ;) https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView

    0 讨论(0)
  • 2020-11-28 02:49

    Ned Rockson basically answers this question. However there is a fatal flaw within his solution. When the targeted element is closer to the bottom of the page than the viewport-height, the function doesn't reach its exit statement and traps the user on the bottom of the page. This is simply solved by limiting the iteration count.

    var smoothScroll = function(elementId) {
        var MIN_PIXELS_PER_STEP = 16;
        var MAX_SCROLL_STEPS = 30;
        var target = document.getElementById(elementId);
        var scrollContainer = target;
        do {
            scrollContainer = scrollContainer.parentNode;
            if (!scrollContainer) return;
            scrollContainer.scrollTop += 1;
        } while (scrollContainer.scrollTop == 0);
    
        var targetY = 0;
        do {
            if (target == scrollContainer) break;
            targetY += target.offsetTop;
        } while (target = target.offsetParent);
    
        var pixelsPerStep = Math.max(MIN_PIXELS_PER_STEP,
                                     (targetY - scrollContainer.scrollTop) / MAX_SCROLL_STEPS);
    
        var iterations = 0;
        var stepFunc = function() {
            if(iterations > MAX_SCROLL_STEPS){
                return;
            }
            scrollContainer.scrollTop =
                Math.min(targetY, pixelsPerStep + scrollContainer.scrollTop);
    
            if (scrollContainer.scrollTop >= targetY) {
                return;
            }
    
            window.requestAnimationFrame(stepFunc);
        };
    
        window.requestAnimationFrame(stepFunc);
    }
    
    0 讨论(0)
  • 2020-11-28 02:58

    There are many examples of smooth scrolling using JS libraries like jQuery, Mootools, Prototype, etc.

    The following example is on pure JavaScript. If you have no jQuery/Mootools/Prototype on page or you don't want to overload page with heavy JS libraries the example will be of help.

    http://jsfiddle.net/rjSfP/

    HTML Part:

    <div class="first"><button type="button" onclick="smoothScroll(document.getElementById('second'))">Click Me!</button></div>
    <div class="second" id="second">Hi</div>
    

    CSS Part:

    .first {
        width: 100%;
        height: 1000px;
        background: #ccc;
    }
    
    .second {
        width: 100%;
        height: 1000px;
        background: #999;
    }
    

    JS Part:

    window.smoothScroll = function(target) {
        var scrollContainer = target;
        do { //find scroll container
            scrollContainer = scrollContainer.parentNode;
            if (!scrollContainer) return;
            scrollContainer.scrollTop += 1;
        } while (scrollContainer.scrollTop == 0);
    
        var targetY = 0;
        do { //find the top of target relatively to the container
            if (target == scrollContainer) break;
            targetY += target.offsetTop;
        } while (target = target.offsetParent);
    
        scroll = function(c, a, b, i) {
            i++; if (i > 30) return;
            c.scrollTop = a + (b - a) / 30 * i;
            setTimeout(function(){ scroll(c, a, b, i); }, 20);
        }
        // start scrolling
        scroll(scrollContainer, scrollContainer.scrollTop, targetY, 0);
    }
    
    0 讨论(0)
  • 2020-11-28 03:04

    You can use basic css to achieve smooth scroll

    html {
      scroll-behavior: smooth;
    }
    
    0 讨论(0)
  • 2020-11-28 03:05

    I took the Ned Rockson's version and adjusted it to allow upwards scrolls as well.

    var smoothScroll = function(elementId) {
      var MIN_PIXELS_PER_STEP = 16;
      var MAX_SCROLL_STEPS = 30;
      var target = document.getElementById(elementId);
      var scrollContainer = target;
      do {
        scrollContainer = scrollContainer.parentNode;
        if (!scrollContainer) return;
        scrollContainer.scrollTop += 1;
      } while (scrollContainer.scrollTop === 0);
    
      var targetY = 0;
      do {
        if (target === scrollContainer) break;
        targetY += target.offsetTop;
      } while (target = target.offsetParent);
    
      var pixelsPerStep = Math.max(MIN_PIXELS_PER_STEP,
        Math.abs(targetY - scrollContainer.scrollTop) / MAX_SCROLL_STEPS);
    
      var isUp = targetY < scrollContainer.scrollTop;
    
      var stepFunc = function() {
        if (isUp) {
          scrollContainer.scrollTop = Math.max(targetY, scrollContainer.scrollTop - pixelsPerStep);
          if (scrollContainer.scrollTop <= targetY) {
            return;
          }
        } else {
            scrollContainer.scrollTop = Math.min(targetY, scrollContainer.scrollTop + pixelsPerStep);
    
          if (scrollContainer.scrollTop >= targetY) {
            return;
          }
        }
    
        window.requestAnimationFrame(stepFunc);
      };
    
      window.requestAnimationFrame(stepFunc);
    };
    
    0 讨论(0)
提交回复
热议问题