Scrolling to an Anchor using Transition/CSS3

前端 未结 10 878
-上瘾入骨i
-上瘾入骨i 2020-11-30 20:07

I have a series of links which are using an anchor mechanism:


                      
相关标签:
10条回答
  • 2020-11-30 21:02

    While some of the answers were very useful and informative, I thought I would write down the answer I came up with. The answer from Alex was very good, it is however limited in the sense that the height of the div needs to be hard coded in the CSS.

    So the solution I came up with uses JS (no jQuery) and is actually a stripped down version (almost to the minimum) of over solutions to solve similar problems I found on Statckoverflow:

    HTML

    <div class="header">
        <p class="menu"><a href="#S1" onclick="test('S1'); return false;">S1</a></p>
        <p class="menu"><a href="#S2" onclick="test('S2'); return false;">S2</a></p>
        <p class="menu"><a href="#S3" onclick="test('S3'); return false;">S3</a></p>
        <p class="menu"><a href="#S4" onclick="test('S4'); return false;">S3</a></p>
    </div>
    <div style="width: 100%;">
        <div id="S1" class="curtain">
        blabla
        </div>
        <div id="S2" class="curtain">
        blabla
        </div>
        <div id="S3" class="curtain">
        blabla
        </div>
        <div id="S4" class="curtain">
        blabla
        </div>
     </div>
    

    NOTE THE "RETURN FALSE;" in the on click call. This is important if you want to avoid having your browser jumping to the link itself (and let the effect being managed by your JS).

    JS code:

    <script>
    function scrollTo(to, duration) {
        if (document.body.scrollTop == to) return;
        var diff = to - document.body.scrollTop;
        var scrollStep = Math.PI / (duration / 10);
        var count = 0, currPos;
        start = element.scrollTop;
        scrollInterval = setInterval(function(){
            if (document.body.scrollTop != to) {
                count = count + 1;
                currPos = start + diff * (0.5 - 0.5 * Math.cos(count * scrollStep));
                document.body.scrollTop = currPos;
            }
            else { clearInterval(scrollInterval); }
        },10);
    }
    
    function test(elID)
    {
        var dest = document.getElementById(elID);
        scrollTo(dest.offsetTop, 500);
    }
    </script>
    

    It's incredibly simple. It finds the vertical position of the div in the document using its unique ID (in the function test). Then it calls the scrollTo function passing the starting position (document.body.scrollTop) and the destination position (dest.offsetTop). It performs the transition using some sort of ease-inout curve.

    Thanks everyone for your help.

    Knowing a bit of coding can help you avoiding (sometimes heavy) libraries, and giving you (the programmer) more control.

    0 讨论(0)
  • 2020-11-30 21:03

    Using the scroll-behavior CSS property:

    (which is supported in modern browsers but not Edge):

    a {
      display: inline-block;
      padding: 5px 7%;
      text-decoration: none;
    }
    
    nav, section {
      display: block;
      margin: 0 auto;
      text-align: center;
    }
    
    nav {
      width: 350px;
      padding: 5px;
    }
    
    section {
      width: 350px;
      height: 130px;
      overflow-y: scroll;
      border: 1px solid black;
      font-size: 0; 
      scroll-behavior: smooth;    /* <----- THE SECRET ---- */
    }
    
    section div{
      display: flex;
      align-items: center;
      justify-content: center;
      height: 100%;
      font-size: 8vw;
    }
    <nav>
      <a href="#page-1">1</a>
      <a href="#page-2">2</a>
      <a href="#page-3">3</a>
    </nav>
    <section>
      <div id="page-1">1</div>
      <div id="page-2">2</div>
      <div id="page-3">3</div>
    </section>

    0 讨论(0)
  • 2020-11-30 21:08

    Only mozilla implements a simple property in css : http://caniuse.com/#search=scroll-behavior

    you will have to use JS at least.

    I personally use this because its easy to use (I use JQ but you can adapt it I guess):

    /*Scroll transition to anchor*/
    $("a.toscroll").on('click',function(e) {
        var url = e.target.href;
        var hash = url.substring(url.indexOf("#")+1);
        $('html, body').animate({
            scrollTop: $('#'+hash).offset().top
        }, 500);
        return false;
    });
    

    just add class toscroll to your a tag

    0 讨论(0)
  • 2020-11-30 21:11

    Just apply scroll behaviour to all elements with this one-line code:

    *{
    scroll-behavior: smooth !important;
    }

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