Smooth scrolling on page

Asked

Viewed 3,850 times

5

I wanted to know how to do it so that once I click on a page link in the main menu, the page will scroll smoothly to the desired page that was clicked?

Example: http://worksofwisnu.com/theme-preview/katemi/product-layout/

I wanted an example using links, there is a question using buttons here but I’m having trouble converting. I’m starting to study jQuery now.

I have the following code:

<nav class="collapse navbar-collapse clearfix" role="navigation">
    <ul class="nav navbar-nav navbar-right">
        <li class="active"><a href="#">Home</a>
        </li>
        <li><a href="#nos">Sobre nós</a>
        </li>
        <li><a href="#servico">Serviços</a>
        </li>
    </ul>
</nav>
<section id="servico" class="wow fadeInUp">
    <div class="container">
        <div class="row">
            <div class="col-md-3 col-sm-3">
                <div class="service-content"> <a href="#."><span class="service-icon"><i class="fa fa-desktop img-circle wow flipInX"></i></span></a>

                     <h3>Responsive Layout</h3>

                    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque ultricies vestibulum Graphic river molestie.</p>
                </div>
            </div>
        </div>
    </div>
</section>

http://jsfiddle.net/yLeY4/

  • What you need to do is use the idea of the other answer using the href of the link to search for the id of the target element and switch inside the Animate to scrollTo. I can show you how much more to come and if no one else will answer. However it would be nice to have the definitive HTML with the href and id with the right names.

  • The site is still under construction, I’m testing some passages ready because I know little of jQuery. It’s a form of study.

  • In the example of Sérgio the links are "defective", if you click twice or more on the link element1, it keeps trying to find the id and if you click twice or more on the link element2 it goes from one anchor to another successively. easy to "FIX" but if we give scripts ready, no one will want to study deeply to understand how it works. That’s why the forum is full of questions or duplicated questions, because people will ask until they get the script ready.

1 answer

3


João, the page you indicated uses jQuery. Doing this with simple javascript is very difficult. If you can use jQuery then the answer @bfavaretto indicated is the answer to your problem. If you use another library tell us which one we try to help.

But if you want to use javascript only, you can do this with CSS:

Create a wrapper (external div) that has another div inside that works like an elevator. There uses transition: top 1s ease-in-out; in CSS and can simulate scroll.

Example: http://jsfiddle.net/Je6RR/1

var minhaDiv = document.getElementById('minhaDiv');
var wrapper = document.getElementById('minhaDiv');

function fazerScroll(event) {

    var posicaoAtual = parseInt(minhaDiv.style.top, 10) || 0;
    var alturaDiv = minhaDiv.getBoundingClientRect().height;
    console.log(alturaDiv, posicaoAtual);

    var div = event.target,
        direcao;

    // roda mouse
    var direcaoRoda = event.wheelDelta ? event.wheelDelta : -(event.deltaY ? event.deltaY : event.detail);
    if (event.type == 'wheel') direcao = direcaoRoda > 0 ? 1 : -1;


    // botoes
    if (!direcao) direcao = ~div.className.indexOf('cima') ? 1 : -1;
    if (posicaoAtual >= 0 && direcao == 1) return;
    if (posicaoAtual - 200 < -alturaDiv && direcao == -1) return;
    minhaDiv.style.top = (posicaoAtual + (100 * direcao)) + 'px';
}
var botoes = document.querySelectorAll('button');
for (var i = 0; i < botoes.length; i++) {
    botoes[i].addEventListener('click', fazerScroll);
}
window.addEventListener('wheel', fazerScroll);
minhaDiv.style.top = '0px';

the HTML would be:

<div id="wrapper">
    <div id="minhaDiv">
        <button class="baixo" type="button">Ir para baixo...</button>
        <p>conteudo................. </p>
        <button class="cima" type="button">Ir para cima</button>
    </div>
</div>

This solution does what it needs. Eventually it can be adjusted to also hear older mouse wheel events or even "touch" Events. But as I said above, without a library it gets more work.

  • It’s jquery itself, I got confused but this example of yours with css, js and html was good. But the solution of @bfavaretto, as I understand it is for buttons, I wanted to links.

  • @João, this is what you’re looking for: http://jsfiddle.net/XBUrZ/ ?

  • That’s it @Sergio but how would I implement it in my code? If I can do it using my code, thank you.

  • @João, fix the HTML of the question for what you want. Then I’ll be happy to help with jQuery.

  • my HTML is far from ready, but if you can comment in code what each line does or means, would already be good.

  • @João, I added comments here: http://jsfiddle.net/XBUrZ/1/

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.